C++ 对 `__stack_chk_fail' 的未定义引用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4492799/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 15:32:48  来源:igfitidea点击:

undefined reference to `__stack_chk_fail'

c++gccundefined-reference

提问by Akhil

Getting this error while compiling C++ code:

编译 C++ 代码时出现此错误:

undefined reference to `__stack_chk_fail'

Options already tried:

已经尝试过的选项:

  1. added -fno-stack-protector while compiling - did not work, error persists
  2. added a dummy implementation of void __stack_chk_fail(void) in my code. Still getting the same error.
  1. 编译时添加 -fno-stack-protector - 不起作用,错误仍然存​​在
  2. 在我的代码中添加了 void __stack_chk_fail(void) 的虚拟实现。仍然得到同样的错误。

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 gccof the pre-compiled libraries I am using and the gccversion I was using to compile the code. Updated gccand now I am getting only 2 of these errors.

早些时候,我收到了 10 个这样的错误。发现gcc我正在使用的预编译库的gcc版本与我用来编译代码的版本之间存在版本不匹配。已更新gcc,现在我只收到其中 2 个错误。

Any help, please?

请问有什么帮助吗?

回答by gravitron

libgurobi_c++.a was compiled with -fno-stack-protector (obviously).

libgurobi_c++.a 是用 -fno-stack-protector 编译的(显然)。

A few things come to mind:

想到了几件事:

  1. add -fno-stack-protectorwhen linking. This will make sure that libssp gets linked.
  2. Manually link -lssp
  3. 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 AFTERlibgurobi_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.
  1. 链接时添加-fno-stack-protector。这将确保 libssp 被链接。
  2. 手动链接-lssp
  3. 在它自己的目标文件中制作 __stack_chk_fail(void) 的虚拟版本,并在libgurobi_c++.a之后将此 .o 文件添加到您的链接器命令中。GCC/G++ 在链接期间从左到右解析符号,因此尽管您的代码定义了函数,但包含 __stack_chk_fail 符号的对象的副本需要位于 libgurobi_c++.a 右侧的链接器行上。

回答by bjc

https://wiki.ubuntu.com/ToolChain/CompilerFlags

https://wiki.ubuntu.com/ToolChain/CompilerFlags

says:

说:

"Usually this is a result of calling ld instead of gcc during a build to perform linking"

“通常这是在构建期间调用 ld 而不是 gcc 来执行链接的结果”

This is what I encountered when modified the Makefile of libjpeg manually. Use gcc instead of ld solved the problem.

这是我手动修改libjpeg的Makefile时遇到的。使用 gcc 而不是 ld 解决了问题。

回答by Alex

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:

在 gentoo 中,我遇到了同样的问题,我解决了创建 2 个文件的问题。第一个包含由emerge解析并传递给gcc的选项:

/etc/portage/env/nostackprotector.conf
CFLAGS="-fno-stack-protector -O2"

And the second tells which package should use this settings:

第二个告诉哪个包应该使用这个设置:

/etc/portage/package.env/nostackprotector
x11-libs/vte nostackprotector.conf
sys-libs/glibc nostackprotector.conf
www-client/chromium nostackprotector.conf
app-admin/sudo nostackprotector.conf

回答by OneTwoSteph

Just had the same issue: c++ code with an implementation of void __stack_chk_fail(void)showing several undefined reference to __stack_chk_failerrors when compiling.

刚刚遇到了同样的问题:带有 void 实现的 C++ 代码在编译时__stack_chk_fail(void)显示了几个undefined reference to __stack_chk_fail错误。

My solution was to define __stack_chk_fail(void)as extern "C":

我的解决方案是定义__stack_chk_fail(void)extern "C"

extern "C" {
__stack_chk_fail(void)
{
...
}
}

This suppressed the compilation error :)

这抑制了编译错误:)

Hope it helps!

希望能帮助到你!