编译C++代码时出现此错误:
undefined reference to `__stack_chk_fail'已尝试的选项:
详细错误:
/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个这样的错误。
有什么需要帮忙的吗?
发布于 2010-12-21 02:58:32
libgurobi_c++.a是使用-fno-stack-protector编译的(显然)。
脑海中浮现出几件事:
链接时添加-fstack-protector
/G++从左到右解析符号,因此尽管您的代码定义了该函数,但包含__stack_chk_fail符号的对象的副本需要位于libgurobi_c++.a.右侧的链接器行上
发布于 2015-04-15 15:20:35
在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发布于 2020-04-02 21:54:57
刚刚遇到了同样的问题:在c++代码中使用了void __stack_chk_fail(void)的实现在编译时显示了几个undefined reference to __stack_chk_fail错误。
我的解决方案是将__stack_chk_fail(void)定义为extern "C"
extern "C" {
__stack_chk_fail(void)
{
...
}
}这抑制了编译错误:)
希望它能帮上忙!
https://stackoverflow.com/questions/4492799
复制相似问题