cmake交叉编译

来源:互联网 发布:keil模块化编程认识 编辑:程序博客网 时间:2024/06/07 20:47
在许多工程下自带的编译文本是CMakeLists.txt,
对该文件,在Windows下可以使用cmake gui进行编译生成相应库文件或可执行文件
而在linux下可以使用cmake命令生成相应makefile文件,再执行make命令即可生成相应的库文件或者可执行文件
但是,如果你想直接通过这个CMakeLists.txt文件,在linux进行交叉编译,生成arm平台上的makefile,该怎么办?
可以编写相应的文件,来指定相应的交叉编译工具链位置即可,比如toolchain.cmake文件,里面内容如下:
# this one is important
SET(CMAKE_SYSTEM_NAME Linux)
#this one not so much
SET(CMAKE_SYSTEM_VERSION 1)

# specify the cross compiler:指定交叉编译器路径
SET(CMAKE_C_COMPILER   /home/bin/arm-linux-gcc)
SET(CMAKE_CXX_COMPILER /home/bin/arm-linux-g++)

# where is the target environment:指定交叉编译时,系统寻找lib,include,等的根目录 
SET(CMAKE_FIND_ROOT_PATH  /home//arm/arm-linux/target)

# search for programs in the build host directories
SET(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
# for libraries and headers in the target directories
SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY BOTH)
SET(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE BOTH)

在编译时,只需要执行
cmake path -DCMAKE_TOOLCHAIN_FILE=./toolchain.cmake
即可
path是CMakeLists.txt文件所在目录

参考英文网站:
https://cmake.org/Wiki/CMake_Cross_Compiling#Setting_up_the_system_and_toolchain

对应的在CMakeLists.txt文件中需要设置arm下的头文件目录,库文件目录,库文件等宏变量:
include_directories,link_directories,link_libraries和target_link_libraries:
这几个参数的讲解:
http://blog.csdn.net/woyebuzhidao888/article/details/44236999
cmake简单说明:
https://www.mawenbao.com/note/cmake.html

最后会生成一个交叉编译的makfile,这时直接make即可。
原创粉丝点击