【发布时间】:2015-07-12 18:56:48
【问题描述】:
我正在尝试使用针对 mips 的交叉编译器 (GCC 4.9.2) 来编译以下简单的“Hello World”程序:
#include <stdio.h>
int main()
{
int x = 5;
printf("x = %d\n", x);
}
x 变量用于阻止 GCC 将 printf 更改为 puts,这对于简单的换行符终止的字符串似乎是自动执行的。
我在${HOME}/xc 下构建了一个交叉编译器,并正在使用以下命令执行它:
${HOME}/xc/bin/mips-gcc -v hello.c
但是,我收到以下错误:
/tmp/ccW5mHJu.o: In function `main':
(.text+0x24): undefined reference to `printf'
collect2: error: ld returned 1 exit status
我假设这是链接器的问题,因为如果在搜索路径上找不到例如 stdio.h,我预计该过程会提前失败。我可以编译一个简单的程序,它只返回零,所以并不是整个工具链都被破坏了,大概只是标准库链接(我使用的是 newlib 2.2.0-1)。
无论我是在 Linux (Ubuntu 14.10) 还是 Cygwin (Windows 8) 下运行交叉编译器,我都会遇到相同的错误。
GCC 的完整输出是:
Using built-in specs.
COLLECT_GCC=/home/paul/xc/bin/mips-gcc
COLLECT_LTO_WRAPPER=/home/paul/xc/libexec/gcc/mips/4.9.2/lto-wrapper
Target: mips
Configured with: /home/paul/xc/mips/tmp/gcc-4.9.2/configure --prefix=/home/paul/xc --target=mips --enable-languages=c --with-newlib --without-isl --without-cloogs --disable-threads --disable-libssp --disable-libgomp --disable-libmudflap
Thread model: single
gcc version 4.9.2 (GCC)
COLLECT_GCC_OPTIONS='-v'
/home/paul/xc/libexec/gcc/mips/4.9.2/cc1 -quiet -v hello.c -quiet -dumpbase hello.c -auxbase hello -version -o /tmp/ccCpAajQ.s
GNU C (GCC) version 4.9.2 (mips)
compiled by GNU C version 4.9.1, GMP version 6.0.0, MPFR version 3.1.2, MPC version 1.0.3
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
ignoring nonexistent directory "/home/paul/xc/lib/gcc/mips/4.9.2/../../../../mips/sys-include"
#include "..." search starts here:
#include <...> search starts here:
/home/paul/xc/lib/gcc/mips/4.9.2/include
/home/paul/xc/lib/gcc/mips/4.9.2/include-fixed
/home/paul/xc/lib/gcc/mips/4.9.2/../../../../mips/include
End of search list.
GNU C (GCC) version 4.9.2 (mips)
compiled by GNU C version 4.9.1, GMP version 6.0.0, MPFR version 3.1.2, MPC version 1.0.3
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: cffaaedf0b24662e67a5d97387fc5b17
COLLECT_GCC_OPTIONS='-v'
/home/paul/xc/lib/gcc/mips/4.9.2/../../../../mips/bin/as -EB -O1 -no-mdebug -mabi=32 -o /tmp/ccW5mHJu.o /tmp/ccCpAajQ.s
COMPILER_PATH=/home/paul/xc/libexec/gcc/mips/4.9.2/:/home/paul/xc/libexec/gcc/mips/4.9.2/:/home/paul/xc/libexec/gcc/mips/:/home/paul/xc/lib/gcc/mips/4.9.2/:/home/paul/xc/lib/gcc/mips/:/home/paul/xc/lib/gcc/mips/4.9.2/../../../../mips/bin/
LIBRARY_PATH=/home/paul/xc/lib/gcc/mips/4.9.2/:/home/paul/xc/lib/gcc/mips/4.9.2/../../../../mips/lib/
COLLECT_GCC_OPTIONS='-v'
/home/paul/xc/libexec/gcc/mips/4.9.2/collect2 -plugin /home/paul/xc/libexec/gcc/mips/4.9.2/liblto_plugin.so -plugin-opt=/home/paul/xc/libexec/gcc/mips/4.9.2/lto-wrapper -plugin-opt=-fresolution=/tmp/cc8TAJb9.res -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lgcc -EB /home/paul/xc/lib/gcc/mips/4.9.2/crti.o /home/paul/xc/lib/gcc/mips/4.9.2/crtbegin.o -L/home/paul/xc/lib/gcc/mips/4.9.2 -L/home/paul/xc/lib/gcc/mips/4.9.2/../../../../mips/lib /tmp/ccW5mHJu.o -lgcc -lgcc /home/paul/xc/lib/gcc/mips/4.9.2/crtend.o /home/paul/xc/lib/gcc/mips/4.9.2/crtn.o
/home/paul/xc/lib/gcc/mips/4.9.2/../../../../mips/bin/ld: warning: cannot find entry symbol _start; defaulting to 0000000000400050
/tmp/ccW5mHJu.o: In function `main':
(.text+0x24): undefined reference to `printf'
collect2: error: ld returned 1 exit status
我正在使用的构建脚本在这里(我是根据六个教程编写的,这些教程都提出了略有不同的建议):
https://github.com/UoMCS/mips-cross-compile
基本上它会执行以下步骤:
- 构建 binutils。
- 构建 GCC(第 1 阶段)。
- 构建新库。
- 构建 GCC(第 2 阶段)。
我知道还有其他工具,例如 crosstool-ng 和 builtroot,但是我正在为其构建此工具链的人希望在开始构建过程之前编辑 binutils 的某些部分,并且工具链也必须工作在 Cygwin 下(crosstool-ng 不会因为各种原因,包括区分大小写的文件路径)。
我认为这可能是显而易见的事情,但我已经搞砸了一个星期,看不出它会是什么。任何帮助将不胜感激!
【问题讨论】:
-
尝试转储由 newlib 定义的符号列表
-
我该怎么做?
-
只是出于好奇:
int main(void) {};是否编译? -
是的,我仍然收到
cannot find entry symbol _start警告(根据我使用 ARM 的经验,我认为可以忽略)但它确实编译成功。 -
如果我在我的 Linux 上运行
gcc -v test.c,它的输出中是一个-lc用于 c-library,它不在您的输出中,因此是undefined reference to printf。
标签: c gcc mips cross-compiling undefined-reference