对 `__stack_chk_fail' 的未定义引用
编译 C++ 代码时出现此错误:
Getting this error while compiling C++ code:
undefined reference to `__stack_chk_fail'
已经尝试过的选项:
- 在编译时添加了 -fno-stack-protector - 不起作用,错误仍然存??在
- 在我的代码中添加了 void __stack_chk_fail(void) 的虚拟实现.仍然出现同样的错误.
详细错误:
/u/ac/alanger/gurobi/gurobi400/linux64/lib/libgurobi_c++.a(Env.o)(.text+0x1034): In function `GRBEnv::getPar/u/ac/alanger/gurobi/gurobi400/linux64/lib/libgurobi_c++.a(Env.o)(.text+0x1034): In function `GRBEnv::getParamInfo(GRB_StringParam, std::basic_string<char, std::char_traits<char>, std::allocator<char> >&, std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)':
: undefined reference to `__stack_chk_fail'
amInfo(GRB_StringParam, std::basic_string<char, std::char_traits<char>, std::allocator<char> >&, std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)':
: **undefined reference to `__stack_chk_fail'**
早些时候,我收到了 10 个这样的错误.发现我使用的预编译库的 gcc
和我用来编译代码的 gcc
版本之间存在版本不匹配.更新了 gcc
,现在我只收到了其中的 2 个错误.
Earlier, I was getting 10's of such errors. Found out that there was a version mismatch between the gcc
of the pre-compiled libraries I am using and the gcc
version I was using to compile the code. Updated gcc
and now I am getting only 2 of these errors.
有什么帮助吗?
推荐答案
libgurobi_c++.a 是用 -fno-stack-protector 编译的(显然).
libgurobi_c++.a was compiled with -fno-stack-protector (obviously).
想到一些事情:
- 在链接时添加 -fstack-protector.这将确保 libssp 被链接.
- 手动链接-lssp
- 在它自己的目标文件中制作 __stack_chk_fail(void) 的虚拟版本,然后将此 .o 文件添加到您的链接器命令 AFTER libgurobi_c++.a.GCC/G++ 在链接期间从左到右解析符号,因此尽管您的代码定义了函数,但包含 __stack_chk_fail 符号的对象的副本需要位于 libgurobi_c++.a 右侧的链接器行上.
- add -fstack-protector when linking. This will make sure that libssp gets linked.
- Manually link -lssp
- Make your dummy version of __stack_chk_fail(void) in it's own object file and and add this .o file to your linker command AFTER libgurobi_c++.a. GCC/G++ resolves symbols from left to right during linking so despite your code having the function defined, a copy of an object containing the __stack_chk_fail symbol needs to be on the linker line to the right of libgurobi_c++.a.
相关文章