[CMake] Creating a library

Bill Hoffman bill.hoffman at kitware.com
Wed Feb 18 12:46:58 EST 2009


David Doria wrote:
> If I have many executables in the same project (ie same CMakeLists.txt
> file), it seems like I shouldn't have to do this:
> 
> set(Sources File1.cpp File1.cpp )
> 
> ADD_EXECUTABLE(Test1 Test1.cpp ${Sources})
> 
> ADD_EXECUTABLE(Test2 Test2.cpp ${Sources})
> 
> because it is compiling File1 and File2 twice when that is unnecessary.
> 
> So I tried to do this:
> 
> ADD_LIBRARY(MyLibs ${Sources})
> 
> ADD_EXECUTABLE(Test1 Test1.cpp)
> TARGET_LINK_LIBRARIES(Test1 ${MyLibs})
> 
> ADD_EXECUTABLE(Test2 Test2.cpp)
> TARGET_LINK_LIBRARIES(Test2 ${MyLibs})
> 
> But I get a whole bunch of linker errors - "Undefined reference to ..."
> 
> Have I done something wrong / how would I do this?
> 
This should fix your problem:

TARGET_LINK_LIBRARIES(Test2 MyLibs)

-Bill


More information about the CMake mailing list