mingw32 and SDL2

Im having troubles with having my makefile compile my project. Normal make just builds the libary and everything is correct. But when i do make app it tells me undefined refrence to SDL_Main. Before i had lot more undefined refrences but i got them fixed when i changed the flags order. But now im having this problem and i cant figure out how to solve this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
CXX = g++
CXXFLAGS = -Wall -std=c++11
#CXXFLAGS = -w -Wl, -subsystem,windows


LIBDIR = src/
OBJDIR = obj

LIBSRC = $(wildcard $(LIBDIR)*.cpp)
LIBOBJS = $(LIBSRC:$(LIBDIR)%.cpp=$(OBJDIR)/%.o)

LIB_CXXFLAGS = $(CXXFLAGS) -Iinclude
APP_CXXFLAGS = $(CXXFLAGS) -Iinclude

LIBARY = lib/libmapeditor.a

INCLUDE_PATHS = -IC:\MinGW\include\SDL2 
LIBRARY_PATHS = -LC:\MinGW\lib 

LINKER_FLAGS = -lmingw32 -lSDL2main -lSDL2
LFLAGS = -Llib -lmapeditor

APPOBJS = obj/mapEditor.o
APP = bin/MapEditor.exe
	
build: $(LIBARY)

$(LIBARY):$(LIBOBJS)
	ar cr $(LIBARY) $(LIBOBJS)
	
$(OBJDIR)/%.o: $(LIBDIR)%.cpp
	$(CXX) $(INCLUDE_PATHS) $(LIBRARY_PATHS) $(LIB_CXXFLAGS) -c $(LIBDIR)$*.cpp -o $(OBJDIR)/$*.o
	
$(APP): $(APPOBJS) $(LIBRARY)
	$(CXX) -o $(APP) $(LINKER_FLAGS) $(LFLAGS)
	
obj/mapEditor.o: application/mapEditor.cpp
	$(CXX) $(INCLUDE_PATHS) $(LIBRARY_PATHS) $(APP_CXXFLAGS) -c application/mapEditor.cpp -o obj/mapEditor.o 

app: $(APP)

doc:
	doxygen

clean:
	rm -rf docs/html
	rm -f $(LIBOBJS)
	rm -f $(TESTOBJS)
	rm -f $(LIBRARY)
	rm -f $(APP)
Last edited on
https://wiki.libsdl.org/FAQWindows

I get "Undefined reference to 'SDL_main'" ...

Make sure that you are declaring main() as:

#include "SDL.h"

int main(int argc, char *argv[])

You should be using main() instead of WinMain() even though you are creating a Windows application, because SDL provides a version of WinMain() which performs some SDL initialization before calling your main code. If for some reason you need to use WinMain(), take a look at the SDL source code in src/main/win32/SDL_main.c to see what kind of initialization you need to do in your WinMain() function so that SDL works properly.


I hope this solves your problem.
But in that case you should try googeling before asking questions :)

If that does not solve your problem we will have a hard time figuring out where the problem is, we can just guess.
After playing around for hours i got rid of the undefined refrence to SDL_Main but all the other undefined refrences came back.

I dont quite undertand sdl-config --cflags --libs this thing in the wiki page suggested above.

I have googled alot, but everything i have tryed doesent work. My one guess is that when im compiling my source files, SDL stuff is not included, but then why dont i get error message sooner. Or maybe is there something wrong with the way i create my libary?

