使用交叉编译链工具编译C++的cmake配置

1. 设置CMakeLists.txt

假设arm的编译器位置在/home/softwares/gcc-ubuntu-9.3.0-2020.03-x86_64-aarch64-linux-gnu/下。

cmake_minimum_required(VERSION 3.22)

#set(CMAKE_SYSTEM_NAME Linux) # 可以不用设置
#set(CMAKE_SYSTEM_PROCESSOR arm) # 可以不用设置
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 /home/softwares/gcc-ubuntu-9.3.0-2020.03-x86_64-aarch64-linux-gnu/)

set(CMAKE_CXX_COMPILER "${CMAKE_FIND_ROOT_PATH}/bin/aarch64-linux-gnu-g++") # 设置C++编译器,必须

set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_STANDARD 17)


project(asan_test)
aux_source_directory(. CPP_FILES)

# 设置asan编译链接,注意,后面一定要加上 -lpthread -ldl -lrt,不然在链接的时候会报libasan.so需要这些库
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address -fsanitize=leak -fsanitize-recover=all -fno-optimize-sibling-calls -lpthread -ldl -lrt -g")

add_executable(zhjs ${CPP_FILES})

# 添加REQUIRED,如果找不到,则会直接报错退出
find_library(LIB_ASAN NAMES libasan.so PATHS ${CMAKE_FIND_ROOT_PATH}/lib/gcc-cross/aarch64-linux-gnu/9/ REQUIRED)
target_link_libraries(zhjs ${LIB_ASAN})

2. 设置编译脚本

交叉编译链工具需要设置库目录,否则,在编译的时候会提示找不到某些库,而这些库是gcc或者g++需要依赖的动态库:

#!/bin/bash

rm -rf build/
mkdir build
cd build

# 只有设置了这个path,执行交叉编译器g++才能执行成功
export LD_LIBRARY_PATH=/home/softwares/gcc-ubuntu-9.3.0-2020.03-x86_64-aarch64-linux-gnu/lib/x86_64-linux-gnu:${LD_LIBRARY_PATH}
cmake .. && make

你可能感兴趣的:(C/C++,工具,c++,交叉编译,arm,库依赖)