【问题标题】:undefined reference on linux but works fine on maclinux上未定义的引用,但在mac上工作正常
【发布时间】:2014-03-20 00:13:34
【问题描述】:

我正在尝试执行以下操作:

gcc -Ilibconfig controller.c -o Controller libconfig/libconfig.a

但我得到的是:

In function `processConfigFile':
controller.c:(.text+0x10e): undefined reference to `config_init'
controller.c:(.text+0x132): undefined reference to `config_read_file'
controller.c:(.text+0x169): undefined reference to `config_destroy'
controller.c:(.text+0x188): undefined reference to `config_lookup_string'
controller.c:(.text+0x1e0): undefined reference to `config_lookup_string'
controller.c:(.text+0x269): undefined reference to `config_lookup_string'
controller.c:(.text+0x2cc): undefined reference to `config_lookup_string'
controller.c:(.text+0x317): undefined reference to `config_lookup_string'
controller.c:(.text+0x36e): undefined reference to `config_lookup'
controller.c:(.text+0x403): undefined reference to `config_setting_length'
controller.c:(.text+0x454): undefined reference to `config_setting_get_string_elem'
controller.c:(.text+0x4fa): undefined reference to `config_destroy'
collect2: error: ld returned 1 exit status

现在我可以理解链接器无法链接库,但是我已确保库位于文件之后,这在 OSX 上运行良好,我不知道为什么会这样。

如果有人可以帮忙,请。

谢谢

【问题讨论】:

  • 切换命令行参数的顺序有帮助吗?即致电gcc -Ilibconfig -o Controller libconfig/libconfig.a controller.c
  • libconfig/libconfig.a 是在 Linux 系统上从头开始重建的吗?尝试将 OSX 库链接到 Linux 可执行文件可能会有点不稳定......

标签: c linux macos gcc ld


【解决方案1】:

您应该将库链接为库,而不是目标代码。

gcc -Ilibconfig controller.c -o Controller -Llibconfig -lconfig

Linux 和 OS X 使用完全不同的链接器,因此它们在接受的文件类型上可能表现不同。

【讨论】:

  • 我持怀疑态度。在命令行中列出一个文件并不意味着它是一个目标文件; gcc 命令和链接器能够识别不同类型的文件并适当地处理它们。 `-lconfig”只是“libconfig with any library suffix”的缩写,“-L”只是添加一个目录来检查库文件。问题更可能是路径问题(Linux 系统中的“.”中不存在 libconfig/libconfig.a)或库中的问题(未正确构建、未编入索引等)。
  • 看来我需要再次构建库,而我在 OSX 上生成的 .a 文件无法在 linux 上运行。这是否意味着,我需要项目中的整个 libconfig 源,并且每次构建项目时都必须构建它?喜欢做 ./configure 然后每次都做吗?
  • 如果确实是这个原因,那么您只需在检查项目源的新副本时构建它。不是每次构建项目时。
  • 这是否意味着我的项目有一个 ./configure 脚本来构建它?然后生成一个makefile..?
  • 现在我已经安装了packages.ubuntu.com/quantal/libconfig-dev,但同样的问题仍然存在..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-13
  • 2019-10-28
  • 1970-01-01
  • 2019-10-21
  • 2022-08-22
  • 1970-01-01
相关资源
最近更新 更多