最近需要使用gcc编译一个C++的算法库,移植到TI 3352的芯片上,GCC版本为7.3.0,支持C++17特性,编译过程没有问题,但是编译完后进行.o链接生成Bin文件的时候,出现下列报错:
arm-none-eabi-g++ --specs=nosys.specs obj/main.o obj/position-step1.o obj/velocity-step2.o obj/velocity-step1.o obj/position-step2.o obj/brake.o -o bin/main
c:/xmc/160/gcc/bin/../lib/gcc/arm-none-eabi/7.3.1/../../../../arm-none-eabi/lib\libstdc++.a(locale.o): In function `std::locale::_Impl::_M_install_cache(std::locale::facet const*, unsigned int)':
locale.cc:(.text._ZNSt6locale5_Impl16_M_install_cacheEPKNS_5facetEj+0x18): undefined reference to `__sync_synchronize'
c:/xmc/160/gcc/bin/../lib/gcc/arm-none-eabi/7.3.1/../../../../arm-none-eabi/lib\libstdc++.a(cxx11-ios_failure.o): In function `(anonymous namespace)::__io_category_instance()':
cxx11-ios_failure.cc:(.text._ZN12_GLOBAL__N_122__io_category_instanceEv+0xc): undefined reference to `__sync_synchronize'
c:/xmc/160/gcc/bin/../lib/gcc/arm-none-eabi/7.3.1/../../../../arm-none-eabi/lib\libstdc++.a(locale_init.o): In function `(anonymous namespace)::get_locale_mutex()':
locale_init.cc:(.text._ZN12_GLOBAL__N_116get_locale_mutexEv+0xc): undefined reference to `__sync_synchronize'
collect2.exe: error: ld returned 1 exit status
make: *** [makefile:22: bin/main] Error 1
主要问题出在这里:undefined reference to `__sync_synchronize'。
最后网上搜寻资料,找到原因为:arm-noe-babi编译过程需要指定CPU类型,如下
CFLAGS = -g -std=c++17 -Wall -I ${DIR_INC} -Os -flto -ffunction-sections -fdata-sections -mthumb -mabi=aapcs-linux -fstack-usage -mcpu=cortex-a8 -mtune=cortex-a8 -march=armv7-a
CXXFLAGS=$(CFLAGS)
因为我编译的时候指定的了处理器类型 -mcpu=cortex-a8 -mtune=cortex-a8 -march=armv7-a
所以链接的过程也需要指定处理器类型
#目标文件main,依赖/obj下的所有.o文件
${BIN_TARGET}:${OBJ}
$(CC) --specs=nosys.specs -mcpu=cortex-a8 -mtune=cortex-a8 -march=armv7-a $^ -o $@
--specs=nosys.specs要加上,不然会报undefined reference to _exit()的错误。
最后用make命令编译,成功输出BIN文件
arm-none-eabi-g++ --specs=nosys.specs -mcpu=cortex-a8 -mtune=cortex-a8 -march=armv7-a obj/main.o obj/position-step1.o obj/velocity-step2.o obj/velocity-step1.o obj/position-step2.o obj/brake.o -o bin/main
c:/xmc/160/gcc/bin/../lib/gcc/arm-none-eabi/7.3.1/../../../../arm-none-eabi/bin/ld.exe: warning: C:\Users\ADMINI~1\AppData\Local\Temp\ccuah5CG.ltrans0.ltrans.o uses 32-bit enums yet the output is to use variable-size enums; use of enum values across objects may fail
c:/xmc/160/gcc/bin/../lib/gcc/arm-none-eabi/7.3.1/../../../../arm-none-eabi/bin/ld.exe: warning: C:\Users\ADMINI~1\AppData\Local\Temp\ccuah5CG.ltrans1.ltrans.o uses 32-bit enums yet the output is to use variable-size enums; use of enum values across objects may fail