Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
978 views
in Technique[技术] by (71.8m points)

linker - CMake: Link precompiled library depending on OS and architecture

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...