40

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?

4 Answers 4

49

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.
6
  • 1
    Glad that solved it. Had you added -fno-stack-protector during linking? Maybe it put the -lssp in the wrong order, who knows...
    – gravitron
    Commented Dec 20, 2010 at 19:15
  • 1
    Though it compiled when I run the program I get this error: ./jetAlloc: error while loading shared libraries: libssp.so.0: cannot open shared object file: No such file or directory
    – Akhil
    Commented Dec 22, 2010 at 0:21
  • 2
    Why would adding "no" -fno-stack-protector make sure libssp gets linked? Am I misunderstanding something?
    – rogerdpack
    Commented Feb 23, 2018 at 20:11
  • 1
    Indeed, it should be -fstack-protector, unless you're wanting to disable it in the linked static library and rebuild that instead.
    – Zrax
    Commented Feb 5, 2019 at 23:50
  • 1
    Another clean solution sometimes is to check if _FORTIFY_SOURCE is defined anywhere in the source or build files and if it does remove that (or set it to 0). Commented Dec 31, 2020 at 7:39
2

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!

1
  • 1
    Makes sense. Reason is the C++ name mangling (can be seen in the output of nm) which makes it impossible for the linker to find the symbol __stack_chk_fail.
    – jorisv92
    Commented May 26, 2021 at 17:39
1

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
0

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.

1
  • This seem to refer to __stack_chk_fail_local and not __stack_chk_fail which is explained as "Indicates a program was compiled to expect to have the stdlib available, but did not find it at runtime."
    – norq
    Commented Oct 6, 2022 at 14:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.