Reputation: 1817
I have a multi-target CMake file where all the targets are building fine on OS X, but when I build the target on Ubuntu Linux, it does not link the system libraries added in target_include_directories.
Here's an example of a target (each target is similar).
set(SOURCE_FILES_TARGET)
set(RELEASE_LIBS iconv m xml2 z icucore resolv curl )
add_executable(TARGET_NAME ${SOURCE_FILES_TARGET)
target_include_directories(TARGET_NAME PRIVATE external_lib1/headers external_lib2/headers )
target_link_libraries(TARGET_NAME ${RELEASE_LIBS})
target_link_libraries(TARGET_NAME ${CMAKE_SOURCE_DIR}/lib1.a)
target_link_libraries(TARGET_NAME ${CMAKE_SOURCE_DIR}/lib2.a)
The libraries are a little different for linux (comparing the .la files), but even one which is the same (xml2) results in undefined reference errors. Also, on OS X the "target_link_libraries" command works as listed above, and with -lxml2 format.
I tried to add the "pthreads" library on linux (as it asked for this, but not on OS X), and the only way I was able to add it was
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -pthread")
however, this is a global setting for all targets. Basically, if I put pthread into target_link_libraries it is ignored, just like the rest of them.
How should I best add these libraries to the targets? Maybe I'm missing something on the OS X side and it just happens to work?
Upvotes: 1
Views: 798
Reputation: 5346
Not sure what your problem exactly is, but here some tips that may help you.
Instead of setting the library names and tell you program to compile against them, you should rather try to find the libraries using find_library()
and link your program against the variable resulting from the CMake command. That way, CMake may actually tell you if the library is not found.
For example:
find_library(XML2_LIBRARY xml2)
add_executable(TARGET_NAME ${XML2_LIBRARY})
That way, you can provide several possible library names (for example the names may be different depending on the system you are compiling on). Also, you can tell CMake to look into additional directories (eg. CMAKE_LIBRARY_PATH
) without changing your CMakeLists.txt
.
You should also check for CMake modules that provide configuration for the dependencies you are interested in (CMake 3.7 has a FindLibXml2
module).
For pthread
, you should check CMake's FindThreads
.
Upvotes: 1