[CMake] problems building static lib

Alexander Neundorf a.neundorf-work at gmx.net
Fri Aug 31 08:38:08 EDT 2007


Hi,

On Friday 31 August 2007 07:30, rhenning at informatik.hu-berlin.de wrote:
> Could pls someone take a look at :
>
> http://forums.gentoo.org/viewtopic-t-579090.html
>
> i keep having problems to build lonely functions into
> a static library, and then link against them
>
> I hope someone here is able to help me, thanks in advance
ADD_LIBRARY 
 (functions 
 class1.hpp class1.cpp 
 class2.hpp class2.cpp 
 class3.hpp class3.cpp 
 )

ADD_EXECUTABLE(../bin/test_data test_data_base.cpp) 

target_link_libraries(../bin/test_data 
             functions 
             math 
             traindata 
             )

Does traindata link against functions ?
If not, you will get a link command like:
g++ ... -o test_data -L ..... -lfunctions -lmath -ltraindata

Now these are static libraries and if traindata uses stuff from functions the 
functions lib has to appear behind it in the link command, otherwise the 
linker won't find the functions (except the same functions have already been 
referenced by any of the code listed before -lfunctions). 

So either add
target_link_libraries(traindata functions)
or change the order of the libraries in the target_link_libraries() command 
for test_data, the lib with the most dependencies first, the lib with the 
least dependencies last, in some cases libs have to ve listed twice. This is 
due to the way the Unix linker works for static libs.

One more note: don't do
add_executable(../bin/test_data ...)
If you want to have the executable created in some other directory, use
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR/bin)  
(I guess that's what you want)

Bye
Alex


More information about the CMake mailing list