0

I'm trying to statically link glibc in order to run my application on an older OS, but when I use the -static flag I get "undefined reference" errors for other libraries I'm using that I don't get without using -static. How do I fix this issue?

My Makefile produces the following commands:

g++ -static -Wall -O3 -w -std=c++11 -I/storage/home/PA/libs -I/storage/home/PA/libs/xerces -fopenmp -c  Utilities.cpp
gcc -static -Wall -O3 -w -std=c++11 -I/storage/home/PA/libs -I/storage/home/PA/libs/xerces -fopenmp -c  ccvt.c
gcc -static -Wall -O3 -w -std=c++11 -I/storage/home/PA/libs -I/storage/home/PA/libs/xerces -fopenmp -c  client.c
g++ -static -Wall -O3 -w -std=c++11 -I/storage/home/PA/libs -I/storage/home/PA/libs/xerces -fopenmp -c  XML_Params.cpp
g++ -static -Wall -O3 -w -std=c++11 -I/storage/home/PA/libs -I/storage/home/PA/libs/xerces -fopenmp -c  main.cpp
g++ -static -Wall -O3 -std=c++11 -L/storage/home/PA/libs/gsl -fopenmp -lgsl -lgslcblas -lm -L/storage/home/PA/libs/xerces -lxerces-c  -o App main.o Utilities.o XML_Params.o ccvt.o client.o 

After the last line I get a huge wall of errors complaining about undefined references to Xerces and gsl functions. However, if I remove the -static from the makefile, everything builds fine. What is the proper way to link these libraries when I'm using -static?

ping
  • 39
  • 5
  • Forgive the obvious, but have you confirmed that the static libraries are installed in addition to the share-object libraries? If I recall correctly, there are a number of packages that only install shared-object libs unless the library archive package is also explicitly installed. What distro are you using? For instance on SuSE, only the share-object versions of libgsl are installed. – David C. Rankin Apr 25 '19 at 23:27
  • The respective lib directories contain both .a and .so files, so I believe that's okay. Using CentOS 7. – ping Apr 25 '19 at 23:32
  • [Why does the order in which libraries are linked sometimes cause errors in GCC?](https://stackoverflow.com/q/45135/608639), [Linking libraries with gcc: order of arguments](https://stackoverflow.com/q/7826448/608639), etc. – jww Apr 26 '19 at 00:55
  • Provide **exact error message** please. Just words "undefined reference" may mean too many reasons to occure. – Tsyvarev Apr 27 '19 at 18:55

1 Answers1

0

according to gcc manual:

 -llibrary
           It makes a difference where in the command you write this option; the
           linker searches and processes libraries and object files in the order
           they are specified.  Thus, foo.o -lz bar.o searches library z after
           file foo.o but before bar.o.  If bar.o refers to functions in z,
           those functions may not be loaded.

Move -lxerces after *.o might solve your problem.

I think you don't need to add -static except for the last line, correct me if i'm wrong.

dedowsdi
  • 209
  • 2
  • 8