更多GCC链接时间问题:对main的未定义引用

Hag*_*ble 5 c linker gcc makefile options

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

像往常一样,我开始使用一个带有main.cfunctions.s的简单C项目.我使用GCC 编译 main.c ,使用as 汇编 functions.s 并再次使用GCC 链接生成的目标文件,但在此过程中我得到了奇怪的错误.

一个重要的发现 -

同时,我发现我的汇编代码可能有一些问题,因为当我使用命令单独组装它as -o functions.o functions.s并尝试运行生成的functions.o using ./functions.o命令时,bash shell无法将此文件识别为可执行文件(在按下选项卡函数时) .o未被选中/ PuTTY未突出显示该文件).

任何人都可以建议这里发生什么?在链接过程中,我是否需要向GCC发送任何特定选项?我看到的错误是奇怪的,超出了我的理解,我不明白GCC所指的是什么.

我在这里粘贴main.c,functions.s,Makefile和错误列表的内容.

请帮忙!!!

根据人们的建议编辑makfile之后包含的最新错误 -

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
Run Code Online (Sandbox Code Playgroud)

main.c中

#include <stdio.h>
#include <stdlib.h>

int main(void) {

    puts("!!!Hello World!!!"); /* prints !!!Hello World!!! */
    return EXIT_SUCCESS;
}
Run Code Online (Sandbox Code Playgroud)

functions.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
Run Code Online (Sandbox Code Playgroud)

Makefile文件

all: hello

hello: main.o functions.o
    gcc hello -o main.o functions.o
Run Code Online (Sandbox Code Playgroud)

- hello是在这里包含在stackoverflow的人在这里提出的,但问题仍然存在,我仍然得到相同的错误.

main.o: main.c
    gcc -c -mcpu=cortex-a8 main.c

functions.o: functions.s
    as -mcpu=cortex-a8 -o functions.o functions.s
Run Code Online (Sandbox Code Playgroud)

错误

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
Run Code Online (Sandbox Code Playgroud)

小智 8

在makefile中:

hello: main.o functions.o
    gcc -o main.o functions.o
Run Code Online (Sandbox Code Playgroud)

应该:

hello: main.o functions.o
    gcc -o hello main.o functions.o
Run Code Online (Sandbox Code Playgroud)

就目前而言,您将链接functions.o,但不是main.o,并生成一个名为main.o的输出可执行文件,它将覆盖您现有的main.o.