2016-07-13 25 views
0

我试图用G ++,这些文件同时依赖于libxml2和GSL库的问题,whene我给编译命令
g++ -Wall -I/usr/include/libxml2 -lgsl main.cpp YUNucNet.cpp src/*.cpp -lxml2 -lm未定义的参考`gsl_vector_free”

它alaways给编译一些C++文件我很多链接错误的未定义参考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 

这里有什么问题??

+1

可能的重复[为什么库的链接顺序有时会导致GCC错误?](http://stackoverflow.com/questions/45135/why-does-the-order-in-which-libraries-正在连接 - 有时因 - 错误 - 在-GCC) –

回答

1

长话短说:库-lgsl必须在命令行的CPPS之后,即:

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

你可以调用G ++与选项-v看到引擎盖下会发生什么,比你会看到,在编译目标文件之前,链接器被调用-lgsl。因此,在目标文件之前链接器将继续执行库,并且链接器将丢弃库中的所有函数,因为直到现在它不知道它们是需要的。之后,通过处理目标文件,它会知道库中的函数确实是需要的,但是已经太晚了。