2016-10-04 65 views
0

我正在使用ARM-gcc交叉编译器,并且遇到链接到libc内的任何实现的问题。未定义对任何libc符号的引用,尽管与档案直接链接

Compiling prog.c, writing to prog.o... 
~/.programs/arm-gcc/bin/arm-none-eabi-gcc -Wall -fno-common -mcpu=cortex-m4 -mthumb -O0 -g -DF_CPU=72000000 -I~/.programs/arm-gcc/arm-none-eabi/include -I../include -I. -I../src/include -c prog.c -o prog.o > prog.lst 

Compiling ../common/sysinit.c, writing to sysinit.o... 
~/.programs/arm-gcc/bin/arm-none-eabi-gcc -Wall -fno-common -mcpu=cortex-m4 -mthumb -O0 -g -DF_CPU=72000000 -I~/.programs/arm-gcc/arm-none-eabi/include -I../include -I. -I../src/include -c ../common/sysinit.c -o sysinit.o > sysinit.lst 

Assembling ../common/crt0.s, writing to crt0.o... 
~/.programs/arm-gcc/bin/arm-none-eabi-as -mcpu=cortex-m4 -o crt0.o ../common/crt0.s > crt0.lst 

Compiling uart.c, writing to uart.o... 
~/.programs/arm-gcc/bin/arm-none-eabi-gcc -Wall -fno-common -mcpu=cortex-m4 -mthumb -O0 -g -DF_CPU=72000000 -I~/.programs/arm-gcc/arm-none-eabi/include -I../include -I. -I../src/include -c uart.c -o uart.o > uart.lst 

~/.programs/arm-gcc/bin/arm-none-eabi-ld prog.o sysinit.o crt0.o uart.o -Map=prog.map -T../common/Teensy31_flash.ld -L~/.programs/arm-gcc/arm-none-eabi/lib -o prog.elf 
prog.o: In function `print_char': 
/home/user/workspace/prog/src/prog.c:47: undefined reference to `sprintf' 
/home/user/workspace/prog/src/prog.c:48: undefined reference to `printf' 
/home/user/workspace/prog/src/prog.c:49: undefined reference to `strlen' 
prog.o: In function `main': 
/home/user/workspace/prog/src/prog.c:72: undefined reference to `memset' 
make: *** [makefile:133: prog.elf] Error 1 

我向stdlib投了几个电话来说明问题。我曾尝试使用-lc对libc中明确地联系起来,但我遇到了:

/home/prog/.programs/arm-gcc/bin/arm-none-eabi-ld: cannot find -lc 

存档目录通过传递给链接:

L~/.programs/arm-gcc/arm-none-eabi/lib 

我已经检查这个目录的内容,验证存档存在:

$ ls ~/.programs/arm-gcc/arm-none-eabi/lib 
aprofile-validation.specs libc_s.a   linux.specs 
aprofile-ve.specs   libg.a    nano.specs 
armv6-m     libgloss-linux.a nosys.specs 
armv7-ar     libg_s.a   pid.specs 
armv7e-m     libm.a    rdimon-crt0.o 
armv7-m     libnosys.a   rdimon.specs 
cpu-init     librdimon.a   rdpmon-crt0.o 
crt0.o      librdimon_s.a  rdpmon.specs 
fpu      librdpmon.a   redboot-crt0.o 
iq80310.specs    libstdc++.a   redboot.ld 
ldscripts     libstdc++.a-gdb.py redboot.specs 
libarm_cortexM0l_math.a libstdc++_s.a  redboot-syscalls.o 
libarm_cortexM4lf_math.a libsupc++.a   thumb 
libarm_cortexM4l_math.a libsupc++_s.a 
libc.a      linux-crt0.o 

最后,libc中确实出口sprintf(等)

$ nm libc.a | grep "sprintf" 
... 
lib_a-sprintf.o: 
00000000 T sprintf 
00000000 T _sprintf_r 
     U sprintf 
     U _sprintf_r 
... 

信息上我使用的是ARM-GCC编译:

$ ./arm-none-eabi-gcc -v 
Using built-in specs. 
COLLECT_GCC=./arm-none-eabi-gcc 
COLLECT_LTO_WRAPPER=/home/user/.programs/arm-gcc/bin/../lib/gcc/arm-none-eabi/5.4.1/lto-wrapper 
Target: arm-none-eabi 
Configured with: /home/build/work/GCC-5-0-build/src/gcc/configure --target=arm-none-eabi --prefix=/home/build/work/GCC-5-0-build/install-native --libexecdir=/home/build/work/GCC-5-0-build/install-native/lib --infodir=/home/build/work/GCC-5-0-build/install-native/share/doc/gcc-arm-none-eabi/info --mandir=/home/build/work/GCC-5-0-build/install-native/share/doc/gcc-arm-none-eabi/man --htmldir=/home/build/work/GCC-5-0-build/install-native/share/doc/gcc-arm-none-eabi/html --pdfdir=/home/build/work/GCC-5-0-build/install-native/share/doc/gcc-arm-none-eabi/pdf --enable-languages=c,c++ --enable-plugins --disable-decimal-float --disable-libffi --disable-libgomp --disable-libmudflap --disable-libquadmath --disable-libssp --disable-libstdcxx-pch --disable-nls --disable-shared --disable-threads --disable-tls --with-gnu-as --with-gnu-ld --with-newlib --with-headers=yes --with-python-dir=share/gcc-arm-none-eabi --with-sysroot=/home/build/work/GCC-5-0-build/install-native/arm-none-eabi --build=i686-linux-gnu --host=i686-linux-gnu --with-gmp=/home/build/work/GCC-5-0-build/build-native/host-libs/usr --with-mpfr=/home/build/work/GCC-5-0-build/build-native/host-libs/usr --with-mpc=/home/build/work/GCC-5-0-build/build-native/host-libs/usr --with-isl=/home/build/work/GCC-5-0-build/build-native/host-libs/usr --with-cloog=/home/build/work/GCC-5-0-build/build-native/host-libs/usr --with-libelf=/home/build/work/GCC-5-0-build/build-native/host-libs/usr --with-host-libstdcxx='-static-libgcc -Wl,-Bstatic,-lstdc++,-Bdynamic -lm' --with-pkgversion='GNU Tools for ARM Embedded Processors' --with-multilib-list=armv6-m,armv7-m,armv7e-m,armv7-r,armv8-m.base,armv8-m.main 
Thread model: single 
gcc version 5.4.1 20160609 (release) [ARM/embedded-5-branch revision 237715] (GNU Tools for ARM Embedded Processors) 

回答

1

~-L~/.programs/arm-gcc/arm-none-eabi/lib没有得到扩大。这是shell that expands ~ into /home/user, but only when it is at the beginning of a word

实际上,没有必要添加这个-L选项,因为这已经在gcc的默认搜索路径中。但是,您直接链接ld而不是gcc。这不是一个好主意。最好链接gcc,并允许它添加默认搜索路径和其他所需的库,如libclibgcc。由于您提供了自己的crt0,因此请将-nostartfiles添加到参数中。所以链接:

~/.programs/arm-gcc/bin/arm-none-eabi-gcc prog.o sysinit.o crt0.o uart.o \ 
    -Wl,-Map=prog.map -Wl,-T../common/Teensy31_flash.ld -nostartfiles -o prog.elf 
相关问题