CMake:根据操作系统和架构链接预编译库 [英] CMake: Link precompiled library depending on OS and architecture
问题描述
我有不同的第 3 方库(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
在所有系统上编译?它可以在不同的 Linux 发行版上运行吗?
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:根据操作系统和架构链接预编译库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!