Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 405
zeboidlund Avatar asked Dec 06 '22 19:12

zeboidlund


2 Answers

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.

like image 140
fvu Avatar answered Dec 20 '22 11:12

fvu


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.

like image 36
Kerrek SB Avatar answered Dec 20 '22 11:12

Kerrek SB