Getting this error while compiling C++ code:
undefined reference to `__stack_chk_fail'
Options already tried:
added -fno-stack-protector while compiling - did not work, error persists
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?
libgurobi_c++.a was compiled with -fno-stack-protector (obviously).
A few things come to mind:
add -fstack-protector when linking. This will make sure that libssp gets linked.
Manually link -lssp
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.
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
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!
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.
Related
I have library which I have compiled on Ubuntu 18.04 using the version of gcc which comes bundled in the build-essential package, gcc 7.5.0.
If I then try to compile an application on Ubuntu 20.04 and link against the that library I previous built using the default gcc version, gcc 9.3.0, then I get a bunch of errors such as undefined reference to __expf_finite. Note, I do compile my original library with -Ofast as I need to take advantage of all optimizations. According to this SO question, it suggests that math-finite.h is no longer supported by the libc update.
So as a possible solution, I tried compiling my library on Ubuntu 20.04 using gcc 9.3.0 to then see if I can link against that library using an application compiled with gcc 7.5.0 on Ubuntu 18.04 (basically the reverse of the first scenario).
However, when I do this, I get ABI compat errors with std::string:
./trueface_sdk/libtf.a(ocl.cpp.o): In function `cv::ocl::Kernel::set(int, cv::ocl::KernelArg const&)':
ocl.cpp:(.text._ZN2cv3ocl6Kernel3setEiRKNS0_9KernelArgE+0x375): undefined reference to `std::__cxx11::basic_stringstream<char, std::char_traits<char>, std::allocator<char> >::basic_stringstream()'
ocl.cpp:(.text._ZN2cv3ocl6Kernel3setEiRKNS0_9KernelArgE+0x49d): undefined reference to `std::__cxx11::basic_stringstream<char, std::char_traits<char>, std::allocator<char> >::basic_stringstream()'
../trueface_sdk/libtf.a(ocl.cpp.o): In function `oclCleanupCallback.cold':
ocl.cpp:(.text.unlikely.oclCleanupCallback+0x36): undefined reference to `std::__cxx11::basic_stringstream<char, std::char_traits<char>, std::allocator<char> >::basic_stringstream()'
ocl.cpp:(.text.unlikely.oclCleanupCallback+0xcf): undefined reference to `std::__cxx11::basic_stringstream<char, std::char_traits<char>, std::allocator<char> >::basic_stringstream()'
ocl.cpp:(.text.unlikely.oclCleanupCallback+0x184): undefined reference to `std::__cxx11::basic_stringstream<char, std::char_traits<char>, std::allocator<char> >::basic_stringstream()'
../trueface_sdk/libtf.a(ocl.cpp.o):ocl.cpp:(.text._ZN2cv3ocl6Device4ImplC2EPv[_ZN2cv3ocl6Device4ImplC5EPv]+0xb7b): more undefined references to `std::__cxx11::basic_stringstream<char, std::char_traits<char>, std::allocator<char> >::basic_stringstream()' follow
../trueface_sdk/libtf.a(sqlite3.c.o):(.data.rel+0xb0): undefined reference to `fcntl64'
I also get undefined reference to fcntl64.
So what is the solution? How can I compile a library that will be compatible with multiple version of gcc?
Based on the comments, it looks like it is not compatible because of the use of the -Ofast compiler optimization flag.
I generated RTI code using:
/opt/rti_connext_dds-6.0.0/bin/rtiddsgen -d GENERATED_CODE_DIR -language C++11 -replace IDL_DIR
My CMake file:
link_libraries(gsl gslcblas nnz12 occi clntsh dl nddsc nddscpp2 pthread)
add_definitions(-DRTI -DRTI_64BIT -DRTI_LINUX -DRTI_UNIX -D_GLIBCXX_DEBUG)
include_directories(-isystem /opt/rti_connext_dds-6.0.0/include/ndds)
include_directories(-isystem /opt/rti_connext_dds-6.0.0/include/ndds/hpp)
include_directories(-isystem /opt/rti_connext_dds-6.0.0/include/ndds/hpp/rti)
include_directories(-isystem /opt/rti_connext_dds-6.0.0/include/ndds/hpp/dds)
Now when compiling, I am getting following errors:
undefined reference to `rti::domain::register_type_plugin(dds::domain::TDomainParticipant<rti::domain::DomainParticipantImpl>&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, PRESTypePlugin* (*)(), void (*)(PRESTypePlugin*))'
undefined reference to `dds::core::PreconditionNotMetError::PreconditionNotMetError(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
collect2: error: ld returned 1 exit status
Is there anything I am missing ?
The problem was due to ABI. I am using gcc10.2.
I solved the problem by adding definition:
_GLIBCXX_USE_CXX11_ABI 0
The problem was due to the difference in ABIs of RTI libraries and the occi.so.
Solved the problem by using occi_gcc53.so instead of occi.so library.
I'm using ubuntu 18.
I'v installed codeblocks ide and bare gnu gcc compiler (command line) both
gcc version 7.4.0 (Ubuntu 7.4.0-1ubuntu1~18.04.1)
code blocks 16.01
I wrote a very simple code to create and print a string variable
Code blocks(with inbuilt gnu gcc compiler) succeeded in compiling. But bare gcc compiler failes
I've tried by including iostream and stdio also
#include <string>
using namespace std;
int main() {
string a = "I am learning C++";
printf("%s",a.c_str());
return 0;
}
codeblocks compiles it successfully and gives the output without raising an error. As well as sololearn online compiler also
I am learning C++
I got into the folder where the cpp file is and gave following command
gcc string_var.cpp -o string_var.out
Bt the bare gcc compiler gives some long stuff of compiler errors which i can't understand
/tmp/ccen8bfp.o: In function `main':
string_var.cpp:(.text+0x20): undefined reference to `std::allocator<char>::allocator()'
string_var.cpp:(.text+0x37): undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(char const*, std::allocator<char> const&)'
string_var.cpp:(.text+0x43): undefined reference to `std::allocator<char>::~allocator()'
string_var.cpp:(.text+0x4f): undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::c_str() const'
string_var.cpp:(.text+0x74): undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()'
string_var.cpp:(.text+0x96): undefined reference to `std::allocator<char>::~allocator()'
string_var.cpp:(.text+0xb0): undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()'
/tmp/ccen8bfp.o:(.data.rel.local.DW.ref.__gxx_personality_v0[DW.ref.__gxx_personality_v0]+0x0): undefined reference to `__gxx_personality_v0'
collect2: error: ld returned 1 exit status
The GNU Compiler Collection (GCC) can be used to compile many different languages. Some of those languages are common enough that they can use the same front-end program.
C and C++ are two such languages, and both can be compiled with the gcc front-end program. However, the gcc program by default is for C code, and because of that don't use some flags needed to build C++ programs. One such missing thing is that gcc doesn't link with the C++ standard library.
You can solve this by explicitly linking with the library:
gcc string_var.cpp -o string_var.out -lstdc++
Or, even simpler, use the front-end program for C++, g++:
g++ string_var.cpp -o string_var.out
gcc string_var.cpp -o string_var.out
gcc is a C compiler. You want to compile C++ code:
g++ string_var.cpp -o string_var.out
It just so happens that either gcc or g++ works insofar as compiling the actual code, the compiler understand that .cpp files contain C++ code, and proceeds accordingly.
However, when it times to linking the resulting object module, gcc does not provide the runtime C++ library to the linker (it still thinks it's just a C compiler, and has never heard of C++), resulting in your link failure.
I am trying to compile a program using cmake, and am seeing the following linker error:
/home/quant/bin/boost_1_61_0/stage/lib/libboost_log_setup.so:
undefined reference to
boost::filesystem::path_traits::convert(wchar_t const*, wchar_t
const*, std::__cxx11::basic_string<char, std::char_traits<char>,
std::allocator<char> >&, std::codecvt<wchar_t, char, __mbstate_t>
const&)' /home/quant/bin/boost_1_61_0/stage/lib/libboost_log.so:
undefined reference to
boost::filesystem::path_traits::dispatch(boost::filesystem::directory_entry
const&, std::__cxx11::basic_string,
std::allocator >&)'
The linker command that ninja generated looks like this:
g++ -pthread -DBOOST_ALL_DYN_LINK
utility/test/CMakeFiles/utilityTest.dir/loadCSVTests.cpp.o
utility/test/CMakeFiles/utilityTest.dir/main.cpp.o
utility/test/CMakeFiles/utilityTest.dir/randomDeviceTests.cpp.o -o
utility/test/utilityTest -rdynamic
/home/quant/bin/boost_1_61_0/stage/lib/libboost_thread.so
/home/quant/bin/boost_1_61_0/stage/lib/libboost_program_options.so
/home/quant/bin/boost_1_61_0/stage/lib/libboost_serialization.so
/home/quant/bin/boost_1_61_0/stage/lib/libboost_unit_test_framework.so
/home/quant/bin/boost_1_61_0/stage/lib/libboost_system.so
/home/quant/bin/boost_1_61_0/stage/lib/libboost_log.so
/home/quant/bin/boost_1_61_0/stage/lib/libboost_log_setup.so
/home/quant/bin/boost_1_61_0/stage/lib/libboost_filesystem.so
utility/lib/libutilityLib.a utility/testLib/libutilityTestLib.a
utility/lib/libutilityLib.a
/home/quant/bin/boost_1_61_0/stage/lib/libboost_thread.so
/home/quant/bin/boost_1_61_0/stage/lib/libboost_program_options.so
/home/quant/bin/boost_1_61_0/stage/lib/libboost_serialization.so
/home/quant/bin/boost_1_61_0/stage/lib/libboost_unit_test_framework.so
/home/quant/bin/boost_1_61_0/stage/lib/libboost_system.so
/home/quant/bin/boost_1_61_0/stage/lib/libboost_log.so
/home/quant/bin/boost_1_61_0/stage/lib/libboost_log_setup.so
/home/quant/bin/boost_1_61_0/stage/lib/libboost_filesystem.so
-Wl,-rpath,/home/quant/bin/boost_1_61_0/stage/lib
As you can see, I am linking against boost_filesystem and boost_system, so it's not the same problem as referenced on this SO post (and the many others like it).
I am using boost 1.61, which I compiled with gcc 5.3 (the same compiler as the one I'm compiling my program with).
What am I doing wrong?
I had a similar issue, this could be because of a new ABI which is introduced from gcc 5.1.
https://github.com/openframeworks/openFrameworks/issues/4203
I fixed mine by adding "add_definitions(-D_GLIBCXX_USE_CXX11_ABI=0)" to CMakeLists.txt
I am using boost 1.45.0 on Ubuntu with Code::Blocks as my IDE, and I can't get basic_regex.hpp to compile. I'm pretty sure I set up boost correctly, because I can compile programs using boost::format without any errors. But I'm getting this annoying error, and I don't know how to get rid of it.
The code that is provoking the error:
boost::regex e("\"http:\\\\/\\\\/localhostr.com\\\\/files\\\\/.+?\"");
Compiler output (GCC):
obj/Debug/main.o
In function `boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::assign(char const*, char const*, unsigned int)'
/home/neal/Documents/boost_1_45_0/boost/regex/v4/basic_regex.hpp|379|
undefined reference to `boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::do_assign(char const*, char const*, unsigned int)'|
||=== Build finished: 1 errors, 0 warnings ===|
Did I miss a step when setting up boost, or should I downgrade to another version of boost?
This looks like a linker error. boost::regex is not a header only library so you need to pass -lboost_regex with correct -L/path/to/boost/lib to linker.
Boost::Regex has some code that lives in a separate library (libboost_regex.so). To link against it, add -lboost_regex to the GCC commandline you're using.
Depending on your install, that might be libboost_regex-mt.so. In that case, you'll need to use -lboost_regex-mt on your command line. (The MT stands for mutlithreaded.)
That's a linking error rather than a compiler error. You need to explicitly link against Boost's regex library.
g++ program.cpp -lboost_regex -L/path/to/boost/lib