Undefined links in makefile

Ok, I read about 10 lessons, but I constantly get errors, I have 5 files, main.cpp class.cpp, class.h and functions.cpp and functions.h. They all use functions from different objects, which means that functions in functions.cpp use objects from classes.cpp.

My make file is 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 keep saying that it has undefined links. What am I missing?

What are the output makefiles

 /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 

Files compile fine when I had them in Visual C ++, so it should be my make file.

+6
source share
3 answers

You really do something weird. You must compile ( -c ) the object files and then link them together. It will 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 do this, it is even better if you are also the main.o compiler separately. 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 when using -c I deleted $(LIBS) because the link is not running.

+7
source

You are trying to link your .o files to executable files. Add -c to the compilation flags so that they compile only for your object files.

Make it the first option like this

 SDLfunctions.o: $(SDLFH) $(SDLF) $(CLASS) $(CLASSH) $(CC) -c -o $@ $(SDLF) $(LIBS) 
+6
source

You have a typo. You use $(CLASSESH) , but declared CLASSH .

+2
source

Source: https://habr.com/ru/post/902416/


All Articles