5

我正在为 Cortex-A8 处理器编写软件,我必须编写一些 ARM 汇编代码来访问特定的寄存器。我正在使用 gnu 编译器和相关工具链,这些工具安装在 Ubuntu 的处理器板上(飞思卡尔 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./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

主程序

#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

- 在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

错误

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
4

4 回答 4

8

在生成文件中:

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。

于 2010-04-19T16:28:41.053 回答
2

正如 Bigbohne 建议的那样,gcc 正在尝试链接标准运行时库。尝试将 -nostdlib 选项添加到您的 gcc 调用中:

gcc -nostdlib -o hello main.o functions.o
于 2010-04-19T18:50:30.017 回答
2

不应该

hello: main.o functions.o
    gcc -o main.o functions.o

hello: main.o functions.o
    gcc -o hello main.o functions.o
于 2010-04-19T16:28:59.250 回答
0

我认为这与 gcc 最后链接的运行时库有关。

这个库中已经有一个“_start”。

我认为您必须在没有“标准库”的情况下进行编译。但是你不会有 printf、getchar 和所有其他有用的东西。

于 2010-04-19T16:50:51.087 回答