编译简单的C程序没有正确链接

时间:2014-04-11 18:20:28

标签: c gcc cross-compiling

我正在尝试编译一个简单的C程序,但显然程序没有正确链接。

的hello.c

/* Simple C program. */
#include<stdio.h>

int main() {
printf("Hello MIPS! \n");

return 0;
}

我正在尝试使用以下命令mips-gcc -v hello.c -o hello

编译程序

当我尝试编译/链接程序时我得到的输出,

Using built-in specs.
COLLECT_GCC=bin/mips-gcc
COLLECT_LTO_WRAPPER=/opt/cross/gcc-mips/libexec/gcc/mips/4.8.2/lto-wrapper
Target: mips
Configured with: ../gcc-4.8.2/configure --target=mips --prefix=/opt/cross/gcc-mips --enable-interwork --enable-multilib --enable-languages=c --with-newlib --with-headers=/opt/cross/src/newlib-2.1.0/newlib/libc/include/ --disable-libssp --disable-nls
Thread model: single
gcc version 4.8.2 (GCC) 
COLLECT_GCC_OPTIONS='-v' '-o' 'hello'
 /opt/cross/gcc-mips/libexec/gcc/mips/4.8.2/cc1 -quiet -v hello.c -quiet -dumpbase hello.c -auxbase hello -version -o /var/folders/1z/k_by6wd95tsccc6s1_tkttpr0000gn/T//ccqPwmTq.s
GNU C (GCC) version 4.8.2 (mips)
    compiled by GNU C version 4.2.1 Compatible Apple LLVM 5.1 (clang-503.0.38), GMP version 4.3.2, MPFR version 2.4.2, MPC version 0.8.1
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
#include "..." search starts here:
#include <...> search starts here:
 /opt/cross/gcc-mips/lib/gcc/mips/4.8.2/include
 /opt/cross/gcc-mips/lib/gcc/mips/4.8.2/include-fixed
 /opt/cross/gcc-mips/lib/gcc/mips/4.8.2/../../../../mips/sys-include
 /opt/cross/gcc-mips/lib/gcc/mips/4.8.2/../../../../mips/include
End of search list.
GNU C (GCC) version 4.8.2 (mips)
    compiled by GNU C version 4.2.1 Compatible Apple LLVM 5.1 (clang-503.0.38), GMP version 4.3.2, MPFR version 2.4.2, MPC version 0.8.1
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: e28a4def2fba399e5af333f18f473404
COLLECT_GCC_OPTIONS='-v' '-o' 'hello'
 /opt/cross/gcc-mips/lib/gcc/mips/4.8.2/../../../../mips/bin/as -EB -O1 -no-mdebug -mabi=32 -o /var/folders/1z/k_by6wd95tsccc6s1_tkttpr0000gn/T//ccgd49A6.o /var/folders/1z/k_by6wd95tsccc6s1_tkttpr0000gn/T//ccqPwmTq.s
COMPILER_PATH=/opt/cross/gcc-mips/libexec/gcc/mips/4.8.2/:/opt/cross/gcc-mips/libexec/gcc/mips/4.8.2/:/opt/cross/gcc-mips/libexec/gcc/mips/:/opt/cross/gcc-mips/lib/gcc/mips/4.8.2/:/opt/cross/gcc-mips/lib/gcc/mips/:/opt/cross/gcc-mips/lib/gcc/mips/4.8.2/../../../../mips/bin/
LIBRARY_PATH=/opt/cross/gcc-mips/lib/gcc/mips/4.8.2/:/opt/cross/gcc-mips/lib/gcc/mips/4.8.2/../../../../mips/lib/
COLLECT_GCC_OPTIONS='-v' '-o' 'hello'
 /opt/cross/gcc-mips/libexec/gcc/mips/4.8.2/collect2 -EB -o hello /opt/cross/gcc-mips/lib/gcc/mips/4.8.2/crti.o /opt/cross/gcc-mips/lib/gcc/mips/4.8.2/crtbegin.o -L/opt/cross/gcc-mips/lib/gcc/mips/4.8.2 -L/opt/cross/gcc-mips/lib/gcc/mips/4.8.2/../../../../mips/lib /var/folders/1z/k_by6wd95tsccc6s1_tkttpr0000gn/T//ccgd49A6.o -lgcc -lgcc /opt/cross/gcc-mips/lib/gcc/mips/4.8.2/crtend.o /opt/cross/gcc-mips/lib/gcc/mips/4.8.2/crtn.o
/opt/cross/gcc-mips/lib/gcc/mips/4.8.2/../../../../mips/bin/ld: warning: cannot find entry symbol _start; defaulting to 0000000000400038
/var/folders/1z/k_by6wd95tsccc6s1_tkttpr0000gn/T//ccgd49A6.o: In function `main':
(.text+0x18): undefined reference to `puts'
collect2: error: ld returned 1 exit status

1 个答案:

答案 0 :(得分:2)

停止传递-v;它只会产生一些疯狂的调试输出,与您的问题无关。

mips-gcc -o hello hello.c会产生类似

的内容
ld: warning: cannot find entry symbol _start; defaulting to 0000000000400038
ccgd49A6.o: In function `main': (.text+0x18): undefined reference to `puts'

第二个错误表示您需要在命令行末尾使用-lc来提取libc库(提供puts)。第一个错误表示您还需要某处crt0.o(提供_start)。换句话说,你在某种&#34;裸机&#34;中调用编译器。模式,它假定您编写的代码直接在计算机上运行,​​而不是在托管的C环境中运行(您可以访问命令行参数,标准库和文件系统,以及等等。)

你的问题与2006年的这个人类似:http://osdir.com/ml/lib.newlib/2006-07/msg00029.html 那里的解决方案是使用-T nullmon.ld来指定包含crt0.o的链接器指令文件;也许还可以使用mips-elf-gcc代替mips-gcc

然而,裸机编程通常要求你开始非常低级别,而不是尝试任何像C一样复杂的东西,直到你已经熟悉组装。有关更多信息,请参阅Bare metal cross compilers input的答案(它甚至专门讨论MIPS!)。