【问题标题】:Compiling simple C program does not link properly编译简单的 C 程序没有正确链接
【发布时间】:2023-04-08 18:22:02
【问题描述】:

我正在尝试编译一个简单的 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

【问题讨论】:

  • 链接器命令似乎没有自动包含 C 库。将-lc 添加到您的gcc 命令行有帮助吗?
  • 我尝试使用以下代码进行编译,mips-gcc -lc -v hello.c -o hello,但得到了相同的错误结果。
  • 在末尾尝试-lc - 例如mips-gcc -v -o hello hello.c -lc
  • mips-gcc -v -o hello hello.c -lc 产生了不同的错误输出。 ghostbin.com/paste/cdwaq

标签: c gcc cross-compiling


【解决方案1】:

停止传递-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)。换句话说,您正在以某种“裸机”模式调用编译器,它假定您正在编写代码以直接在机器上运行,而不是在托管的 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!)。

【讨论】:

    猜你喜欢
    • 2020-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多