记一次arm交叉编译的cmake写法

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
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值