More GCC link time issues: undefined reference to main

15,678

Solution 1

Shouldn't

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

be

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

Solution 2

As Bigbohne suggests, gcc is trying to link in the standard runtime library. Try adding the -nostdlib option to your gcc call:

gcc -nostdlib -o hello main.o functions.o
Share:
15,678

Related videos on Youtube

HaggarTheHorrible
Author by

HaggarTheHorrible

Updated on June 04, 2022

Comments

  • HaggarTheHorrible
    HaggarTheHorrible about 2 years

    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 stackoverflow, 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
    
    • Clifford
      Clifford about 14 years
      The build log suggests that you are targeting Linux. Is this intentional or were you intending a 'bare-metal' target? It appears by targeting Linux, init.o is being implicitly linked, and that already contains the symbol _start. That is probably a different issue however to the missing main().

Related