Reputation: 25
I am currently learning how to use makefile and currently dividing my main.cpp to multiple files.
here is my current structure
├── makefile
└── src
├── Graphics
│ ├── Graphics.cpp
│ └── Graphics.h
└── main.cpp
and my makefile
CC = g++
CFLAGS = -c
LFLAGS = -Wall
OUTPUT = game
game : main.o Graphics.o
$(CC) $(LFLAGS) main.o Graphics.o -lSDL2 -lSDL2_image -o $(OUTPUT)
main.o : src/main.cpp
$(CC) $(CFLAGS) src/main.cpp
Graphics.o : src/Graphics/Graphics.cpp
$(CC) $(CFlAGS) src/Graphics/Graphics.cpp -lSDL2
Everytime I compile. I always get this Error. But I have my main declared. is it looking for a main inside my Graphics.cpp?
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_line): relocation 0 has invalid symbol index 2
/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: error: ld returned 1 exit status
make: *** [Graphics.o] Error 1
Here is my main function
#include "Graphics/Graphics.h"
int main(int argc , const char * argv[]){
Graphics graphics;
while(true){
//do things here later
}
return 0;
}
//Edit.
Btw. is there also a way not to link sdl2 on Graphics.cpp, If remove -lSDL2. it would give me all that sdl2 function undefined
Upvotes: 1
Views: 1402
Reputation: 63892
You have a typo in your makefile; $(CFlAGS)
is not the same as $(CFLAGS)
, meaning that -c
is not passed as a command-line option to the compiler when trying to compile src/Graphics/Graphics.cpp
.
CC = g++
CFLAGS = -c
LFLAGS = -Wall
OUTPUT = game
game : main.o Graphics.o
$(CC) $(LFLAGS) main.o Graphics.o -lSDL2 -lSDL2_image -o $(OUTPUT)
main.o : src/main.cpp
$(CC) $(CFLAGS) src/main.cpp
Graphics.o : src/Graphics/Graphics.cpp
$(CC) $(CFlAGS) src/Graphics/Graphics.cpp -lSDL2
Variables in makefiles are case-sensitive, which means that even though you wanted to include the contents of CFLAGS
you instead got the contents of an undeclared variable named CFlAGS
.
Note: Without
-c
you are tellingg++
to run the linker, and as such it will look for a function namedmain
so that it can produce an executable;graphics.cpp
does not contain such function, and the diagnostic you have presented is emitted.
Note: After you have fixed the issue with
$(CFlAGS)
you will be able to remove-lSDL2
from the relevant line.
Upvotes: 2
Reputation: 85887
In your error message it says:
make: *** [Graphics.o] Error 1
That means this error occurred while compiling Graphics.o
, not while linking game
.
Your command for compiling Graphics.o
is
$(CC) $(CFlAGS) src/Graphics/Graphics.cpp -lSDL2
-lSDL2
doesn't make sense here: You want to compile an object file, so the linker is not involved. But you're getting linker errors - why?
Because to compile without linking, you need the -c
option. This is part of your CFLAGS
variable. But you're not using CFLAGS
, you're using CFlAGS
(lowercase l
). So I bet the command that make is running looks like
g++ src/Graphics/Graphics.cpp -lSDL2
without -c
.
Fix: remove -lSDL2
, capitalize L
in variable name.
Upvotes: 1