Strange Compiler Error: "undefined reference to 'main'"

10,250

Solution 1

That first rule should be

program : main.o render.o screenwriter.o
    g++ -o program main.o render.o screenwriter.o -lSDL

Assuming that you want to link main.o render.o screenwriter.o into an executable called program

Also, in the compile steps ( -c ) the -lDSL bit is not useful, it's a linker instruction.

Solution 2

Change the second line to:

g++ -o program main.o render.o screenwriter.o -lSDL
       ^^^^^^^

Otherwise your output is main.o and you're missing it in the input.

Even better than manual maintenance martyrdom is to use special macros:

$(CXX) -o $@ $+ -lSDL

So even when you expand your program, you won't have to edit that command again.

Share:
10,250
zeboidlund
Author by

zeboidlund

Updated on June 05, 2022

Comments

  • zeboidlund
    zeboidlund almost 2 years

    Could someone tell me what this means?

    /usr/lib/i386-linux-gnu/gcc/i686-linux-gnu/4.5.2/../../../crt1.o: In function `_start':
    (.text+0x18): undefined reference to `main'
    collect2: ld returned 1 exit status
    make: *** [program] Error 1
    

    My make file looks like as follows:

    program : main.o render.o screenwriter.o
        g++ -o main.o render.o screenwriter.o -lSDL
    
    main.o : main.cpp render.h screenwriter.h
        g++ -c main.cpp render.h screenwriter.h -lSDL
    
    render.o : render.h render.cpp
        g++ -c render.h render.cpp -lSDL
    
    screenwriter.o : screenwriter.h screenwriter.cpp
        g++ -c screenwriter.h screenwriter.cpp -lSDL
    
    clean:
        rm program main.o render.o screenwriter.o -lSDL
    

    Thanks.