I'm trying to get SDL2 working in Dev-C++. I DLed the 2.26.4 version from GitHub which is the latest stable release.
I only have one .cpp file in my project but the linker is complaining that multiple definitions of 'main' have been found.
main.cpp:
#include "SDL.h"
#include <iostream>
int SDL_main(int argc, char *argv[])
{
SDL_Init( SDL_INIT_EVERYTHING );
SDL_Quit();
return (0);
}
screenshot:
You can see there the really frustrating thing: Line and File fields are mostly blank. So when it says "first defined here", it's not actually showing me where it was first defined...
But the Compile Log was better:
make.exe -f "E:\Utilities\Dev-Cpp\Makefile.win" all
g++.exe -c Example_SDLtest1/main.cpp -o Example_SDLtest1/main.o -I"E:/Utilities/Dev-Cpp/include/c++/3.4.2/backward" -I"E:/Utilities/Dev-Cpp/include/c++/3.4.2/mingw32" -I"E:/Utilities/Dev-Cpp/include/c++/3.4.2" -I"E:/Utilities/Dev-Cpp/include" -I"C:/SDL/SDL2-2.26.4/include"
g++.exe Example_SDLtest1/main.o -o "Example_SDLtest1.exe" -L"E:/Utilities/Dev-Cpp/lib" -L"C:/SDL/SDL2-2.26.4/lib/x86" -mwindows -lmingw32 -lSDL2main -lSDL2
C:/SDL/SDL2-2.26.4/lib/x86\SDL2main.lib(Win32/Release/SDL_windows_main.obj)(.text$mn+0x0): multiple definition of `main'
E:/Utilities/Dev-Cpp/lib/libmingw32.a(main.o)(.text+0x0):main.c: first defined here
collect2: ld returned 1 exit status
make.exe: *** [Example_SDLtest1.exe] Error 1
Execution terminated
Makefile.win:
# Project: Example_SDLtest1
# Makefile created by Dev-C++ 4.9.9.2
CPP = g++.exe
CC = gcc.exe
WINDRES = windres.exe
RES =
OBJ = Example_SDLtest1/main.o $(RES)
LINKOBJ = Example_SDLtest1/main.o $(RES)
LIBS = -L"E:/Utilities/Dev-Cpp/lib" -L"C:/SDL/SDL2-2.26.4/lib/x86" -mwindows -lmingw32 -lSDL2main -lSDL2
INCS = -I"E:/Utilities/Dev-Cpp/include"
CXXINCS = -I"E:/Utilities/Dev-Cpp/include/c++/3.4.2/backward" -I"E:/Utilities/Dev-Cpp/include/c++/3.4.2/mingw32" -I"E:/Utilities/Dev-Cpp/include/c++/3.4.2" -I"E:/Utilities/Dev-Cpp/include" -I"C:/SDL/SDL2-2.26.4/include"
BIN = Example_SDLtest1.exe
CXXFLAGS = $(CXXINCS)
CFLAGS = $(INCS)
RM = rm -f
.PHONY: all all-before all-after clean clean-custom
all: all-before Example_SDLtest1.exe all-after
clean: clean-custom
${RM} $(OBJ) $(BIN)
$(BIN): $(OBJ)
$(CPP) $(LINKOBJ) -o "Example_SDLtest1.exe" $(LIBS)
Example_SDLtest1/main.o: Example_SDLtest1/main.cpp
$(CPP) -c Example_SDLtest1/main.cpp -o Example_SDLtest1/main.o $(CXXFLAGS)
screenshots of the includes and lib folders:
(I also tried removing all those folders from both tabs, except the actual SDL folders. This didn't change the error message and in fact the compile log still pointed to libmingw32.a.)
What I've Done to Investigate:
Rename "int main" to "int SDL_main" or something arbitrary like "main2". This doesn't solve the problem.
I switched around the 32- and 64-bit versions of the compiler and linker, making sure to point the include and lib folders to the right subfolder. This didn't produce any different results except when the 32/64 stuff didn't match.
I hunted down the libmingw32.a file in the Dev-Cpp/Lib folder, but it's last modified date was 2005 so I don't think the problem is in there. However I don't know how to open and view the .a file so idk for sure.
In Project Options, General tab, I tried switching among Win32 GUI and Win32 Console type, but that never changed any error messages.
I looked inside SDL.h, but I didn't see anywhere where it's #defining main or SDL_main. However this .h file has a ton of other includes, at least 30 of them, which is why I really wish the log would tell me where the first #define of it is located...
But even then I don't understand what exactly to do. Modify the SDL2 source files? Instead, I suspect there is something wrong with the way the linker or compiler is set up, trying to do things out of order or getting the wrong structure somehow.
I have also looked at similar Stack Overflow questions: here and here. The last one has a really promising troubleshooting guide which I read in its entirety, but could not track down the problem.
I understand that SDL somehow pre-empts your main because it wants to be the entry point, so it can configure stuff, and the somehow hand off to your "main". If that's the case, I don't understand why I can't just rename my main to something arbitrary and go from there.
The good news is, if it's a linker error, then the code actually compiles, so this SDL stuff along with my own code is apparently working on its own. It just won't link together because of name conflicts. I suspect the solution should be some really simple thing to resolve that conflict, but cannot figure it out.