我有一个问题,当 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
文件,从而发生此链接器错误?
这里有什么问题?
谢谢。
最佳答案
仅包含头文件是不够的;您还必须指定定义这些函数的库。
要使链接器找到所有这些方法/类(json_read_string
、write_stringz
、exception
),您需要引用该库。如果例如它们包含在名为 libjson.so 的库中,您应该这样做:
g++ -ljson -o "daytime" ./tcf/services/daytime.o ./tcf/main/main.o
(或者将库添加到项目选项,如果 Eclipse 正在管理您的 make 文件)。
或者,如果它是另一个 .o 文件,则将其包含在编译中(-> 或在项目中,如果 eclipse 正在创建 make 文件)。
关于c++ - g++ undefined reference 虽然包含所有文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20263701/