I started using SDL today and had some trouble before, now I got it running but it won't let me init it.
This is my code:
#include <iostream>
#include "SDL.h"
#undef main
using namespace std;
int main(){
if(SDL_Init(SDL_INIT_EVERYTHING)<0){
cout << "error starting sdl" << endl;
}
return 0;
}
This the build log:
-------------- Build: Debug in Graphics (compiler: GNU GCC Compiler)---------------
mingw32-g++.exe -Wall -g -std=c++11 -IC:\Users\73638G75MA\Documents\SDL2-2.0.3\x86_64-w64-mingw32\include\SDL2 -I"C:\Users\73638G75MA\Documents\C++ projects\Graphics" -c "C:\Users\73638G75MA\Documents\C++ projects\Graphics\main.cpp" -o obj\Debug\main.o
mingw32-g++.exe -LC:\Users\73638G75MA\Documents\SDL2-2.0.3\x86_64-w64-mingw32\lib -o bin\Debug\Graphics.exe obj\Debug\main.o -lmingw32 -lSDL2main -lSDL2
obj\Debug\main.o: In function `main':
C:/Users/73638G75MA/Documents/C++ projects/Graphics/main.cpp:8: undefined reference to `SDL_Init'
collect2.exe: error: ld returned 1 exit status
Process terminated with status 1 (0 minute(s), 0 second(s))
1 error(s), 0 warning(s) (0 minute(s), 0 second(s))
I would apreciate all possible help in this, the #undef main at the start is because it won't let me run it otherwise. If it isn't there it gives me a "undefined reference to winmain@16" While I am creating a console application.
According to the include and library search paths ...\SDL2-2.0.3\x86_64-w64-mingw32\...
, you're trying to build with a 64-bit SDL2. Judging from the compiler name According to the Code::Blocks download page and my inspection of the contents of codeblocks-13.12mingw-setup.exe, the included toolchain is 32-bit only and can't create 64-bit binaries nor use 64-bit libraries.mingw32-g++
, I'd say you're using the mingw.org toolchain
If you want to use a pre-built SDL2, you either need to download the matching toolchain (64-bit mingw-w64) and use that, or change your build parameters to use the 32-bit build of SDL2 (it's present in the development libraries archive in the i686-w64-mingw32
directory).
You have several errors here.
1.
Don't undef main just because you don't know why it's here! When you're using SDL, main()
(almost) always must look like int main(int, char **) {}
.
So, remove #undef main
and change int main()
to int main(int, char **)
.
2.
You must use contents of i686-w64-mingw32
instead of x86_64-w64-mingw32
when building x32 executables. So, adjust your compilation flags properly.
x86_64-w64-mingw32
is probably for x64 applications, but I think you're building x32 one.
g++ -std=c++17 Test.cpp -I"include" -L"lib" -Wall -lmingw32 -lSDL2main -lSDL2 -lSDL2_image -o Test
Some things you might not have realized that are important:
int main(int argc, char* argv[]) {
because SDL needs to find it to take over.My file structure looks like this:
Test/
include/
SDL.h
SDL_main.h
SDL_image.h
...
lib/
libSDL2.a
libSDL2.dll.a
libSDL2_image.a
...
licenses/
Test.exe
SDL2.dll
SDL2_image.dll
zlib1.dll
Test.cpp
sdl2-config
Note that I have my command line's current directory at Test.
Also note that I have the mingw-w64 g++ executable's folder on the environment variables path.
Also also note that the ...s in the structure above mean there are more that should be in there and they come from the x86_64 folder given to you by SDL.
in terminal or command prompt use bellow code
gcc main.c -lSDL2 -lSDL2main -o main
"main.c" is name of your file
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With