CMake交叉编译工具编写
CMake交叉编译工具编写
- CMake交叉编译工具编写
- 前言
- 一、编写toolchain.cmake
- 二、编写CMakeLists.txt
- 三、编写build.sh
前言
在嵌入式设备应用时,需要在X86架构下编译板卡需要的ARM架构的文件,需要配置交叉编译器等来完成。这时就需要编写.cmake文件来实现配置,在运行cmake命令时添加-DCMAKE_TOOLCHAIN_FILE=.cmake,在build目录下执行cmake -DDCMAKE_TOOLCHAIN_FILE=.cmake …
一、编写toolchain.cmake
#该指令必须存在
#其目的是设置目标机使用的操作系统名称,支持Linux,QNX,WindowsCE,Android等。
#如果没有操作系统,那么就写 Generic。执行该指令后,cmake 变量——CMAKE_CROSSCOMPILING 会自动被设置为 TRUE
#此时 cmake 就会“知道“现在执行的是交叉编译;
SET(CMAKE_SYSTEM_NAME Linux) #设置系统类型#由于 cmake 无法自动获取目标机器信息,因此需要显式指明编译工具
set(CMAKE_C_COMPILER /opt/zlg/m1126-sdk-v1.8.0-ga_2021.08.16/host/bin/arm-linux-gnueabihf-gcc)
set(CMAKE_CXX_COMPILER /opt/zlg/m1126-sdk-v1.8.0-ga_2021.08.16/host/bin/arm-linux-gnueabihf-g++)#嵌入式系统环境镜像根目录,将其加到其他搜索目录之前
SET(CMAKE_FIND_ROOT_PATH /opt/zlg/m1126-sdk-v1.8.0-ga_2021.08.16/host/arm-buildroot-linux-gnueabihf/sysroot)SET(CMAKE_C_FLAGS_INIT "-O3 --sysroot=${CMAKE_FIND_ROOT_PATH}")
SET(CMAKE_CXX_FLAGS_INIT "-O3 --sysroot=${CMAKE_FIND_ROOT_PATH}")#从来不在指定目录下查找工具程序
SET(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
#只在指定目录下查找库文件
#SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY BOTH)
#只在指定目录下查找头文件
#SET(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
SET(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE BOTH)
#只在指定目录下查找包
#SET(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)
#SET(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE BOTH)
二、编写CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
project(get_image_send_recognise)set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_C_FLAGS ${CMAKE_C_FLAGS})
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")set(MQTT_INC ${CMAKE_CURRENT_SOURCE_DIR}/include)
set(JSON_INC ${CMAKE_CURRENT_SOURCE_DIR}/nlohmann)
message(${MQTT_INC})
message(${JSON_INC})FILE(GLOB MQTT_LIB ${CMAKE_CURRENT_SOURCE_DIR}/lib/*.so*)#find_package(OpenCV REQUIRED)
set(OPNECV_INCLUDE ${CMAKE_FIND_ROOT_PATH}/usr/include/opencv ${CMAKE_FIND_ROOT_PATH}/usr/include/opencv2)include_directories(${OPNECV_INCLUDE} ${MQTT_INC} ${JSON_INC})
add_executable(get_image_send_recognise main.cpp base64.cpp)
target_link_libraries(get_image_send_recognise #${OpenCV_LIBS} ${MQTT_LIB}-lavutil -lavformat -lavcodec -lswscale -lz -lm-lopencv_calib3d -lopencv_core # -lopencv_dnn -lopencv_features2d -lopencv_flann -lopencv_highgui -lopencv_imgcodecs -lopencv_imgproc -lopencv_ml -lopencv_objdetect -lopencv_photo -lopencv_shape -lopencv_stitching -lopencv_superres -lopencv_videoio -lopencv_video -lopencv_videostab)
三、编写build.sh
#########################################################################
# File Name: build.sh
# Author: Shey Chen
#########################################################################
#!/bin/bash
basepath=$(cd `dirname $0`; pwd) # current path
dir="./build"[-d "$dir"] && -rf "$dir"
mkdir "$dir"
cd "$dir"cmake -DCMAKE_TOOLCHAIN_FILE=../toolchain.cmake ..
#cmake -DCMAKE_TOOLCHAIN_FILE=../CMake/toolchain.cmake.RV1126 ..
echo "RV1126"
makecd ..