【问题标题】:g++ undefined reference though all files includedg++ 未定义的引用,尽管所有文件都包含在内
【发布时间】:2013-11-28 10:35:43
【问题描述】:

我遇到的问题是,当 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)?您是否指定了链接库?

标签: c++ eclipse gcc g++


【解决方案1】:

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

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

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

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

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

【讨论】:

  • 那些不是共享库。它们没有被编译。只是 .h 和 .c 文件,如果它们被包含,我希望它们被 gcc 编译..
  • 它们是否在您在正在运行的命令 (daytime.o/main.o) 中指定的这两个文件之一中?如果它们不在那里,请在项目中包含包含它们的 .c 文件,如上所述...
猜你喜欢
  • 2017-09-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-15
  • 2015-07-11
  • 2021-10-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多