在ubuntu下面写算法,用makefile进行自动编译.
我的目录结构如下:
graph
union_find_set
依赖关系是这样的:
union_find.h引用了graph目录下面的vertex.h graph.cpp 引用了union_find_set目录下面的union_find_set.h.
其他的都是引用各自目录下面的文件了。
我的makefile如下:
union_find_set目录下面的makefile
union_find_set : main.o union_find_set.o union_find.o
g++ -o union_find_set main.o union_find_set.o union_find.o
main.o : main.cpp union_find_set.h
g++ -c main.cpp
union_find_set.o : union_find_set.cpp union_find_set.h
g++ -c union_find_set.cpp
union_find.o : union_find.cpp union_find.h
g++ -c union_find.cpp
clean:
rm union_find_set *.o
2. graph目录下面的makefile
#include ../union_find_set/makefile
graph : main.o graph.o edge.o vertex.o
g++ -o graph main.o graph.o edge.o vertex.o
main.o : main.cpp graph.h
g++ -c main.cpp
graph.o : graph.cpp graph.h ../union_find_set/union_find_set.h
g++ -c graph.cpp
edge.o : edge.cpp edge.h
g++ -c edge.cpp
vertex.o : vertex.cpp vertex.h
g++ -c vertex.cpp
clean :
rm graph *.o
最后编译出现如下错误:
g++ -o graph main.o graph.o edge.o vertex.o graph.o: In function
Graph::Kruskal()': graph.cpp:(.text+0x1328): undefined reference to
UnionFindSet::UnionFindSet()' graph.cpp:(.text+0x13c4): undefined reference toNode::Node(Node*, int)' graph.cpp:(.text+0x13e3): undefined reference to
Node::setData(Vertex)' graph.cpp:(.text+0x13f5): undefined reference toUnionFindSet::addNode(Node*)' graph.cpp:(.text+0x1641): undefined reference to
UnionFindSet::findNode(Vertex)' graph.cpp:(.text+0x1656): undefined reference toUnionFindSet::findNode(Vertex*)' graph.cpp:(.text+0x166b): undefined reference to
UnionFindSet::findSet(Node)' graph.cpp:(.text+0x167f): undefined reference toUnionFindSet::findSet(Node*)' graph.cpp:(.text+0x16bb): undefined reference to
UnionFindSet::merge(Node, Node*)' collect2: ld returned 1 exit status make: *** [graph] Error 1
这几个函数都是union_find_set.h中定义的函数,在graph.cpp中的kruskal函数中用到,请问我的makefile错在哪里啊 多谢了
The functions declared in union_find_set.h cannot be found during linking. You should define these functions in union_find_set.cpp
Therefore, union_find_set.o should be added when generating the graph. That is
changed to
In this way, the previously undefined reference to XXX will be found in union_find_set.o when linking