c - 更多 GCC 链接时间问题 : undefined reference to main

标签 c gcc makefile linker

我正在为 Cortex-A8 处理器编写软件,我必须编写一些 ARM 汇编代码来访问特定的寄存器。我正在使用 GNU 编译器和相关工具链;这些工具安装在带有 Ubuntu 的处理器板 (Freescale i.MX515) 上。我使用 WinSCP 和 PuTTY 终端从我的主机 (Windows) 连接到它。

像往常一样,我从一个包含 ma​​in.cfunctions.s 的简单 C 项目开始。我使用 GCC编译 main.c,使用 aslink<组装 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

ma​​in.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/

相关文章:

c - strerror_r 示例

c++ 程序在一个线程发生访问冲突时终止 - 如何在 linux 中捕获此问题 - 对于 win32 我在 vs2010 中获得堆栈跟踪

c++ - GCC轮流实现

c++ - 使用 cmake/mingw 输出 DLL 而不是 .exe

c++ - 如何将 Makefile 的隐式规则与 .cpp 文件一起使用?

c - 打印各种FILE信息

作为 C 回调的 C++ 静态成员函数需要访问非静态引用

c - strtok 未按预期工作,未拆分字符串

c++ - 这是合法的 C++ 代码吗?

c++ - 如何在 Eclipse C++ 项目中创建两个主电源