Code Yarns ‍👨‍💻
Tech BlogPersonal Blog

Undefined reference error with cxa_free_exception

📅 2014-May-02 ⬩ ✍️ Ashwin Nanjappa ⬩ 📚 Archive

Problem

I was compiling some C++ and CUDA code using CMake on Ubuntu 14.04. The linking stage threw up this error:

/usr/bin/ld: foobar.cpp.o: undefined reference to symbol '__cxa_free_exception@@CXXABI_1.3'
//usr/lib/x86_64-linux-gnu/libstdc++.so.6: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status

I had used these same source and CMake files without any compile error on a Ubuntu 12.04 system.

Solution

Ubuntu 14.04 uses newer versions of the GCC compilers and libraries. The hint here is that its complaining about a core C++ symbol: __cxa_free_exception. Suggesting the linker to link with the standard C++ library fixed this error. You can do this by adding -lstdc++ to the linker invocation.

For my CMake, I did this by adding -lstdc++ to the linking options to the target as:

target_link_libraries(
    foobar
    -lsomelibrary
    -lstdc++
    )

Tried with: GCC 4.8.2, CMake 2.8.12.2 and Ubuntu 14.04