CMake交叉编译

文章目录

    • 文件目录
    • CMakeLists.txt
    • 交叉编译环境配置

文件目录

├── CMakeLists.txt
│   ├── cmake
│   │   ├── modules               
│   │   └── toolschain
│   │       └── Tiny4412.cmake    

CMakeLists.txt

在顶层CMakeLists.txt中添加以下命令:

# 添加判断条件,在编译时使用如下命令才会进行交叉编译,否则调用本机编译程序,如果有多个目标平台添加相关判断即可
# cmake -DCMAKE_BUILD_TARGET=Tiny4412 ..
# cmake -DCMAKE_TOOLCHAIN_FILE=./cmake/Tiny4412.cmake
set(CODE_ROOT_PATH ${CMAKE_SOURCE_DIR})
if(CMAKE_BUILD_TARGET STREQUAL Tiny4412)
    # 设置toolchain文件路径
    set(CMAKE_TOOLCHAIN_FILE 
        ${CMAKE_SOURCE_DIR}/cmake/toolschain/Tiny4412.cmake)
endif()

交叉编译环境配置

Tiny4412.cmake内容如下:

# 设置目标系统
set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_SYSTEM_PROCESSOR arm)

# 设置工具链目录
set(TOOL_CHAIN_DIR /usr/local/arm/4.4.3)
set(TOOL_CHAIN_INCLUDE 
    ${TOOL_CHAIN_DIR}/arm-none-linux-gnueabi/sys-root/usr/include
    ${TOOL_CHAIN_DIR}/arm-none-linux-gnueabi/include
)
set(TOOL_CHAIN_LIB 
    ${TOOL_CHAIN_DIR}/arm-none-linux-gnueabi/sys-root/usr/lib
    ${TOOL_CHAIN_DIR}/arm-none-linux-gnueabi/lib
)

# 设置编译器位置
set(CMAKE_C_COMPILER "arm-linux-gcc")
set(CMAKE_CXX_COMPILER "arm-linux-g++")

# 设置Cmake查找主路径
set(CMAKE_FIND_ROOT_PATH ${TOOL_CHAIN_DIR}/arm-none-linux-gnueabi)

set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
# 只在指定目录下查找库文件
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
# 只在指定目录下查找头文件
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
# 只在指定目录下查找依赖包
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)

include_directories(
    ${TOOL_CHAIN_DIR}/arm-none-linux-gnueabi/include
    ${TOOL_CHAIN_DIR}/arm-none-linux-gnueabi/sys-root/usr/include
)

set(CMAKE_INCLUDE_PATH 
    ${TOOL_CHAIN_INCLUDE}
)

set(CMAKE_LIBRARY_PATH 
    ${TOOL_CHAIN_LIB}
)

你可能感兴趣的:(Linux)