我正在为 Cortex-A8 处理器编写软件,我必须编写一些 ARM 汇编代码来访问特定的寄存器。我正在使用 GNU 编译器和相关工具链;这些工具安装在带有 Ubuntu 的处理器板 (Freescale i.MX515) 上。我使用 WinSCP 和 PuTTY 终端从我的主机 (Windows) 连接到它。
像往常一样,我从一个包含 main.c 和 functions.s 的简单 C 项目开始。我使用 GCC编译 main.c,使用 as 和 link<组装 functions.s/strong> 再次使用 GCC 生成的目标文件,但在此过程中出现奇怪的错误。
一项重要发现 -
与此同时,我发现我的汇编代码可能有一些问题,因为当我使用命令 as -o functions.o functions.s
单独汇编它并尝试运行生成的函数时.o 使用 ./functions.o
命令,Bash shell 无法将此文件识别为可执行文件(按下选项卡 functions.o 未被选中/PuTTY 未突出显示文件)。
谁能告诉我这里发生了什么?在链接过程中,是否有任何我必须发送给 GCC 的特定选项?我看到的错误很奇怪,超出了我的理解范围,我不明白 GCC 指的是什么。
我在此处粘贴 main.c、functions.s、Makefile 和错误列表的内容。
请帮忙!!!
**按照此处的建议编辑生成文件后包含的最新错误**
ubuntu@ubuntu-desktop:~/Documents/Project/Others/helloworld$ make
gcc -c -mcpu=cortex-a8 main.c
as -mcpu=cortex-a8 -o functions.o functions.s
gcc -o hello main.o functions.o
functions.o: In function `_start':
(.text+0x0): multiple definition of `_start'
/usr/lib/gcc/arm-linux-gnueabi/4.3.3/../../../crt1.o:init.c:(.text+0x0): first defined here
collect2: ld returned 1 exit status
make: *** [hello] Error 1
main.c
#include <stdio.h>
#include <stdlib.h>
int main(void) {
puts("!!!Hello World!!!"); /* prints !!!Hello World!!! */
return EXIT_SUCCESS;
}
函数.s
* Main program */
.equ STACK_TOP, 0x20000800
.text
.global _start
.syntax unified
_start:
.word STACK_TOP, start
.type start, function
start:
movs r0, #10
movs r1, #0
.end
生成文件
all: hello
hello: main.o functions.o
gcc hello -o main.o functions.o
-- hello 是在 Stack Overflow 的人在这里建议后包含在这里的,但问题仍然存在;我仍然遇到同样的错误。
main.o: main.c
gcc -c -mcpu=cortex-a8 main.c
functions.o: functions.s
as -mcpu=cortex-a8 -o functions.o functions.s
错误
ubuntu@ubuntu-desktop:~/Documents/Project/Others/helloworld$ make
gcc -c -mcpu=cortex-a8 main.c
as -mcpu=cortex-a8 -o functions.o functions.s
gcc -o main.o functions.o
functions.o: In function `_start':
(.text+0x0): multiple definition of `_start'
/usr/lib/gcc/arm-linux-gnueabi/4.3.3/../../../crt1.o:init.c:(.text+0x0): first defined here
/usr/lib/gcc/arm-linux-gnueabi/4.3.3/../../../crt1.o: In function `_start':
init.c:(.text+0x30): undefined reference to `main'
collect2: ld returned 1 exit status
make: *** [hello] Error 1
最佳答案
在生成文件中:
hello: main.o functions.o
gcc -o main.o functions.o
应该是:
hello: main.o functions.o
gcc -o hello main.o functions.o
就目前而言,您链接的是 functions.o,而不是 main.o,并生成一个名为 main.o 的输出可执行文件,它会覆盖现有的 main.o。
关于c - 更多 GCC 链接时间问题 : undefined reference to main,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2669082/