Code Yarns ‍👨‍💻
Tech BlogPersonal Blog

Undefined reference error with sincosf and sqrtf

📅 2014-May-02 ⬩ ✍️ Ashwin Nanjappa ⬩ 🏷️ error, linker ⬩ 📚 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 'sincosf@@GLIBC_2.2.5'
//lib/x86_64-linux-gnu/libm.so.6: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status

Another error of the same form:

/usr/bin/ld: foobar.cpp.o: undefined reference to symbol 'sqrtf@@GLIBC_2.2.5'
//lib/x86_64-linux-gnu/libm.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 mathematical symbol: sincosf. Suggesting the linker to link with the math library fixed this error. You can do this by adding -lm to the linker invocation.

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

target_link_libraries(
    foobar
    -lsomelibrary
    -lm
    )

Tried with: GCC 4.8.2, CMake 2.8.12.2 and Ubuntu 14.04


© 2023 Ashwin Nanjappa • All writing under CC BY-SA license • 🐘 Mastodon📧 Email