1.背景
用cmake交叉编译高通4G模组Linux应用代码;
2.问题
之前都是参考网上各种所谓的cmake交叉编译写法,例如某乎上config.cmake是这样写的:
set(CMAKE_SYSTEM_NAME Linux)
set(TOOLCHAIN_PATH /OPT/gcc-arm-linux-gnueabi)
set(CMAKE_C_COMPILER ${tools}/bin/arm-linux-gnueabi-gcc)
set(CMAKE_CXX_COMPILER ${tools}/bin/arm-linux-gnueabi-g++)
实际根本不行,各种报错,提示找不到
The C compiler xxx is not able to compile a simple test program.
error: cannot open crt1.o: No such file or directory
error: cannot open crti.o: No such file or directory
error: cannot open crtbegin.o: No such file or directory
error: cannot open crtend.o: No such file or directory
error: cannot open crtn.o: No such file or directory
error: cannot find -lgcc
error: cannot find -lgcc_s
error: cannot find -lc
error: cannot find -lgcc
error: cannot find -lgcc_s
3.解决办法
分析几天找到原因,还需要配置CMAKE_SYSROOT,实际配置如下:
set(CMAKE_SYSTEM_NAME Linux)
SET(CMAKE_SYSTEM_PROCESSOR armhf) #arm 32位
# set tool chain path
set(TOOLCHAIN_PATH /opt/ql_crosstools/ql-gcc640-toolchain/gcc/usr/bin/arm-oe-linux-gnueabi)
set(CMAKE_C_COMPILER ${TOOLCHAIN_PATH}/arm-oe-linux-gnueabi-gcc)
set(CMAKE_CXX_COMPILER ${TOOLCHAIN_PATH}/arm-oe-linux-gnueabi-g++)
add_compile_options("-marm")
add_compile_options("-march=armv7-a")
add_compile_options("-mfpu=neon")
add_compile_options("-mfloat-abi=hard")
set(SDK_DIR /home/xxx/ag35/ql-ol-extsdk)
SET(ENV{LDFLAGS} "$ENV{LDFLAGS} -marm -march=armv7-a -mfpu=neon -mfloat-abi=hard -s -no-pie")
set(CMAKE_SYSROOT /home/xxx/ag35/ql-ol-extsdk/ql-sysroots)
4.写CMakeLists.txt
cmake_minimum_required(VERSION 3.3)
project(Demo VERSION 1.0)
add_executable(Demo test.c)
5.编译
$ mkdir build
$ cd build
$ cmake -DCMAKE_TOOLCHAIN_FILE=../config.cmake ..
$ make