38

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?

StephenG
  • 2,851
  • 1
  • 16
  • 36
Akhil
  • 2,269
  • 6
  • 32
  • 39

4 Answers4

45

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.
pevik
  • 4,523
  • 3
  • 33
  • 44
gravitron
  • 3,514
  • 1
  • 20
  • 18
  • 1. I had added -fno-stack-protector earlier but that had not helped. 2. Thanks a lot, adding -lssp worked. 3. Thanks, a lot! This information was useful. I had forgotten this. – Akhil Dec 20 '10 at 19:06
  • 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 Dec 20 '10 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 Dec 22 '10 at 00:21
  • 2
    Why would adding "no" `-fno-stack-protector` make sure libssp gets linked? Am I misunderstanding something? – rogerdpack Feb 23 '18 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 Feb 05 '19 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`). – Brecht Sanders Dec 31 '20 at 07: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
Zioalex
  • 3,441
  • 2
  • 33
  • 30
1

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
    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 May 26 '21 at 17:39
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.

bjc
  • 103
  • 2
  • 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 Oct 06 '22 at 14:31