Mingw 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.

Code:
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)

FWIW, here’s a one-liner I use to compile my SDL2 stuff with MinGW:

g++ -mwindows -o game.exe *.cpp -ISDL2 -lmingw32 -lSDL2main -lSDL2 -lm -ldinput8 -ldxguid -ldxerr8 -luser32 -lgdi32 -lwinmm -limm32 -lole32 -loleaut32 -lshell32 -lversion -luuid -lSDL2_image -lSDL2_ttf -lfreetype -lBox2D -Llibs

HTH------------------------
bigosaur.com

vastrolorde wrote:

Quite a lot of libaries you have there. although i dont know whatm ost of them are. My code is nut much different, except for fewer libaries.

Error message:

Code:

…>make app
g++ -o bin/MapEditor.exe -lmingw32 -lSDL2main -lSDL2 -Llib -lmapeditor
c:/mingw/bin/…/lib/gcc/mingw32/4.8.1/…/…/…/libSDL2main.a(SDL_windows_main.o)
: In function console_main': /Users/slouken/release/SDL/SDL2-2.0.3-source/foo-x86/../src/main/windows/SDL_win dows_main.c:140: undefined reference toSDL_main’
collect2.exe: error: ld returned 1 exit status
make: *** [bin/MapEditor.exe] Error 1

Are you sure that you are linking in the .o file created from .c or .cpp file that has a valid main() function?------------------------
bigosaur.com

Quite a lot of libaries you have there. although i dont know whatm ost of them are. My code is nut much different, except for fewer libaries.

Error message:

Code:

…>make app
g++ -o bin/MapEditor.exe -lmingw32 -lSDL2main -lSDL2 -Llib -lmapeditor
c:/mingw/bin/…/lib/gcc/mingw32/4.8.1/…/…/…/libSDL2main.a(SDL_windows_main.o)
: In function console_main': /Users/slouken/release/SDL/SDL2-2.0.3-source/foo-x86/../src/main/windows/SDL_win dows_main.c:140: undefined reference toSDL_main’
collect2.exe: error: ld returned 1 exit status
make: *** [bin/MapEditor.exe] Error 1

vastrolorde wrote:

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?

Most probably.

I never install SDL with MinGW. Instead I just keep it in a custom directory, so that I can have multiple versions of SDL available at the same time. When compiling, I use -I and -L to point to those directories (for .h and .a files respectively).------------------------
bigosaur.com

Played around with make file so i got to new error message. I get the feeling, that makeing the .cpp files into .o there is somekind of problem, or maybe where i make it into libary the SDL doesent get included.

Code:
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 toSD
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 toS
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 toS
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 toS
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

Current makefile:

Code:
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)
#Make libary
$(LIBARY):$(LIBOBJS)
ar cr $(LIBARY) $(LIBOBJS)
#Compile libary .cpp files
$(OBJDIR)/%.o: $(LIBDIR)%.cpp
$(CXX) $(INCLUDE_PATHS) $(LIBRARY_PATHS) $(LIB_CXXFLAGS) -c $(LIBDIR)$.cpp -o $(OBJDIR)/$.o
#COmpile app.exe and link
$(APP): $(APPOBJS) $(LIBRARY)
$(CXX) -o $(APP) $(APPOBJS) $(CXXFLAGS) $(LINKER_FLAGS) $(LFLAGS)
#Compile app-s .cpp 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)

My main file

Code:

//Using SDL and standard IO
#include <SDL.h>
#include <stdio.h>
#include “window.h”

//Screen dimension constants
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
bool init(SDL_Renderer* renderer, Window window);
void close(SDL_Renderer* renderer, Window* window);

int main( int argc, char *args[] )
{
//The window we’ll be rendering to
Window window;

SDL_Renderer* renderer = NULL;

if(!init(renderer, window)){
	printf("Failed to initialize! \n");
}else{
	bool quit = false;
	
	SDL_Event e;
	
	while(!quit){
		while(SDL_PollEvent(&e)!= 0){
			if(e.type == SDL_QUIT){
				quit = true;
			}
			
			window.handleEvents(e, renderer);
		}
		
		if(!window.isMinimized()){
			//SDL_SetRenderDrawColor(renderer, 0xFF,0xFF,0xFF,0xFF);
			//SDL_RenderClear(renderer);
			
			//SDL_RenderPresent(renderer);
		}
	}
}
close(renderer, &window);

return 0;

}

bool init(SDL_Renderer* renderer, Window window){
bool success = true;
if(SDL_Init(SDL_INIT_VIDEO) < 0){
printf(“SDL could not initialize! SDL Error: %s\n”, SDL_GetError());
success = false;
} else{
if(!SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, “1”)){
printf(“Warning: Linear texture filtering not enabled!”);
}

	if(!window.init()){
		printf("Window could not be created! SDL Error: %s \n", SDL_GetError());
		success = false;
	} else{
		renderer = window.createRenderer();
		if(renderer == NULL)
		{
			printf("Renderer could not be created! SDL Error: %s\n", SDL_GetError());
			success = false;
		}else{
			SDL_SetRenderDrawColor(renderer, 0xFF,0xFF,0XFF,0xFF);
		}
	}
}
return success;

}

void close(SDL_Renderer* renderer, Window* window){
SDL_DestroyRenderer(renderer);
window->free();

SDL_Quit();

}

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?

Well this is for school project and we have to use mingw. But i got it working. Problem was i had but 64 bit binaries instead of 32 bit binaries and mingw didnt like them.

My final makefile to anyone who is interested

Code:
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)