0

I'm Trying to compile some c++ files using g++ , the problem that these files depends on both libxml2 and gsl libraries , whene i give the compilation command
g++ -Wall -I/usr/include/libxml2 -lgsl main.cpp YUNucNet.cpp src/*.cpp -lxml2 -lm

it alaways give me many linking error of undefined reference for gsl

'/tmp/ccCJrl0t.o: In function `WnSparseSolve__Phi__solve:
WnSparseSolve.cpp:(.text+0x24bc): undefined reference to `gsl_vector_calloc'
WnSparseSolve.cpp:(.text+0x24cc): undefined reference to `gsl_vector_calloc'
WnSparseSolve.cpp:(.text+0x24e9): undefined reference to `gsl_vector_calloc'
WnSparseSolve.cpp:(.text+0x24ff): undefined reference to `gsl_vector_alloc'
WnSparseSolve.cpp:(.text+0x250f): undefined reference to `gsl_vector_calloc
....etc

what is the problem here ??

M.Talafha
  • 7
  • 6
  • 1
    Possible duplicate of [Why does the order in which libraries are linked sometimes cause errors in GCC?](http://stackoverflow.com/questions/45135/why-does-the-order-in-which-libraries-are-linked-sometimes-cause-errors-in-gcc) – underscore_d Jul 13 '16 at 17:59

1 Answers1

2

The long story short: the library -lgsl must be after your cpps in the command line, i.e.:

g++ -Wall -I/usr/include/libxml2   main.cpp YUNucNet.cpp src/*.cpp -lxml2 -lgsl -lm

You can call g++ with option -v to see what happens under the hood, than you will see, that the linker is called with -lgsl prior to your compiled object files. Thus the -lgsl library will be proceeded by linker prior to the object files and the linker will discard all functions from the library because up to this point it doesn't know they are needed. Only later, by working through your object files it will know, that the functions from the library are indeed needed, but than it is already too late.

ead
  • 32,758
  • 6
  • 90
  • 153