cmake arm-none-eabi-gcc for stm32 cpp project

2023-05-16

尝试把原有的stm32工程F1canBootloader用cmake来管理,遇到了以下几个坑:

1. 报错,undefined reference to `__dso_handle'

CMakeFiles/app.elf.dir/home/chenxx/engineering/stmproj/F1canBootLoader/iap/uartIapDevice.cpp.obj: 

In function `__static_initialization_and_destruction_0(int, int)':
uartIapDevice.cpp:(.text+0x5dc): undefined reference to `__dso_handle'
/usr/lib/gcc/arm-none-eabi/4.8.2/../../../arm-none-eabi/bin/ld: app.elf: hidden symbol `__dso_handle' isn't defined
/usr/lib/gcc/arm-none-eabi/4.8.2/../../../arm-none-eabi/bin/ld: final link failed: Bad value
collect2: error: ld returned 1 exit status


实际情况是根本没有起名为__dso_handle的函数。后来经查询是gcc在编译静态链接库时,如果声明了全局变量,会报这个错。一般可以通过添加一下编译选项"--specs=nano.specs"(亲测不可用)或“-fno-use-cxa-atexit”来消除,但并不是每一次都有用。推荐在代码里加一句:extern "C" void * __dso_handle = 0;

亲测可用。


2. objcopy: Unable to recognise the format of the input file

调用的是/usr/bin/objcopy。原因是没有在Toochain-arm-none-eabi.cmake中指明用哪个objcopy。
在其中加入:set(CMAKE_OBJCOPY arm-none-eabi-objcopy)
即可

3. undefined reference to `__set_MSP'

CMakeFiles/app.elf.dir/CommonConfig.c.obj: In function `CommonConfig':
CommonConfig.c:(.text+0x6): undefined reference to `__set_PRIMASK'
../iap/libiap.a(iap.c.obj): In function `iap_load_app(unsigned long)':
iap.c:(.text+0x3c): undefined reference to `__set_MSP'
iap.c:(.text+0x42): undefined reference to `__set_PRIMASK'
__set_MSP和 __set_PRIMASK均为core_cm3.h中的函数,经确认core_cm3.h确实被包含,core_cm3.c也定义了,__GNUC__的宏也确实生效,但是仍然无法搜索到这两个函数。无奈只能把这两个函数重新写一遍,但不用再找地方声明(core_cm3.h里声明)问题解决。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

cmake arm-none-eabi-gcc for stm32 cpp project 的相关文章

随机推荐