Undefined reference to, despite correct link order

2019-08-25 03:44发布

问题:

I am having trouble linking against an external library, MY_LIB.a.

The following is invoked from the makefile created by mbed.org compiler, and with the addition of my link to MY_LIB.a path.

arm-none-eabi-gcc -mcpu=cortex-m3 -mthumb -Wl,--gc-sections --specs=nano.specs 
-u _printf_float -u _scanf_float -T./mbed/TARGET_LPC1768/TOOLCHAIN_GCC_ARM/LPC1768.ld 
-L./mbed/TARGET_LPC1768/TOOLCHAIN_GCC_ARM -o MyProg.elf main.o mbed/TARGET_LPC1768
/TOOLCHAIN_GCC_ARM/cmsis_nvic.o mbed/TARGET_LPC1768/TOOLCHAIN_GCC_ARM/system_LPC17xx.o
mbed/TARGET_LPC1768/TOOLCHAIN_GCC_ARM/startup_LPC17xx.o mbed/TARGET_LPC1768
/TOOLCHAIN_GCC_ARM/board.o mbed/TARGET_LPC1768/TOOLCHAIN_GCC_ARM/retarget.o  
-lstdc++ -lsupc++ -lm -lc -lgcc -lnosys  -lstdc++ -lsupc++ -lm -lc -lgcc -lnosys 
-L/usr/local/lib/MY_LIB.a
main.o: In function `main':

And then I get undefined reference to <function> errors for every function defined in MY_LIB.

My understanding was that MY_LIB.a just needed to be linked after main.o which uses it.

I also tried linking (immediately) after -o MyProg.elf main.o, but then I get could not read symbols: File in wrong format.

What is the correct way to link this library? Does it need to be linked in the previous step to be listed here in.. the correct format? How is that done?

回答1:

-L only specifies the search path for libraries. You could remove it from your parameter that is including MY_LIB.a, e.g.:

arm-none-eabi-gcc -mcpu=cortex-m3 -mthumb -Wl,--gc-sections --specs=nano.specs 
-u _printf_float -u _scanf_float -T./mbed/TARGET_LPC1768/TOOLCHAIN_GCC_ARM/LPC1768.ld 
-L./mbed/TARGET_LPC1768/TOOLCHAIN_GCC_ARM -o MyProg.elf main.o mbed/TARGET_LPC1768
/TOOLCHAIN_GCC_ARM/cmsis_nvic.o mbed/TARGET_LPC1768/TOOLCHAIN_GCC_ARM/system_LPC17xx.o
mbed/TARGET_LPC1768/TOOLCHAIN_GCC_ARM/startup_LPC17xx.o mbed/TARGET_LPC1768
/TOOLCHAIN_GCC_ARM/board.o mbed/TARGET_LPC1768/TOOLCHAIN_GCC_ARM/retarget.o  
-lstdc++ -lsupc++ -lm -lc -lgcc -lnosys  -lstdc++ -lsupc++ -lm -lc -lgcc -lnosys 
/usr/local/lib/MY_LIB.a


回答2:

From your discussion with adpeace I gather the library is targeting i386 whilst the the build commands you are using show clearly you are targeting an arm platform.

You will not have any luck using this particular library file in that case and would need to find an arm version of it.