CMake是比较容易上手的跨平台编译工具。
基本结构:
1、版本声明
cmake_minimum_required(VERSION 3.2)
2、添加头文件目录,可以多引用,但是不能缺,因为缺了就编译不过(以CMakeLists文件为当前目录计算对应路径)
include_directories(
"../../../myWindows"
"xxx/include"
"../../../include_windows"
)
3、添加环境变量,请结合实际项目要求,不是必须的
add_definitions( -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_REENTRANT -DE NV_UNIX -DBREAK_HANDLER -DUNICODE -D_UNICODE)
还可以添加条件
IF(APPLE)
add_definitions(-DENV_MACOSX)
FIND_LIBRARY(COREFOUNDATION_LIBRARY CoreFoundation )
ENDIF(APPLE)
4、源文件
file(GLOB_RECURSE src_files
"src/main.c"
)
5、设置生成静态库以及名称
add_library(myLibName STATIC ${src_files})
IF(APPLE)
TARGET_LINK_LIBRARIES(myLibName ${COREFOUNDATION_LIBRARY} ${CMAKE_THREAD_ LIBS_INIT})
ELSE(APPLE)
IF(HAVE_PTHREADS)
TARGET_LINK_LIBRARIES(myLibName ${CMAKE_THREAD_LIBS_INIT})
ENDIF(HAVE_PTHREADS)
ENDIF(APPLE)
//
本机编译
直接执行cmake .(代表CMakeLists文件在当前目录,也可以在其他目录,该参数指定到对应的目录即可)
mac: 除了执行上面的指令之外,还可以生成xcode工程,通过工程编译 cmake -GXcode . (注意执行该命令之前不可以执行cmake . 若执行过,还需将上次cmake指令生成的相关文件删除<CMakeCache文件,CMakeFiles文件夹等>)
Linux:若为64位系统生成32位还需安装32位系统的编译工具链,以下还未在Linux平台进行尝试
sudo apt-get install libx32gcc-4.8-dev
sudo apt-get install libc6-dev-i386
sudo apt-get install lib32stdc++6
sudo apt-get install g++-multilib
然后设定cxx_flags为-m32即可:set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -m32")
交叉编译:
iOS
执行如下命令生成xcode工程,直接编译即可:
cmake -DCMAKE_TOOLCHAIN_FILE=iOS_64.cmake -DCMAKE_IOS_DEVELOPER_ROOT=/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/ -DCMAKE_IOS_SDK_ROOT=/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk/ -GXcode .
同时在同目录下(可以不在同目录下,-DCMAKE_TOOLCHAIN_FILE调整成对应的目录文件即可),保存iOS_64.cmake文件即可,该文件内容如下:
# This file is based off of the Platform/Darwin.cmake and Platform/UnixPaths.cmake
# files which are included with CMake 2.8.4
# It has been altered for iOS development
# Options:
#
# IOS_PLATFORM = OS (default) or SIMULATOR
# This decides if SDKS will be selected from the iPhoneOS.platform or iPhoneSimulator.platform folders
# OS - the default, used to build for iPhone and iPad physical devices, which have an arm arch.
# SIMULATOR - used to build for the Simulator platforms, which have an x86 arch.
#
# CMAKE_IOS_DEVELOPER_ROOT = automatic(default) or /path/to/platform/Developer folder
# By default this location is automatcially chosen based on the IOS_PLATFORM value above.
# If set manually, it will override the default location and force the user of a particular Developer Platform
#
# CMAKE_IOS_SDK_ROOT = automatic(default) or /path/to/platform/Developer/SDKs/SDK folder
# By default this location is automatcially chosen based on the CMAKE_IOS_DEVELOPER_ROOT value.
# In this case it will always be the most up-to-date SDK found in the CMAKE_IOS_DEVELOPER_ROOT path.
# If set manually, this will force the use of a specific SDK version
# Standard settings
set (CMAKE_SYSTEM_NAME Darwin)
set (CMAKE_SYSTEM_VERSION 1 )
set (UNIX True)
set (APPLE True)
set (IOS True)
# Force the compilers to gcc for iOS
include (CMakeForceCompiler)
#CMAKE_FORCE_C_COMPILER (gcc gcc)
#CMAKE_FORCE_CXX_COMPILER (g++ g++)
CMAKE_FORCE_C_COMPILER ("/usr/bin/gcc" gcc)
CMAKE_FORCE_CXX_COMPILER ("/usr/bin/g++" g++)
# Skip the platform compiler checks for cross compiling
set (CMAKE_CXX_COMPILER_WORKS TRUE)
set (CMAKE_C_COMPILER_WORKS TRUE)
# All iOS/Darwin specific settings - some may be redundant
set (CMAKE_SHARED_LIBRARY_PREFIX "lib")
set (CMAKE_SHARED_LIBRARY_SUFFIX ".dylib")
set (CMAKE_SHARED_MODULE_PREFIX "lib")
set (CMAKE_SHARED_MODULE_SUFFIX ".so")
set (CMAKE_MODULE_EXISTS 1)
set (CMAKE_DL_LIBS "")
set (CMAKE_C_OSX_COMPATIBILITY_VERSION_FLAG "-compatibility_version ")
set (CMAKE_C_OSX_CURRENT_VERSION_FLAG "-current_version ")
set (CMAKE_CXX_OSX_COMPATIBILITY_VERSION_FLAG "${CMAKE_C_OSX_COMPATIBILITY_VERSION_FLAG}")
set (CMAKE_CXX_OSX_CURRENT_VERSION_FLAG "${CMAKE_C_OSX_CURRENT_VERSION_FLAG}")
# Hidden visibilty is required for cxx on iOS
set (CMAKE_C_FLAGS "")
set (CMAKE_CXX_FLAGS "-headerpad_max_install_names -fvisibility=hidden -fvisibility-inlines-hidden")
set (CMAKE_C_LINK_FLAGS "-Wl,-search_paths_first ${CMAKE_C_LINK_FLAGS}")
set (CMAKE_CXX_LINK_FLAGS "-Wl,-search_paths_first ${CMAKE_CXX_LINK_FLAGS}")
set (CMAKE_PLATFORM_HAS_INSTALLNAME 1)
set (CMAKE_SHARED_LIBRARY_CREATE_C_FLAGS "-dynamiclib -headerpad_max_install_names")
set (CMAKE_SHARED_MODULE_CREATE_C_FLAGS "-bundle -headerpad_max_install_names")
set (CMAKE_SHARED_MODULE_LOADER_C_FLAG "-Wl,-bundle_loader,")
set (CMAKE_SHARED_MODULE_LOADER_CXX_FLAG "-Wl,-bundle_loader,")
set (CMAKE_FIND_LIBRARY_SUFFIXES ".dylib" ".so" ".a")
# hack: if a new cmake (which uses CMAKE_INSTALL_NAME_TOOL) runs on an old build tree
# (where install_name_tool was hardcoded) and where CMAKE_INSTALL_NAME_TOOL isn't in the cache
# and still cmake didn't fail in CMakeFindBinUtils.cmake (because it isn't rerun)
# hardcode CMAKE_INSTALL_NAME_TOOL here to install_name_tool, so it behaves as it did before, Alex
if (NOT DEFINED CMAKE_INSTALL_NAME_TOOL)
find_program(CMAKE_INSTALL_NAME_TOOL install_name_tool)
endif (NOT DEFINED CMAKE_INSTALL_NAME_TOOL)
# Setup iOS platform
if (NOT DEFINED IOS_PLATFORM)
set (IOS_PLATFORM "OS")
endif (NOT DEFINED IOS_PLATFORM)
set (IOS_PLATFORM ${IOS_PLATFORM} CACHE STRING "Type of iOS Platform")
# Check the platform selection and setup for developer root
if (${IOS_PLATFORM} STREQUAL "OS")
set (IOS_PLATFORM_LOCATION "iPhoneOS.platform")
elseif (${IOS_PLATFORM} STREQUAL "SIMULATOR")
set (IOS_PLATFORM_LOCATION "iPhoneSimulator.platform")
else (${IOS_PLATFORM} STREQUAL "OS")
message (FATAL_ERROR "Unsupported IOS_PLATFORM value selected. Please choose OS or SIMULATOR")
endif (${IOS_PLATFORM} STREQUAL "OS")
# Setup iOS developer location
if (NOT DEFINED CMAKE_IOS_DEVELOPER_ROOT)
set (CMAKE_IOS_DEVELOPER_ROOT "/Developer/Platforms/${IOS_PLATFORM_LOCATION}/Developer")
endif (NOT DEFINED CMAKE_IOS_DEVELOPER_ROOT)
set (CMAKE_IOS_DEVELOPER_ROOT ${CMAKE_IOS_DEVELOPER_ROOT} CACHE PATH "Location of iOS Platform")
# Find and use the most recent iOS sdk
if (NOT DEFINED CMAKE_IOS_SDK_ROOT)
file (GLOB _CMAKE_IOS_SDKS "${CMAKE_IOS_DEVELOPER_ROOT}/SDKs/*")
if (_CMAKE_IOS_SDKS)
list (SORT _CMAKE_IOS_SDKS)
list (REVERSE _CMAKE_IOS_SDKS)
list (GET _CMAKE_IOS_SDKS 0 CMAKE_IOS_SDK_ROOT)
else (_CMAKE_IOS_SDKS)
message (FATAL_ERROR "No iOS SDK's found in default seach path ${CMAKE_IOS_DEVELOPER_ROOT}. Manually set CMAKE_IOS_SDK_ROOT or install the iOS SDK.")
endif (_CMAKE_IOS_SDKS)
message (STATUS "Toolchain using default iOS SDK: ${CMAKE_IOS_SDK_ROOT}")
endif (NOT DEFINED CMAKE_IOS_SDK_ROOT)
set (CMAKE_IOS_SDK_ROOT ${CMAKE_IOS_SDK_ROOT} CACHE PATH "Location of the selected iOS SDK")
# Set the sysroot default to the most recent SDK
set (CMAKE_OSX_SYSROOT ${CMAKE_IOS_SDK_ROOT} CACHE PATH "Sysroot used for iOS support")
# set the architecture for iOS - using ARCHS_STANDARD_32_BIT sets armv6,armv7 and appears to be XCode's standard.
# The other value that works is ARCHS_UNIVERSAL_IPHONE_OS but that sets armv7 only
set (CMAKE_OSX_ARCHITECTURES "$(ARCHS_STANDARD_64_BIT)" CACHE string "Build architecture for iOS")
# Set the find root to the iOS developer roots and to user defined paths
set (CMAKE_FIND_ROOT_PATH ${CMAKE_IOS_DEVELOPER_ROOT} ${CMAKE_IOS_SDK_ROOT} ${CMAKE_PREFIX_PATH} CACHE string "iOS find search path root")
# default to searching for frameworks first
set (CMAKE_FIND_FRAMEWORK FIRST)
# set up the default search directories for frameworks
set (CMAKE_SYSTEM_FRAMEWORK_PATH
${CMAKE_IOS_SDK_ROOT}/System/Library/Frameworks
${CMAKE_IOS_SDK_ROOT}/System/Library/PrivateFrameworks
${CMAKE_IOS_SDK_ROOT}/Developer/Library/Frameworks
)
# only search the iOS sdks, not the remainder of the host filesystem
set (CMAKE_FIND_ROOT_PATH_MODE_PROGRAM ONLY)
set (CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set (CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
安卓
安卓编译耗费了比较久的时间,原因就是按照网上的步骤按部就班执行命令总是报一些错误,然后针对报错找对应的文件开始排查,起色并不是很好,比如较流行的是使用toolchain.cmake进行安卓交叉编译,直接执行命令指定toolchain.cmake文件路径执行,网上配备的toolchain.cmake文件放到本地总是执行报错,一次次尝试,最后印象比较深的是:
CMake Error at CMakeLists.txt:5 (project):
The CMAKE_C_COMPILER:
/Users/ouasahikage/code/Polycom/toolchains/llvm/prebuilt/darwin-x86_64/bin/clang
is not a full path to an existing compiler tool.
Tell CMake where to find the compiler by setting either the environment
variable "CC" or the CMake cache entry CMAKE_C_COMPILER to the full path to
the compiler, or to the compiler name if it is in the PATH.
-- Configuring incomplete, errors occurred!
明明在命令行中指定这个变量了,仍然报这个错误,后来发现CMakeLists.txt文件也可以指定变量(原谅我现在才知道这个原理),然后在CMakeList.txt中也设置CMAKE_C_COMPILER这个值,发现还是没有生效,说明还是以toolchain.cmake文件为准。总之饶了很大圈,最后想着不用toolchain.cmake文件(也就是把cmake命令中指定toolchain.cmake文件的项目去掉),试试看吧,就可以了,其实最简单的编译只需指定几个变量,使用toolchain.cmake文件也就是为了几个平台的CMakeLists.txt文件统一,然后使用不同的toolchain.cmake文件。
后续有时间再研究android.toolchain.cmake文件为何有问题吧,如果简单编译安卓平台的库或者可执行文件,仅需在CMakeLists.txt文件中增加几项配置:
# Android 5.0 以上需要在此处设置 PIE
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIE")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fPIE -pie")
set(CMAKE_SYSTEM_NAME AndroidARM)
set(NDK_STANDALONE_TOOLCHAIN /Users/ouasahikage/code/Polycom/mobile/Android/android-toolchain)
#set(CMAKE_C_COMPILER ${NDK_STANDALONE_TOOLCHAIN}/bin/arm-linux-androideabi-gcc)
set(CMAKE_C_COMPILER /Users/ouasahikage/code/Polycom/mobile/Android/android-toolchain/bin/arm-linux-androideabi-gcc)
set(CMAKE_CXX_COMPILER ${NDK_STANDALONE_TOOLCHAIN}/bin/arm-linux-androideabi-g++)
set(CMAKE_FIND_ROOT_PATH ${NDK_STANDALONE_TOOLCHAIN})
步骤:
1、先使用ndk自带的make-standlone-toolchain.sh脚本生成对应平台的toolchain。该脚本位于NDK路径下的build/tools目录下(还需要详细看看)。
$NDK/build/tools/make-standalone-toolchain.sh --toolchain=arm-linux-androideabi-4.9 --arch=arm --platform=android-19 --install-dir=./android-toolchain
2、执行cmake生成Makefile文件:
cmake -DANDROI-DANDROID_ABI="armeabi-v7a" -DANDROID=1 -DANDROID_PLATFORM=android-19 .
若使用toolchain文件,则执行下面的命令:
export PATH=$PATH:./android-toolchain/bin
export ANDROID_STANDALONE_TOOLCHAIN=./android-toolchain (定义此变量是结合android.toolchain.cmake文件使用的,不同的文件需要定义的变量不同,另外android.toolchain.cmake文件在ndk目录build/cmake下,若自带的不好用可以从网上找)
cmake -DCMAKE_TOOLCHAIN_FILE=./android.toolchain.cmake -DANDROID_ABI="armeabi-v7a" -DANDROID=1 -DANDROID_PLATFORM=android-19
3、make
还有其他的配置后续使用过程中再尝试再添加。
ps:如果使用android.toolchain.cmake文件,则在CMakeLists.txt、以及cmake命令中定义的变量(-Dxx=xx)都失效。
报错及解决方法
1、Does not match the generator used previously: Unix Makefiles
清除之前cmake生成的文件
2、CMake can not determine linker language for target
没有找到源文件,导致不清楚链接语言导致的,看下指定源文件的信息是否正确,能否指到对应的文件位置
cmake终于告一段落了,后续使用过程中有需要记录的再回来补充。
参考链接:https://www.cnblogs.com/wengzilin/p/4466708.html
https://my.oschina.net/gotax/blog/516861