undefined reference to怎么解决的?

Zhentiwei 2008-06-10 10:56:06
"undefined reference to"怎么解决的?
有下面几个文件:
tiwei@tiwei-desktop:~/graph$ ls
binet.cpp global.cpp graph.cpp group.cpp main_1.cpp node_graph.cpp partition.cpp support.cpp
binet.hpp global.hpp graph.hpp group.hpp Makefile node_graph.hpp partition.hpp support.hpp
tiwei@tiwei-desktop:~/graph$
其中:
graph.cpp和partition.cpp引用了global.cpp中定义的GetNodeDict()函数,在
graph.cpp和partition.cpp中都使用了#include "global.hpp".

下面是我的Makefile文件:(我是改写别人的Makefile文件)
CC = g++

CFLAGS = -g

INCLUDE = -I. -I/usr/include/ -I/usr/include/X11/ -I/usr/local/inclucde

LDFLAGS = -L. -L/usr/lib -L/usr/lib/X11 -L/usr/local/lib

LDLIBS = -L. -lprng -L/usr/lib -lm

SRCS = \
global.cpp graph.cpp group.cpp node_graph.cpp partition.cpp support.cpp \
main_1.cpp

OBJS = \
global.o graph.o group.o node_graph.o partition.o support.o main_1.o

.c.o:
$(CC) $(DEFS) $(INCLUDE) $(CFLAGS) -c $<

all:main

main:$(OBJS)
$(CC) $(CFLAGS) $(INCLUDE) $(OBJS) $(LDFLAGS) $(LDLIBS) -o $@

clean:
rm -f *.o core main

我在网上看了,别人的undefined reference to是用系统的库函数,在-L后加上 -lm就可以了,
或是简单的,
请各位帮我看看Makefile文件怎么写的?我猜可能是Makefile文件的问题。
谢谢了各位!
下面是连接时的错误:
tiwei@tiwei-desktop:~/graph$ sudo make -f Makefile
g++ -c -o global.o global.cpp
g++ -c -o graph.o graph.cpp
g++ -c -o group.o group.cpp
g++ -c -o node_graph.o node_graph.cpp
g++ -c -o partition.o partition.cpp
g++ -c -o support.o support.cpp
g++ -c -o main_1.o main_1.cpp
g++ -g -I. -I/usr/include/ -I/usr/include/X11/ -I/usr/local/inclucde global.o graph.o group.o node_graph.o partition.o support.o main_1.o -L. -L/usr/lib -L/usr/lib/X11 -L/usr/local/lib -L. -lprng -L/usr/lib -lm -o main
graph.o: In function `graph::RewireAdjacencyByLabel()':graph.cpp:(.text+0xe74): undefined reference to `GetNodeDict(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, void*)'
graph.o: In function `graph::ClearAdjacencies()':graph.cpp:(.text+0x1df2): undefined reference to `GetNodeDict(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, void*)'
partition.o: In function `partition::MapPartToNet(graph*)':partition.cpp:(.text+0x232f): undefined reference to `GetNodeDict(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, void*)'
partition.o: In function `partition::partition(graph*, double, double, double, double, int, char, int, char, prng*)':partition.cpp:(.text+0x3da9): undefined reference to `GetNodeDict(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, void*)'
partition.o: In function `partition::partition(graph*, double, double, double, double, int, char, int, char, prng*)':partition.cpp:(.text+0x58c3): undefined reference to `GetNodeDict(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, void*)'
collect2: ld returned 1 exit status
make: *** [main] Error 1
tiwei@tiwei-desktop:~/graph$

...全文
6589 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
wwwypy 2008-12-02
  • 打赏
  • 举报
回复
看看!!!
Zhentiwei 2008-06-11
  • 打赏
  • 举报
回复
在global.hpp中声明的函数
global.hpp文件
extern node_graph *GetNodeDict(string label,void *dict);

在global.cpp中实现的代码
// Find and return a given node by label
node_graph *GetNodDict(string label,void *dict)
{
node_tree *temptnd = new node_tree;
void *treend = NULL;

temptnd->label(label);
treend = tfind((void*)temptnd,&dict,
NodeTreeLabelCompare);

FreeNodeTree(temptnd);

return treend ? (*(node_tree**)treend)->ref() : NULL;
}

在graph.hpp中包含的头文件
graph.hpp文件
#include "global.hpp"

在graph.cpp中的一个成员函数用到的
void graph::RewireAdjacencyByLabel()
{
void *nodeDict = NULL;

// map the nodes into a dictionary
nodeDict = MakeLabelDict();

node_list *adja = NULL;
node_graph *p = _header;
while( (p = p->next()) != NULL )
{
adja = p->neig();
while((adja = adja->next()) != NULL)
{
string a;
adja->ref(GetNodeDict(a,nodeDict));
adja->node(adja->ref()->num());
}
}

// Done
FreeLabelDict(nodeDict);// 奇怪,FreeLabelDict()函数也是和GetNodeDict()一样的,而这个就不出错?

return;

}

graph.cpp中的
void graph::ClearAdjacencies()

函数也是这样用的,
还有partition.cpp中也是这样用的。
就不知道为什么会出错。
谢谢!
帅得不敢出门 2008-06-11
  • 打赏
  • 举报
回复
楼主把代码贴点出来看看.
johnelf 2008-06-11
  • 打赏
  • 举报
回复
路过 看看
babyvox1999 2008-06-10
  • 打赏
  • 举报
回复
你的是GetNodeDict这个函数没定义,可能没包头文件之类的
Zhentiwei 2008-06-10
  • 打赏
  • 举报
回复
我试了,还是不行。
OBJS = global.o graph.o group.o node_graph.o partition.o support.o main_1.o global.o graph.o group.o node_graph.o partition.o support.o main_1.o
有好多的"multiple definition of"错误。
Zhentiwei 2008-06-10
  • 打赏
  • 举报
回复
楼上的,请问为什么写两遍呢?
我试试看
blh 2008-06-10
  • 打赏
  • 举报
回复
try as the following
OBJS = global.o graph.o group.o node_graph.o partition.o support.o main_1.o global.o graph.o group.o node_graph.o partition.o support.o main_1.o


Zhentiwei 2008-06-10
  • 打赏
  • 举报
回复
GetNodeDict函数在graph.cpp声明了,
在partition中没有,可是他们都是一样的错误啊?
是不是Makefile文件写错了啊?
j_willl 2008-06-10
  • 打赏
  • 举报
回复
GetNodeDict在graph.cpp和partition.cpp中声明了吗
Zhentiwei 2008-06-10
  • 打赏
  • 举报
回复
GetNodeDict这个函数在Global.cpp中定义了,在Global.hpp 也中声明了。
在graph.cpp和partition.cpp文件中也加了#include "global.hpp"语句了。

64,639

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