I can compile with SDL1.2 but not with SDL2 (C::B) - sdl

I've been learning SDL for a little time, and now I've decided to try out SDL2, mainly to try its hardware acceleration. But the problem is, I can't compile it at all, while the same code compiled correctly with SDL1.2.
The sample code is:
#include "SDL/SDL.h"
int main( int argc, char *args[] )
{
SDL_Init(SDL_INIT_EVERYTHING);
SDL_Quit();
return 0;
}
With the original linker settings: -lmingw32 -lSDLmain -lSDL
everything compiles.
But as soon as I change #include "SDL/SDL.h" to #include "SDL2/SDL.h"
and change linker settings to
-lmingw32 -lSDL2main -lSDL2
I get the errors:
obj\Debug\main.o||In function `SDL_main':|
main.cpp|5|undefined reference to `SDL_Init'|
main.cpp|8|undefined reference to `SDL_Quit'|
libmingw32.a(main.o):main.c:(.text.startup+0xa7)||undefined reference to `WinMain#16'|
I've got SDL1.2 installed in C:/SDL-1.2.15 and SDL2 installed in C:/SDL2
In search directories, I added both SDL1.2 and SDL2 Include and Lib folders.

I'm not sure if this will work, but if you are using the "x86_64-w64-mingw32" folder, try using the other one (the one with i686) this helped me. I was having the EXACT same problem as you, and using literally, to the line, the same test code as you. I hope this helps.

Related

C++ Include path on x86_64-w64-mingw32-g++ with OpenGL

