Reputation: 1467
I have different, precompiled versions of a 3rd-party library (Windows/Linux/Mac, 32/64-bit). It must be included in the project files to keep the requirements for compilation on other systems to a minimum and because I cannot expect it to be available for download years later. Both static and dynamic versions are available but no source.
How should I link it in my CMakeLists.txt
so that the dependent main.cpp
compiles on all systems? Would it work on different Linux distributions?
CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
PROJECT(ExampleProject)
LINK_DIRECTORIES(${CMAKE_SOURCE_DIR}/libs)
ADD_EXECUTABLE(Test main.cpp)
TARGET_LINK_LIBRARIES(Test lib1_win32)
This works under Windows but obviously does not account for different operating systems and architectures. I know the alternatives to LINK_DIRECTORIES
, this is just an example.
Upvotes: 1
Views: 2707
Reputation: 10971
Use CMAKE_SYSTEM_NAME to test the operating system and CMAKE_SIZEOF_VOID_P to test wether it's 32 or 64 bits:
if (${CMAKE_SYSTEM_NAME} MATCHES "Linux")
if (${CMAKE_SIZEOF_VOID_P} MATCHES "8")
target_link_libraries(Test lib1_linux64)
else()
target_link_libraries(Test lib1_linux32)
endif()
elseif (${CMAKE_SYSTEM_NAME} MATCHES "Windows")
if (${CMAKE_SIZEOF_VOID_P} MATCHES "8")
target_link_libraries(Test lib1_win64)
else()
target_link_libraries(Test lib1_win32)
endif()
# ETC
endif()
Btw, my example is for CMake 2.8, you'll have to adapt the tests for 2.6.
Upvotes: 7