Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Undefined references in makefile

Ok, ive read about 10 tutorials, but i keep getting errors all the time, i have 5 files, main.cpp class.cpp, class.h and functions.cpp and functions.h. All of those use functions from different objects meaning that functions in functions.cpp uses objects from classes.cpp.

My makefile looks as follows

CC = g++ -O2 -I./sdl/include -L.
LIBS = -lm -lSDL -lpthread -ldl
SRC = main.cpp
SDLF = SDLfunctions.cpp
CLASS = classes.cpp
CLASSH = classes.h
SDLFH = SDLfunctions.h

all: main

main: SDLfunctions.o Classes.o $(SRC)
    $(CC) -o $@ $(SRC) $(LIBS)

SDLfunctions.o: $(SDLFH) $(SDLF) $(CLASS) $(CLASSH)
    $(CC) -o $@ $(SDLF) $(LIBS)

Classes.o: $(CLASS) $(CLASSH) $(SDLF) $(SDLFH)
    $(CC) -o $@ $(CLASS) $(LIBS) 

I keeps telling me that it has undefined references. What am i missing?

What makefile outputs

/usr/lib/i386-linux-gnu/gcc/i686-linux-gnu/4.5.2/../../../crt1.o: In function `_start':
(.text+0x18): undefined reference to `main'
/tmp/ccJG6yQA.o: In function `DrawEnemies(SDL_Surface*)':
SDLfunctions.cpp:(.text+0x3a7): undefined reference to `Enemy::sprite'
/tmp/ccJG6yQA.o: In function `rysujpociski(int, SDL_Surface*, SDL_Surface*, 
std::vector<AllyBullet, std::allocator<AllyBullet> >&, double)':
SDLfunctions.cpp:(.text+0x141f): undefined reference to `AllyBullet::sprite'
/tmp/ccJG6yQA.o: In function `global constructors keyed to width':
SDLfunctions.cpp:(.text+0x14a7): undefined reference to `Enemy::Enemy()'
collect2: ld returned 1 exit status
make: *** [SDLfunctions.o] Error 1

The files compile great when i had them in Visual C++, so it has to be my makefile.

like image 801
Bartlomiej Lewandowski Avatar asked Nov 25 '11 20:11

Bartlomiej Lewandowski


People also ask

What is an undefined reference?

An “Undefined Reference” error occurs when we have a reference to object name (class, function, variable, etc.) in our program and the linker cannot find its definition when it tries to search for it in all the linked object files and libraries.

How do you fix undefined references to Main?

To fix this error, correct the spelling of the main() function.

How do you fix a undefined reference in C++?

How To Fix Undefined Reference in C++ You can fix undefined reference in C++ by investigating the linker error messages and then providing the missing definition for the given symbols. Note that not all linker errors are undefined references, and the same programmer error does not cause all undefined reference errors.

How do you fix undefined reference error in C?

The error: undefined reference to function show() has appeared on the terminal shell as predicted. To solve this error, simply open the file and make the name of a function the same in its function definition and function call. So, we used to show(), i.e., small case names to go further.


2 Answers

You are indeed doing something strange. What you should is to compile (-c) the object files and then link them together. This would look like this:

CC = g++ -O2 -I./sdl/include -L.
LIBS = -lm -lSDL -lpthread -ldl
SRC = main.cpp
SDLF = SDLfunctions.cpp
CLASS = classes.cpp
CLASSH = classes.h
SDLFH = SDLfunctions.h

all: main

main: SDLfunctions.o Classes.o $(SRC)
    $(CC) -o $@ $(SRC) SDLfunctions.o Classes.o $(LIBS)  # you forgot to link
                                                         # the object files

SDLfunctions.o: $(SDLFH) $(SDLF) $(CLASS) $(CLASSH)
    $(CC) -o $@ -c $(SDLF)        # -c added to compile, not link

Classes.o: $(CLASS) $(CLASSH) $(SDLF) $(SDLFH)
    $(CC) -o $@ -c $(CLASS)       # -c added to compile, not link

While you are doing this, it is even better if you compiled main.o separately also. Therefore:

CC = g++ -O2 -I./sdl/include -L.
LIBS = -lm -lSDL -lpthread -ldl
MAIN = main.cpp
SDLF = SDLfunctions.cpp
CLASS = classes.cpp
CLASSH = classes.h
SDLFH = SDLfunctions.h

all: main

main: SDLfunctions.o Classes.o main.o
    $(CC) -o $@ SDLfunctions.o Classes.o main.o $(LIBS)

main.o: $(SDLFH) $(MAIN) $(CLASSH)
    $(CC) -o $@ -c $(MAIN)

SDLfunctions.o: $(SDLFH) $(SDLF) $(CLASS) $(CLASSH)
    $(CC) -o $@ -c $(SDLF)

Classes.o: $(CLASS) $(CLASSH) $(SDLF) $(SDLFH)
    $(CC) -o $@ -c $(CLASS)

Also note that I removed the $(LIBS) when using -c because linking doesn't happen then.

like image 119
Shahbaz Avatar answered Oct 01 '22 09:10

Shahbaz


You are trying to link your .o files into executables. Add a -c to the compile flags so it compiles only for you object files.

Make it the first option like this

SDLfunctions.o: $(SDLFH) $(SDLF) $(CLASS) $(CLASSH)
    $(CC) -c -o $@ $(SDLF) $(LIBS)
like image 40
Adrian Cornish Avatar answered Oct 01 '22 09:10

Adrian Cornish