Question

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?

Was it helpful?

Solution

libgurobi_c++.a was compiled with -fno-stack-protector (obviously).

A few things come to mind:

  1. add -fno-stack-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.

OTHER TIPS

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.

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top