【问题标题】:undefined reference to `__stack_chk_fail'对 `__stack_chk_fail' 的未定义引用
【发布时间】:2011-05-28 10:14:43
【问题描述】:

编译 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,现在我只收到其中两个错误。

有什么帮助吗?

【问题讨论】:

    标签: c++ gcc undefined-reference


    【解决方案1】:

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

    我想到了一些事情:

    1. 链接时添加-fstack-protector。这将确保 libssp 得到链接。
    2. 手动链接-lssp
    3. 在自己的目标文件中创建 __stack_chk_fail(void) 的虚拟版本,并将此 .o 文件添加到链接器命令 AFTER libgurobi_c++.a。 GCC/G++ 在链接期间从左到右解析符号,因此尽管您的代码定义了函数,但包含 __stack_chk_fail 符号的对象的副本需要位于 libgurobi_c++.a 右侧的链接器行。

    【讨论】:

    • 1.我之前添加了 -fno-stack-protector 但这没有帮助。 2. 非常感谢,添加 -lssp 有效。 3. 非常感谢!这些信息很有用。这个我忘了。
    • 很高兴解决了这个问题。您是否在链接期间添加了 -fno-stack-protector?也许它把 -lssp 的顺序弄错了,谁知道呢……
    • 虽然它是在我运行程序时编译的,但我得到了这个错误:./jetAlloc:加载共享库时出错:libssp.so.0:无法打开共享对象文件:没有这样的文件或目录跨度>
    • 为什么要添加“no”-fno-stack-protector 以确保 libssp 得到链接?我是不是误会了什么?
    • 确实,它应该是-fstack-protector,除非你想在链接的静态库中禁用它并重新构建它。
    【解决方案2】:

    在 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
    

    【讨论】:

      【解决方案3】:

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

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

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

      这抑制了编译错误:)

      希望对你有帮助!

      【讨论】:

      • 有道理。原因是 C++ 名称修改(可以在 nm 的输出中看到)导致链接器无法找到符号 __stack_chk_fail。
      【解决方案4】:

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

      说:

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

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

      【讨论】:

        猜你喜欢
        • 2011-07-02
        • 1970-01-01
        • 1970-01-01
        • 2011-03-27
        • 2022-01-22
        • 2016-04-04
        • 2015-11-03
        • 2011-08-11
        相关资源
        最近更新 更多