【问题标题】:undefined reference to main for shared library未定义对共享库的 main 的引用
【发布时间】:2015-01-02 20:12:34
【问题描述】:

我正在尝试构建一个盒子标准共享库(在 ubuntu 上使用 code::blocks 13.12),它显然没有 main 函数,因为在 Windows 上编译得非常好。我收到一堆重定位错误 + 对 main 的未定义引用??

有什么建议吗?我在下面粘贴了输出。

干杯, 迈克

-------------- Build: Debug in utils (compiler: GNU GCC Compiler)---------------

g++ -Wall -fexceptions -std=c++0x -g -fPIC -I -c utils/exception.cpp -o obj/Debug/exception.o
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 0 has invalid symbol index 11
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 1 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 2 has invalid symbol index 2
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 3 has invalid symbol index 2
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 4 has invalid symbol index 11
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 5 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 6 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 7 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 8 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 9 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 10 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 11 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 12 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 13 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 14 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 15 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 16 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 17 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 18 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 19 has invalid symbol index 21
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_line): relocation 0 has invalid symbol index 2
/usr/lib/gcc/x86_64-linux-gnu/4.9/../../../x86_64-linux-gnu/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: error: ld returned 1 exit status

【问题讨论】:

  • 我之前也遇到过很多次这个错误,我通常通过从项目中删除所有文件,在删除所有项目元文件之前删除所有目标文件,关闭 code::blocks,重新打开来修复它code::blocks 并递归地读取所有文件,这通常可以修复它,但工作量很大。不用说这是 code::blocks 中的错误。但是,这也可能是您正在使用的库的错误。
  • 它适用于 Cmake 文件,所以我怀疑它是 code::blocks

标签: c++ shared-libraries main


【解决方案1】:

您的链接命令缺少 -shared 标志,这是链接共享库所必需的。

很遗憾,您的构建系统没有打印失败的链接命令。

错误被简单地复制如下:

echo "int foo() { return 0; }" | gcc -xc -

/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 0 has invalid symbol index 11
...
/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: error: ld returned 1 exit status

【讨论】:

  • 我在链接器设置中添加了 -shared 标志,但没有运气。
  • @Mike "I added ..." -- 要么您将其添加到错误的位置,要么错误已更改。您的第一步应该是弄清楚您正在执行什么实际链接命令。我 99.9% 确定它没有 -shared。
  • 我在弄清楚在 code::blocks 中的何处添加它时遇到了一些麻烦。供下一位读者参考:它位于 other Build Options -> Other Options(在 Compiler Flags 右侧)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-04-09
  • 2013-04-15
  • 2013-06-21
  • 1970-01-01
  • 1970-01-01
  • 2017-03-20
  • 2015-07-11
相关资源
最近更新 更多