且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

更多GCC链接时的问题:未定义引用主

更新时间:2022-10-25 20:34:26

在生成文件:

 你好:main.o中functions.o
    GCC -o main.o中functions.o

应该是:

 你好:main.o中functions.o
    GCC -o你好main.o中functions.o

既然这样,你要链接functions.o,但并不main.o中,并会生成一个名为main.o中输出可执行,这是覆盖现有main.o.

I'm writing software for a Cortex-A8 processor and I have to write some ARM assembly code to access specific registers. I'm making use of the gnu compilers and related tool chains, these tools are installed on the processor board(Freescale i.MX515) with Ubuntu. I make a connection to it from my host PC(Windows) using WinSCP and the PuTTY terminal.

As usual I started with a simple C project having main.c and functions.s. I compile the main.c using GCC, assemble the functions.s using as and link the generated object files using once again GCC, but I get strange errors during this process.

An important finding -

Meanwhile, I found out that my assembly code may have some issues because when I individually assemble it using the command as -o functions.o functions.s and try running the generated functions.o using ./functions.o command, the bash shell is failing to recognize this file as an executable(on pressing tab functions.o is not getting selected/PuTTY is not highlighting the file).

Can anyone suggest whats happening here? Are there any specific options I have to send, to GCC during the linking process? The errors I see are strange and beyond my understanding, I don't understand to what the GCC is referring.

I'm pasting here the contents of main.c, functions.s, the Makefile and the list of errors.

Help, please!!!

Latest errors included after the makfile was edited as suggested by guys here -

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;
}

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

Makefile

all: hello

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

-- hello was included here after suggested here by guys at ***, but the problem still persists, I still get the same errors.

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

functions.o: functions.s
    as -mcpu=cortex-a8 -o functions.o functions.s

Errors

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

In the makefile:

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

should be:

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

As it stands, you are linking functions.o, but not main.o, and producing an output executable called main.o, which is overwriting your existing main.o.