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

标签 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文件,从而发生此链接器错误? 这里有什么问题?

谢谢。

最佳答案

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

要使链接器找到所有这些方法/类(json_read_stringwrite_stringzexception),您需要引用该库。如果例如它们包含在名为 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/

相关文章:

eclipse - 未绑定(bind)类路径容器 : 'JRE System Library [OSGi/Minimum-1.2]' in Eclipse Juno

java - 一旦你猜对了数字,如何让这个猜数字游戏重新开始?

java - Eclipse:变量的调试和检查导致 com.sun.jdi.ObjectCollectedException

c++ - 限制模板化类数据类型

c++ - 对象的 std::vector 和排序导致段错误

c++ - Pointer->Call() 和 (*Pointer).Call() 之间的区别

c++ - 从链表中删除一个值/删除不是动态分配的值

c++ - 基于ARM的系统中浮点到整数的转换

C++ : gcc compiler warning for large stack allocation

c - 是否有 GCC 关键字允许结构重新排序?