0

I have a situation where I need to compile C code and C++ code aswell as linking to a C++ static libary.

This works by using the following command:

gcc  file.cpp file.c -xc++ -pthread -std=c++11 -lstdc++ -shared-libgcc -L/path -l:liba.a -lm -o program

However, if I change the order of link commands (switching -lm and -l:libary.a) as follows:

gcc  file.cpp file.c -xc++ -pthread -std=c++11 -lstdc++ -shared-libgcc -lm -L/path -l:liba.a -o program

This does not work as the compiler complains:

liba.o: undefined reference to symbol 'log@@GLIBC_2.2.5'
/lib/x86_64-linux-gnu/libm.so.6: error adding symbols: DSO missing from command line

Why this happens?

jww
  • 97,681
  • 90
  • 411
  • 885
SFD
  • 565
  • 7
  • 18
  • 7
    Use `g++` instead of `gcc` for compiling c++. – pSoLT Jan 19 '17 at 12:27
  • 2
    linking happens from left to right, you need to move `-lm` *after* `-l:liba.a` because `liba.a` declares a need for something from `libm` (namely log) – Anya Shenanigans Jan 19 '17 at 14:45
  • 3
    Possible duplicate of [Why does the order in which libraries are linked sometimes cause errors in GCC?](https://stackoverflow.com/questions/45135/why-does-the-order-in-which-libraries-are-linked-sometimes-cause-errors-in-gcc) – Mike Kinghan Dec 09 '18 at 07:58

0 Answers0