0

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:

enter image description here

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:

enter image description here

enter image description here

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

genpfault
  • 51,148
  • 11
  • 85
  • 139
DrZ214
  • 486
  • 5
  • 19
  • @273K Because the compile log mentioned it as part of the "first defined here" line. It also mentioned (main.o) and (main.c) but those do not exist in the folder E:/Utilities/Dev-Cpp/lib/ so there may be something erroneous going on or maybe they were just temp files part of the compile. – DrZ214 Mar 16 '23 at 06:22
  • @273K Edited OP with screenshots of the includes and lib folders. So those other folders, i think those have been in the compiler options as a default ever since i got Dev-C++ many years ago. However, i just removed all of them except the SDL folders (in Includes and Lib), and tried again and got the exact same error message. So idk why exactly libmingw32.a is being linked all the time. – DrZ214 Mar 16 '23 at 06:35
  • @273K I just tried renaming it to libmingw32_bu.a, and the compiler gives a different error now, "cannot find -lmingw32". I think it's something always linked by default as if it were part of the compiler. – DrZ214 Mar 16 '23 at 06:46
  • Ok, got it, it's done unintentionally. – 273K Mar 16 '23 at 06:49
  • 2
    Why is `SDL2main.lib` mentioned in the error? It shouldn't exist at all (it's for msvc, not gcc), you should only have `limSDL2main.a`. – HolyBlackCat Mar 16 '23 at 06:54
  • @HolyBlackCat You mean lib, not lim? I think you're right. From GitHub I DLed the folder called SDL2-devel-2.26.4-VC.zip but i think what i wanted is actually SDL2-devel-2.26.4-mingw.tar.gz . And i can see in there, there is a libSDL2main.a. However, preliminary tries after setting all the new paths gives me a different error: "cannot find -lSDL2main". In Project Options, Parameters, Linker i have "-lmingw32 -lSDL2main -lSDL2", but what should it say instead? – DrZ214 Mar 16 '23 at 07:11
  • 1
    Your `-l` flags are correct. You need to pass the directory containing `.a`s to `-L`, like you've been doing with `-L"C:/SDL/SDL2-2.26.4/lib/x86"`. – HolyBlackCat Mar 16 '23 at 07:35
  • @HolyBlackCat Yep, i now have "C:\SDL\SDL2-2.26.4\i686-w64-mingw32\lib" for the lib directory and "C:\SDL\SDL2-2.26.4\i686-w64-mingw32\include\SDL2" for the includes directory...**and it works.** I mean it compiles and runs, but nothing pops up. I put in preliminary code to draw something from this tutorial (https://www.youtube.com/watch?v=mxWkj0KiICk) but still no window ever opens. I hate silent errors. I am actually not sure if the 64- or 32- bit version is selected correctly. The other folder version is called "x86_64-w64-mingw32". – DrZ214 Mar 16 '23 at 07:45
  • @HolyBlackCat nm, thx to ur post (https://stackoverflow.com/questions/64396979/how-do-i-use-sdl2-in-my-programs-correctly) i found the issue... didn't have SDL2.dll in the right place, stupidly the .cpp was in another folder from the .exe. Now it works! Thank you for helping me work thru this. – DrZ214 Mar 16 '23 at 07:55
  • Unrelated: dev-c++ is a terrible, outdated excuse for an IDE. Pick up a more modern toolchain. Visual Studio Community is free and recent. Or pick up a more modern GCC from the mingw project. – Botje Mar 16 '23 at 08:32
  • @Botje I like it because it's so lightweight and doesn't have a connection to microsoft. Yeah VS is free...unless you become a successful indie dev, after a certain income you hafta buy a license. I can check out the mingw project, but can you explain why dev-c++ is terrible? It's a little old by now but other than that i don't see anything wrong. – DrZ214 Mar 16 '23 at 08:45
  • Concerning: _stupidly the .cpp was in another folder from the .exe._ This is not as stupid as it seems to you. Once you start bigger projects and use `git` for source management you will see... ;-) – Scheff's Cat Mar 16 '23 at 08:51
  • 1
    dev-C++ 4.9.9.2 ships with [GCC 3.4.2](https://www.bloodshed.net/), which was released *nineteen years ago*. C++ has evolved a lot in the meantime, and you will be unable to use any of the nice features of modern C++. At least switch to a [modern fork of dev-C++](https://www.embarcadero.com/free-tools/dev-cpp) that uses GCC 9.2 which is "only" four years old. Or, again, get an up to date GCC/Clang installation and use that. – Botje Mar 16 '23 at 08:54
  • 1
    If you want to keep using GCC, you should get the latest version from [MSYS2](https://stackoverflow.com/q/30069830/2752075). You can also install SDL2 (and other libs) from here, which makes things easier. – HolyBlackCat Mar 16 '23 at 09:39

0 Answers0