对“__stack_chk_fail”的未定义引用
编译 C++ 代码时出现此错误:
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 个错误。
有什么帮助吗?
Getting this error while compiling C++ code:
undefined reference to `__stack_chk_fail'
Options already tried:
- added -fno-stack-protector while compiling - did not work, error persists
- added a dummy implementation of void __stack_chk_fail(void) in my code. Still getting the same error.
Detailed Error:
/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'**
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.
Any help, please?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在gentoo中我遇到了同样的问题,我解决了创建2个文件的问题。第一个包含由 emerge 解析并传递给 gcc 的选项:
第二个告诉哪个包应该使用此设置:
In gentoo I had the same problem and i resolved creating 2 files. The first contain the option to be parsed by emerge and passed to gcc:
And the second tells which package should use this settings:
https://wiki.ubuntu.com/ToolChain/CompilerFlags
说:
“通常这是一个结果在构建期间调用 ld 而不是 gcc 来执行链接”
这是我在手动修改 libjpeg 的 Makefile 时遇到的情况。使用 gcc 而不是 ld 解决了问题。
https://wiki.ubuntu.com/ToolChain/CompilerFlags
says:
"Usually this is a result of calling ld instead of gcc during a build to perform linking"
This is what I encountered when modified the Makefile of libjpeg manually. Use gcc instead of ld solved the problem.
libgurobi_c++.a 是用
-fno-stack-protector
编译的(显然)。我想到了一些事情:
-fstack-protector
。这将确保 libssp 被链接。-lssp
__stack_chk_fail(void)
的虚拟版本,并将此 .o 文件之后 libgurobi_c++.a。 GCC/G++ 在链接期间从左到右解析符号,因此尽管您的代码定义了函数,但包含 __stack_chk_fail 符号的对象的副本需要位于 libgurobi_c++.a 右侧的链接器行上。libgurobi_c++.a was compiled with
-fno-stack-protector
(obviously).A few things come to mind:
-fstack-protector
when linking. This will make sure that libssp gets linked.-lssp
__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.刚刚遇到了同样的问题:带有 void
__stack_chk_fail(void)
实现的 C++ 代码在编译时显示了几个对 __stack_chk_fail
未定义的引用错误。我的解决方案是将
__stack_chk_fail(void)
定义为extern "C"
:这抑制了编译错误:)
希望它有帮助!
Just had the same issue: c++ code with an implementation of void
__stack_chk_fail(void)
showing severalundefined reference to __stack_chk_fail
errors when compiling.My solution was to define
__stack_chk_fail(void)
asextern "C"
:This suppressed the compilation error :)
Hope it helps!