对“__stack_chk_fail”的未定义引用

发布于 2024-10-08 08:40:23 字数 1165 浏览 0 评论 0原文

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

undefined reference to `__stack_chk_fail'

选项已尝试:

  1. 编译时添加 -fno-stack-protector - 不起作用,错误仍然存​​在,
  2. 在我的代码中添加了 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:

  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.

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

在巴黎塔顶看东京樱花 2024-10-15 08:40:24

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

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

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

/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

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:

/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
香草可樂 2024-10-15 08:40:24

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.

泪之魂 2024-10-15 08:40:23

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

我想到了一些事情:

  1. 链接时添加 -fstack-protector 。这将确保 libssp 被链接。
  2. 手动链接 -lssp
  3. 在其自己的目标文件中制作 __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:

  1. add -fstack-protector when 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 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.
情未る 2024-10-15 08:40:23

刚刚遇到了同样的问题:带有 void __stack_chk_fail(void) 实现的 C++ 代码在编译时显示了几个 对 __stack_chk_fail 未定义的引用错误。

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

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

这抑制了编译错误:)

希望它有帮助!

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

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

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

This suppressed the compilation error :)

Hope it helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文