g ++ undefined reference虽然包含了所有文件

时间:2013-11-28 10:35:44

标签: c++ eclipse gcc g++

我遇到的问题是,当g ++尝试链接目标文件时,我收到以下错误:

11:29:13 **** Build of configuration Debug for project daytime ****
make all 
'Building target: daytime'
'Invoking: Cross G++ Linker'
g++  -o "daytime"  ./tcf/services/daytime.o  ./tcf/main/main.o   
./tcf/services/daytime.o: In function `command_get_time_of_day':
C:\Users\falkstef\runtime-EclipseApplication\daytime\Debug/../tcf/services/daytime.c:38: undefined reference to `json_read_string'
C:\Users\falkstef\runtime-EclipseApplication\daytime\Debug/../tcf/services/daytime.c:40: undefined reference to `exception'
C:\Users\falkstef\runtime-EclipseApplication\daytime\Debug/../tcf/services/daytime.c:43: undefined reference to `exception'
C:\Users\falkstef\runtime-EclipseApplication\daytime\Debug/../tcf/services/daytime.c:52: undefined reference to `write_stringz'
makefile:46: recipe for target 'daytime' failed
C:\Users\falkstef\runtime-EclipseApplication\daytime\Debug/../tcf/services/daytime.c:54: undefined reference to `write_stringz'
C:\Users\falkstef\runtime-EclipseApplication\daytime\Debug/../tcf/services/daytime.c:56: undefined reference to `write_errno'
C:\Users\falkstef\runtime-EclipseApplication\daytime\Debug/../tcf/services/daytime.c:58: undefined reference to `json_write_string'
./tcf/services/daytime.o: In function `ini_daytime_service':
C:\Users\falkstef\runtime-EclipseApplication\daytime\Debug/../tcf/services/daytime.c:70: undefined reference to `add_command_handler'
collect2.exe: error: ld returned 1 exit status
make: *** [daytime] Error 1

我不知道为什么会出现这种情况,例如包含并找到#include <tcf/framework/json.h>

没有gcc编译相应的*.c文件,导致发生此链接器错误? 这有什么问题?

谢谢。

1 个答案:

答案 0 :(得分:1)

仅包含头文件是不够的;您还必须指定定义这些函数的库。

要使链接器找到所有这些方法/类(json_read_stringwrite_stringzexception),您需要引用该库。如果是它们包含在一个名为libjson.so的库中,你应该这样做:

g++ -ljson -o "daytime"  ./tcf/services/daytime.o  ./tcf/main/main.o

(或者将库添加到项目选项中,如果eclipse正在管理你的make文件)。

或者如果它是另一个.o文件,请在编译中包含它( - &gt;或在项目中,如果eclipse正在创建make文件)。