mingw and SDL command line - c++

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.

Related

Error: 'GL/glfw3.h: No such file or directory' when compiling C++ programs using OpenGL on Linux

I receive the error message
GL/glfw3.h: No such file or directory
when I try to compile the example program given in the tutorial here, section 'Opening a window'. I have installed all the libraries they referred to in the 'Building on Linux' section. (My distribution is Ubuntu 16.04.)
I have also successfully run
apt-get install libglfw-dev
as I found suggested somewhere as an answer to this issue.
I think the glfw3 library has possibly been installed in a place that the compiler does not know how to automatically access.
How do I find out if this is the case, where should I put it so it can be accessed (/usr/share?), and what exactly do I put there? I've copied a file called libglfw3.a that I found in /usr/local/lib to /usr/share, but the error reoccurred.
I've tried compiling it using
g++ first.c -o first
and
g++ first.c -lglut -lGL -lGLEW -lglfw -o first. That error message alone occurred both times (which makes me think the other libraries don't even need to be linked?)
I'm a pretty new user. The solution is probably obvious.
Easy fix (the solution was indeed obvious).
#include <GL/glfw3.h> should be #include <GLFW/glfw3.h>

SDL2 error messages won't go away

Compile command/what's in the .bat:
g++ -o program.exe SME.cpp -w -Wl -subsystem,windows -lmingw32 -lSDL2main -lSDL2
pause >nul
SME.cpp:
#include "SDL2\SDL.h"
int main()
{
SDL_Init(SDL_INIT_EVERYTHING);
SDL_Quit();
return 0;
}
I have build.bat, SME.cpp and SDL2.dll in a folder
MinGW is in the default C:\MinGW directory
I installed MinGW using the installer
I added C:\MinGW\include
I put the SDL2 header files in C:\MinGW\include\SDL2\
I put the SDL2 library files (SDL2main.lib and SDL2.lib) in C:\MinGW\lib.
I added C:\MinGW\bin to the Path variable
I've solved all of the previous problems by looking on the internet for answers to them
The compiler keeps complaining that there is an undefined reference to SDL_Init and SDL_Quit and that ld.exe (I have no idea what that is) has returned exit status 1 due to an undefined reference to #WinMain16.
I've searched everywhere and there is a short supply of answers to this problem and all of the answers I've come across haven't worked. I've tried 3 different ways of doing this: with a .bat, with a Makefile and with Eclipse. It always comes down to these three errors. I have no idea what is going wrong, I checked and SDL_init.h and SDL_quit.h both exist. I don't know what #WinMain16 is either.

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!

Cannot find .dll using MinGW and MSYS

OS: Windows 7
I have the source, as well as all the library files I'm using in one directory, on my desktop. I'm running the shell by using msys.bat, which was created when I installed MinGW. I've tried to run the following (and many others):
g++ -I. -L. -o opengltest.exe opengltest.cpp -lglew32 -lglew32s -lglew32.dll
I recieve the following error:
c:/mingw/bin/../lib/gcc/mingw32/4.6.2/../../../../mingw32/bin/ld.exe: cannot find -lglew32.dll
collect2: ld returned 1 exit status
This also happens when I use the -L switch and the entire C:/Users/... path, but I get the same error. Again, glew32.dll is in the same directory that the .cpp is in, which is the same directory I'm working in within the shell. I've tried multiple solutions from multiple posts, and it still seems like I'm missing something. I thought using the -L. was a straightforward way to tell MinGW to look in the working directory, but apparently it doesn't work that way.
Do not link against both the dynamic and static linking version of glew in the same application. This makes no sense. Also do no link against glew32.dll, this makes equally little sense.
Most importantly, do not use the DLL version of glew with g++ at all - it will not work (see one of my previous answers to understand why). Instead, #define GLEW_STATIC (better if you use -DGLEW_STATIC as a compiler switch) and only link to glew32s.

Static-linking of SDL2 libraries

I am using Windows 7, Code::Blocks and MinGW. I have little to no experience when it comes to compiling/building anything, especially when Code::Blocks doesn't use makefiles.
I downloaded SDL2-devel-2.0.0-mingw.tar.gz (SDL Development Libraries) from http://www.libsdl.org/tmp/download-2.0.php, and I'd like to create a standalone executable using SDL2 libraries, but so far I've always had to bundle the SDL2.dll file with the executable to make it work.
I've heard that I can not static-link dynamic libraries, so my only option seems to be doing something with the source files using the file SDL2-2.0.0.tar.gz (Source Code) from the link I mentioned above. However, I do not know what I should do with those.
What I managed to try with the source files is importing the Visual Studio project to Code::Blocks and building it, but it tells me "sdl-config No such file or directory" (I do not know what triggered that). I'm also not sure if building merely gives me an executable, with which I do not know what I can do to link it to my own executable.
A fool proof idiot's step by step guide would be the best bet to solve this case.
EDIT:
I managed to compile the SDL libraries with the guide Jonas provided, and got a libSDL2.a file.
At first I only added the path of libSDL2.a to "Link libraries:" -section of Code::Blocks, but I got a bunch of errors such as "SDL_Init() not declared in this scope".
In addition to the libSDL2.a path, I also added the path of SDL2-2.0.0\include to the Compiler's search directory as well as the path of SDL2-2.0.0\build.libs to the Linker's search directory. I also wrote this to my test file: #include "SDL.h". My test file now looks like this:
#include "SDL.h"
int main( int argc, char* args[] ) {
//Start SDL
SDL_Init( SDL_INIT_EVERYTHING );
//Quit SDL
SDL_Quit();
return 0;
}
It appears it did fix the declaration problem, but now Code::Blocks opened a SDL_mmjoystick.c file and gave me even more errors: "undefined reference to 'waveInClose#4'", "undefined reference to 'waveOutClose#4'", "undefined reference to 'joyGetNumDevs#0'" and tons of other ones.
Here's a screenshot of what's happening, note the different color of #include texts, I'm not sure why that happens: http://gyazo.com/00656a9c1e57a2bd0db1414fa7d68ced.png
I am not sure how to correctly take this library into use. Any help in this case, or should I make another question for it?
EDIT:
I added -lSDL2 to the linker options and deleted the other parameters. Now it builds fine:
mingw32-g++.exe -Wall -fexceptions -g -IC:\Users\User\Desktop\SDL2-2.0.0\include -c "C:\Users\User\Desktop\CppProjects\SDL project\main.cpp" -o obj\Debug\main.o
mingw32-g++.exe -Wall -fexceptions -g -IC:\Users\User\Desktop\SDL2-2.0.0\include -c "C:\Users\User\Desktop\CppProjects\SDL project\thetestfile.cpp" -o obj\Debug\thetestfile.o
mingw32-g++.exe -LC:\Users\User\Desktop\SDL2-2.0.0\build\.libs -o "bin\Debug\SDL project.exe" obj\Debug\main.o obj\Debug\thetestfile.o -lSDL2 ..\..\SDL2-2.0.0\build\.libs\libSDL2.a C:\Users\User\Desktop\SDL2-2.0.0\build\.libs\libSDL2.a -mwindows
Output size is 945.80 KB
Process terminated with status 0 (0 minutes, 1 seconds)
0 errors, 0 warnings (0 minutes, 1 seconds)
But when I try to run it, it says my computer lacks SDL2.dll, while the whole point was to static-link.
So currently I have the path to build/.libs in my Link libraries -settings, -lSDL2 in the Other linker options, and for search directories I have the path to SDL2-2.0.0/include for the compiler and SDL2-2.0.0/build/.libs for the linker.
In the build/.libs directory I can also see libSDL2.a, libSDL2.dll.a, libSDL2.la and libSDL2.lai files, which I don't know what they are.
It's not necessary to recompile the library,
SDL2 is given with static-link library named "libSDL2.a"
on the folder "SDL2-2.0.0\i686-w64-mingw32\lib\".
Just be sure to add these options to the linker :
"-lmingw32 -lSDL2main -lSDL2 -mwindows -lm -ldinput8 -ldxguid -ldxerr8 -luser32 -lgdi32 -lwinmm -limm32 -lole32 -loleaut32 -lshell32 -lversion -luuid -static-libgcc"
on Code:Blocks at "Project / Build Options... / Linket settings / Other linker options"
These options allow you to link with what SDL2.dll was using.
You can retreive them on the file "SDL2-2.0.0\i686-w64-mingw32\bin\sdl2-config"
The magical trick is to delete or rename the file "libSDL2.dll.a"
on the folder "SDL2-2.0.0\i686-w64-mingw32\lib\".
I added a "-" before to keep it in case I need it.
I don't know why this librairy overcomes the other and a clue would be appreciated.
I tried with Code::Blocks 12.11 MinGW32 and it worked.
If you run with some projects that use dynamic-link
and some other which use static-link, you will have to
keep your librairies in two different folders knowing that
"libSDL2main.a" will be in those two.
Sorry for my writing, I'm not used to write in english.
Mike