Fail to use SDL with MinGW on windows - c++

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!

Related

Embedding SpiderMonkey JS

I'm working on a C++ application. I would like to embed SpiderMonkey in the application.
I'm working with CMake, but I couldn't get that build. So, in an attempt to reduce complications, I tried the example code on this page. This wouldn't link using cmake or gcc from the command line.
So, even simpler, just to ensure I can link properly I am trying to get the following to work.
From the command line with gcc:
g++ --std=c++11
-I/home/thetasinner/moz/js/src/build_DBG.OBJ/dist/include
-L/home/thetasinner/moz/js/src/build_DBG.OBJ/js/src -DDEBUG
-Wl,--verbose -lmozjs-54a1 -lm -lz -ldl test.cpp -o test
On the following minimal code example:
#include <iostream>
#include <stdexcept>
#include "jsapi.h"
#include "js/Initialization.h"
int main(int argc, char** args) {
if (!JS_Init()) {
throw std::runtime_error("failed to initialise.");
}
std::cout << "It's alive!\n";
JS_ShutDown();
return 0;
}
Even this doesn't link. I get the errors
/tmp/ccqjx5RY.o: In function `main':
custom.cpp:(.text+0xf2): undefined reference to `JS_ShutDown()'
/tmp/ccqjx5RY.o: In function `JS_Init()':
custom.cpp:(.text._Z7JS_Initv[_Z7JS_Initv]+0xa): undefined reference to
'JS::detail::InitWithFailureDiagnostic(bool)'
collect2: error: ld returned 1 exit status
The headers are found and the linker is finding the mozjs library
attempt to open /home/thetasinner/moz/js/src/custom_build_DBG.OBJ/js/src
/libmozjs-54a1.so succeeded
-lmozjs-54a1 (/home/thetasinner/moz/js/src/custom_build_DBG.OBJ/js/src
/libmozjs-54a1.so)
I am working on Linux (Ubuntu 16.04 and Debian 8.7 tried) because that's where the build tools are. I don't even want to touch Window's yet.
The 'js' executable built in the spidermonkey build works fine, which I assume has the lib I'm trying to link inside it. So I would have thought the lib itself is built okay.
Can anybody help me resolve these linker errors? There are lots of answers to questions about much older versions of SpiderMonkey, but there's nothing for more recent versions. I'm interested in version 45 (which I've tried with very similar errors) or the tip version 52.
I'm comfortable enough digging around in the code working out how to do what I want with it once it builds, hence the interest in the latest version which isn't properly documented, I'm just completely stumped with the building step.
I suspect it's just an ordering problem on the command line:
g++ --std=c++11
-I/home/thetasinner/moz/js/src/build_DBG.OBJ/dist/include
-L/home/thetasinner/moz/js/src/build_DBG.OBJ/js/src -DDEBUG
test.cpp -o test
-Wl,--verbose -lmozjs-54a1 -lm -lz -ldl
Compilation first, followed by linking, with libraries in order of dependency. (My first guess was you'd neglected to mention mozjs on the command line. It took a second look to see it was just in the wrong place.)

Undefined reference to boost::random::random_device constructor and destructor on MinGW-w64 gcc

My OS is Windows 7 64-bit and C++ compiler I'm using is:
g++ (i686-posix-dwarf-rev0, Built by MinGW-W64 project) 5.3.0
And I installed Boost version 1.60 using:
bootstrap.bat mingw
b2 install target=gcc
Then I tested is it working, using examples from Boost.Random tutorial.
With the first two everything was fine, but the third one gave linker errors about boost::random::random_device. I minimized the code to have only this:
// Compiled with:
// g++ -IC:/Boost/include/boost-1_60
// -LC:/Boost/lib -lboost_random-mgw53-mt-1_60
// main.cpp
#include "boost/random/random_device.hpp"
int main() {
boost::random::random_device rng;
}
And I get the following errors:
C:\Users\Daniel\AppData\Local\Temp\cc5DfdjZ.o:main.cpp:(.text+0x15):
undefined reference to `boost::random::random_device::random_device()'
C:\Users\Daniel\AppData\Local\Temp\cc5DfdjZ.o:main.cpp:(.text+0x20):
undefined reference to `boost::random::random_device::~random_device()'
collect2.exe: error: ld returned 1 exit status
Here, on SO, I found that someone with similar problem added -lboost_system to flags, but for me it didn't helped.
Does anyone have any idea, why it isn't working? I checked, and I have random_device.hpp header in my Boost folder, with declarations of random_device() and ~random_device() in it.
I found what was wrong - the g++ command syntax, that I wanted to use to compile and link my code.
As I wrote in my question, I do this that way:
g++ -IC:/Boost/include/boost-1_60 -LC:/Boost/lib -lboost_random-mgw53-mt-1_60 main.cpp
While the correct one is with main.cpp (or any other source code file(s), that we want to include in compiling process) before the -L and -l flags.
For example:
g++ -IC:/Boost/include/boost-1_60 main.cpp -LC:/Boost/lib -lboost_random-mgw53-mt-1_60
or even
g++ main.cpp -IC:/Boost/include/boost-1_60 -LC:/Boost/lib -lboost_random-mgw53-mt-1_60
Hope it will help anyone, who will make such silly mistake too.

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.

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.