问题描述
我有一个第三方库的不同预编译版本(Windows / Linux / Mac,32/64位)。它必须包含在项目文件中,以使在其他系统上进行编译的要求降至最低,并且由于我无法期望它可以在多年后下载。静态和动态版本均可用,但没有源。
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.
如何在我的 CMakeLists.txt
中链接它 main.cpp
依赖的文件在所有系统上编译?
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)
这可以在Windows下使用,但显然不能说明不同的操作系统和体系结构。我知道 LINK_DIRECTORIES
的替代方法,这只是一个例子。
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.
推荐答案
使用CMAKE_SYSTEM_NAME测试操作系统,并使用CMAKE_SIZEOF_VOID_P测试32位还是64位:
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()
顺便说一句,我的示例是针对CMake 2.8,您必须将测试改编为2.6。
Btw, my example is for CMake 2.8, you'll have to adapt the tests for 2.6.
这篇关于CMake:根据操作系统和体系结构链接预编译的库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!