I am trying to get an OpenGL program working on both linux and windows.
Here's my code [file=main.cc]:
#include <iostream>
#include "GL/glew.h"
using namespace std;
int main(int argc, char *argv[]){
cout << "Hello World\n";
return 0;
}
Simple enough. I'm on Linux and using
g++ main.cc -lGL -lGLEW -lSDL2
to compile my program. It works perfectly fine and if I run ./a.out I get a Hello World on my screen.
Then I try to compile it on Linux for Windows using the command
x86_64-w64-mingw32-g++ main.cc -lGL -LGLEW -LSDL2
Then however i get the error:
main.cc:3:21: fatal error: GL/glew.h: No such file or directory
#include "GL/glew.h"
^
compilation terminated.
I've already tried adding the -I/inclulde/path option with paths like /usr/include /usr/include/GL usr/include and the like, yet nothing wants to compile.
The Libraries that I'm using (or planning to) were installed using
#apt install libgl-dev libglew-dev libsdl2-dev
Any help would be very much appreciated (although I feel this is an incredibly easy fix that I'm just too stupid to figure out on my own)

Fail to use SDL with MinGW on windows

I wanted to start learning to program with SDL. So I download x86 for windows,
put all the lib and the include in MinGW. But when I compile it doesn't know the SDL functions exist.
# define SDL_MAIN_HANDLED // somehow it want it to not define its own main
#include <iostream>
#include <sdl2/SDL.h>
using namespace std;
int main( int argc, char* argv[] ) {
SDL_SetMainReady(); // just for check
return 0;
}
I read that the linking need to be in specific order (mingw32, SDL2main and then libSDL2), But I think Eclipse run a wrong compiling command.
The eclipse command:
g++ -O0 -g3 -Wall -c -fmessage-length=0 -o "src\\fire.o" "..\\src\\fire.cpp"
g++ -o fire.exe "src\\fire.o" -lmingw32 -lSDL2main -llibSDL2
src\fire.o: In function `main':
C:\Or\C++\Projects\fire\Debug/../src/fire.cpp:16: undefined reference to `SDL_SetMainReady'
collect2.exe: error: ld returned 1 exit status
Do you think I miss something?
I think you need to change -llibSDL2 to -lSDL2.
Ok I solve it. I'm not sure if the problem was lack of support on 32 bit or the fact that minGW and SDL were from different compilers that probably didn't match..
But what I did is to delete minGW from my pc and download WinBuild. WinBuild its a download manager that offer a lot of libs and tools, include minGW64 bit and SDL.
The advantage is that they were all compile from the same compiler with the same configurations.
after that i change the path to minGW in to the new 64 bit path inside WinBuild folder, add g++ from Winbuild the to the path as well and restart.
Then, adding and linking work without any problem!
I still need to put # define SDL_MAIN_HANDLED on the start to make it work, but its work!

Undefined reference errors when linking GLFW on MinGW

I am trying to develop an openGL application with GLEW and GLFW on Windows using minGW. In the current directory, project/, I have the directories src/, bin/, and glfw-3.0.4.bin.WIN64/. I have the files test.cpp, glew.h, glew.c, and wglew.h in the src/ directory.
The directory ./glfw-3.0.4.bin.WIN64/include/ contains the GLFW/glfw3.h header file.
The directory ./glfw-3.0.4.bin.WIN64/lib-mingw/ contains glfw3.dll, glfw3dll.a, and libglfw3.a.
My main file, test.cpp contains,
#include "glew.h"
#include "GLFW/glfw3.h"
#include <stdio.h>
int main(int argc, char** argv) {
printf("Hello, World!\n");
glewInit();
glfwInit();
}
I am compiling the program from the project/ directory by running (split into two lines for readability)
gcc -DGLEW_STATIC -DGLFW_DLL -o ./bin/test ./src/*.cpp ./src/glew.c
-I ./glfw-3.0.4.bin.WIN64/include/ -L ./glfw-3.0.4.bin.WIN64/lib-mingw/ -lglfw3 -lopengl32
and I am getting the following error:
undefined reference to `_imp_glfwInit'
I think the problem has to do with me linking the GLFW library incorrectly. From what I understand, including the compiler option -lglfw3 will tell gcc to link ./glfw-3.0.4.bin.WIN64/lib-mingw/glfw3.dll, which contains the definition for glfwInit().
I've looked at solutions to other problems similar to mine and they suggest things such as copying the dll file to the source/binary directories and changing the order of the -l options, but none have seemed to solve the problem for me.
Your problem is that gcc follows strict library naming conventions. It attempts to find glfw3.dll.a, but finds none (because it is named glfw3dll.a - simple rename will fix your problem).
Next step it looks for libglfw3.a, and succeeds - but it is a static library, while reference declared as dynamic in header files (tricky windows DECLSPECs... this problem don't exist on e.g. linux). So, it cannot find _imp__glfwInit, because in static library it is called just glfwInit, so you getting an error.
Removing libglfw3.a is also one of options - in that case gcc will look further and eventually find glfw3.dll and use it.

mingw and SDL command line

Now I see that this is not a unique problem and has been raised plenty of times before, but I have followed the advice given in other stack overflow questions and nothing seems to help.
My problem is pretty straight forward, I can not compile my project (a basic c++ gui) and include SDL, because I get: Undefined reference to WinMain#16
I started using code::blocks but it didn't work so moved to trying to compile the simplest implementation on the command line in windows 7, 64 bit, in an attempt to understand whats going on in the background.
The command I am running:
g++ test.cpp -L C:\Projects\C++\tester\SDL\lib -lmingw32 -LSDLmain -LSDL -mwindows
I had a weird issue with the -l parameter in that I had added in a path to the SDL includes folder, C:\Projects\C++\tester\SDL\includes but if I do it chucks a error saying it cannot find this directory, obviously I checked and rechecked the path to make sure it was correct, but leaving it out removes the error. Most likely the cause of my problem now I thing about it.
The answer in this question is pretty comprehensive and helps understand the problem further
but seems to suggest that the issue is that there is no main function defined, that seems to make sense, but doesn't SDL redefine main to SDL_main in SDL_main.h?
#define main SDL_main
I'd like to point out I am a c++ noob.
#include <string.h>
#include "SDL\include\SDL.h"
#include <iostream>
int main(int argc, char*argv[])
{
return 0;
}
Ok I spotted your error.
As you probably know, -L specifies link path while -l specifies the library to be linked. Your command says:
-L C:\Projects\C++\tester\SDL\lib -lmingw32 -LSDLmain -LSDL -mwindows
^^ ^^
Which mistakenly uses -L instead of -l. You meant to write:
-L C:\Projects\C++\tester\SDL\lib -lmingw32 -lSDLmain -lSDL -mwindows
Edit: regarding libSDL2.
First of all, you need to include <SDL2/SDL.h>. You also need to link against -lSDL2main and -lSDL2.
But the main problem here is that you are linking against -lmingw32, which expects a 32-bit architecture, while you are building and linking against the 64-bit version of libSDL2. If you build and link against the 32-bit version of libSDL2, all would be ok.
If your MinGW installation is 32-bits, then you are stuck with 32-bit builds and you need to use 32-bit libraries. If it is 64-bits, -lmingw32 needs to be replaced. Perhaps with -lmingw64, but I don't have a 64-bit MinGW installed to test it and I don't know if -lmingw64 actually exists or not.

Issues compiling SDL with g++

When I try compiling the file
#include "SDL2\SDL.h"
int main( int argc, char* argv[] )
{
//Start SDL
SDL_Init( SDL_INIT_EVERYTHING );
//Quit SDL
SDL_Quit();
return 0;
}
--an example from the SDL wiki--on the command line with
g++ -o SDL_Test.exe SDL_Test.cpp -lmingw32 -lSDLmain -lSDL
I get an error that says my computer cannot find -lSDLmain and -lSDL. Without the library things, it has trouble interpreting the functions.
I think the problem has something to do with the -l things, but I have no idea what they do....
My computer is 64-bit, if that makes a difference. I copied the x84_64-mingw32 bin, include, and lib into the C:\MinGW versions.
I would mostly just like an explicite solution to my problem, but if you could try explaining the -l things and other - things, that would be awesome.
-::- Answered by greatwolf in chat: turns out I was using 32-bit MinGW with 64-bit SDL files; I just needed to replace the 64-bit files with the 32-bit versions. Also, I had to direct the compiler to my SDL2.dll file. End compile command was g++ -o SDL_Test.exe SDL_Test.cpp -LC:\MinGW\lib -lmingw32 -lSDL2main C:\MinGW\bin\SDL2.dll.
You've got to make sure that you specify the appropriate search path for libraries using the -L compiler flag. Otherwise it won't know where to look for linking.