My current error:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
C:\Users\MarkoPC\Desktop\C++\Projekt>make app
g++ -o bin/MapEditor.exe obj/mapEditor.o -Wall -std=c++11 -lmingw32 -lSDL2main -
lSDL2 -Llib -lmapeditor
lib/libmapeditor.a(window.o):window.cpp:(.text+0x7d): undefined reference to `SD
L_CreateWindow'
lib/libmapeditor.a(window.o):window.cpp:(.text+0xf6): undefined reference to `SD
L_CreateRenderer'
lib/libmapeditor.a(window.o):window.cpp:(.text+0x117): undefined reference to `S
DL_DestroyWindow'
lib/libmapeditor.a(window.o):window.cpp:(.text+0x1a9): undefined reference to `S
DL_RenderPresent'
lib/libmapeditor.a(window.o):window.cpp:(.text+0x1b6): undefined reference to `S
DL_RenderPresent'
lib/libmapeditor.a(window.o):window.cpp:(.text+0x2f5): undefined reference to `S
DL_SetWindowTitle'
lib/libmapeditor.a(window.o):window.cpp:(.text+0x356): undefined reference to `S
DL_SetWindowFullscreen'
lib/libmapeditor.a(window.o):window.cpp:(.text+0x37a): undefined reference to `S
DL_SetWindowFullscreen'
c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: lib/libmap
editor.a(window.o): bad reloc address 0x70 in section `.rdata'
c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: final link
 failed: Invalid operation
collect2.exe: error: ld returned 1 exit status
make: *** [bin/MapEditor.exe] Error 1 

My current makefile:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
CXX = g++
CXXFLAGS = -Wall -std=c++11
#CXXFLAGS = -w -Wl, -subsystem,windows


LIBDIR = src/
OBJDIR = obj

LIBSRC = $(wildcard $(LIBDIR)*.cpp)
LIBOBJS = $(LIBSRC:$(LIBDIR)%.cpp=$(OBJDIR)/%.o)

LIB_CXXFLAGS = $(CXXFLAGS) -Iinclude
APP_CXXFLAGS = $(CXXFLAGS) -Iinclude

LIBARY = lib/libmapeditor.a

INCLUDE_PATHS = -IC:\MinGW\include\SDL2 
LIBRARY_PATHS = -LC:\MinGW\lib 

LINKER_FLAGS = -lmingw32 -lSDL2main -lSDL2
LFLAGS = -Llib -lmapeditor

APPOBJS = obj/mapEditor.o
APP = bin/MapEditor.exe
	
build: $(LIBARY)

#Build libary
$(LIBARY):$(LIBOBJS)
	ar cr $(LIBARY) $(LIBOBJS)

#COmbile sources
$(OBJDIR)/%.o: $(LIBDIR)%.cpp
	$(CXX) $(INCLUDE_PATHS) $(LIBRARY_PATHS) $(LIB_CXXFLAGS) -c $(LIBDIR)$*.cpp -o $(OBJDIR)/$*.o

#MAke .exe file
$(APP): $(APPOBJS) $(LIBRARY)
	$(CXX) -o $(APP) $(APPOBJS) $(CXXFLAGS) $(LINKER_FLAGS) $(LFLAGS)

#Compile app source file
obj/mapEditor.o: application/mapEditor.cpp
	$(CXX) $(INCLUDE_PATHS) $(LIBRARY_PATHS) $(APP_CXXFLAGS) -c application/mapEditor.cpp -o obj/mapEditor.o 

app: $(APP)

doc:
	doxygen

clean:
	rm -rf docs/html
	rm -f $(LIBOBJS)
	rm -f $(TESTOBJS)
	rm -f $(LIBRARY)
	rm -f $(APP)
	
Did you try adding $(LIBRARY_PATHS) to your link command (line #38)?
I tryed. Cant the error be on line #34 or maybe on line #30? because these line take care of generating the libary and the error is all about libary .o files
SDL wiki says to chekc if sdl-config follows my source on command line

When you're compiling with gcc, you need to make sure the output of sdl-config follows your source file on the command line: gcc -o test test.c sdl-config --cflags --libs


So i added sdl-config --cflags --libs to my command and i got somekind of weird location
/usr/local/cross-tools


So could it be its looking to the wrong place?
C:\Users\MarkoPC\Desktop\C++\Projekt>make app
g++ -o bin/MapEditor.exe obj/mapEditor.o -Wall -std=c++11 -lmingw32 -lSDL2main -
lSDL2 -Llib -lmapeditor


The order may be the source of the problem.
mapeditor is a library that depends on SDL2main and SDL2, right?

In that case you have to put it before the libraries.

-lA -lB -lC -lD
A depends on B depends on C depends on D

So:
mapeditor depends on SDL2 and SDL2main and therefore has to be before them:
-lmingw32 -lmapeditor -lSDL2main -lSDL2 -Llib
Last edited on
I got it working. The problem was, i had taken 64bit SDL binaries and mingw dindt like them.
After i palced 32bit binaries it started working.

My final makefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
CXX = g++
CXXFLAGS = -Wall -std=c++11 $(shell sdl2-config --cflags)
#CXXFLAGS = -w -Wl, -subsystem,windows


LIBDIR = src/
OBJDIR = obj

LIBSRC = $(wildcard $(LIBDIR)*.cpp)
LIBOBJS = $(LIBSRC:$(LIBDIR)%.cpp=$(OBJDIR)/%.o)

LIB_CXXFLAGS = $(CXXFLAGS) -Iinclude
APP_CXXFLAGS = $(CXXFLAGS) -Iinclude

LIBARY = lib/libmapeditor.a

INCLUDE_PATHS = -IC:\MinGW\include\SDL2 
LIBRARY_PATHS = -LC:\MinGW\lib 

LINKER_FLAGS = -lmingw32 -lmapeditor -lSDL2main -lSDL2 -Llib


APPOBJS = obj/mapEditor.o
APP = bin/MapEditor.exe
	
build: $(LIBARY)

$(LIBARY):$(LIBOBJS)
	ar cr $(LIBARY) $(LIBOBJS)
	
$(OBJDIR)/%.o: $(LIBDIR)%.cpp
	$(CXX) $(INCLUDE_PATHS) $(LIBRARY_PATHS) $(LIB_CXXFLAGS) -c $(LIBDIR)$*.cpp -o $(OBJDIR)/$*.o
	
$(APP): $(APPOBJS) $(LIBRARY)
	$(CXX) -o $(APP) $(APPOBJS) $(INCLUDE_PATHS) $(CXXFLAGS) $(LINKER_FLAGS)
obj/mapEditor.o: application/mapEditor.cpp
	$(CXX) $(INCLUDE_PATHS) $(LIBRARY_PATHS) $(APP_CXXFLAGS) -c application/mapEditor.cpp -o obj/mapEditor.o 

app: $(APP)

doc:
	doxygen

clean:
	rm -rf docs/html
	rm -f $(LIBOBJS)
	rm -f $(TESTOBJS)
	rm -f $(LIBRARY)
	rm -f $(APP)
	
Problems like this are annoying...
Topic archived. No new replies allowed.