C++ SDL2 - Incomprehensible link error - c++

I setup SDL2 under C++ CDT Eclipse.
I added the include path "........SDL2-2.0.3\i686-w64-mingw32\include", the library path "........SDL2-2.0.3\i686-w64-mingw32\lib" and added the libraries 1."mingw32" 2."SDL2main" 3."SDL2".
So now, if I add a main.cpp with this content:
#include <SDL.h>
int main(int argc, char* args[])
{
return 0;
}
I can build the project fine, but if I use this:
#include <SDL.h>
int main()
{
return 0;
}
The project can't build and I get this error:
Info: Internal Builder is used for build g++ "-IO:\Eclipse CDT
Workspace\SDL OpenGL Lab\Libraries\SDL2\include\SDL2" -O0 -g3
-Wall -c -fmessage-length=0 -o main.o "..\main.cpp" g++ "-LO:\Eclipse CDT Workspace\SDL OpenGL Lab\Libraries\SDL2\lib" -o
"SDL OpenGL Lab.exe" main.o -lmingw32 -lSDL2main -lSDL2 O:\Eclipse
CDT Workspace\SDL OpenGL
Lab\Libraries\SDL2\lib/libSDL2main.a(SDL_windows_main.o): In function
console_main':
/Users/slouken/release/SDL/SDL2-2.0.3-source/foo-x86/../src/main/windows/SDL_windows_main.c:140:
undefined reference toSDL_main' collect2.exe: error: ld returned 1
exit status
I simply wonder me why this error is depending on my main method, I want to use the main method I want.
Why this is so, how I can fix this ?

Sorry, you're stuck with it. I also preferred a simpler main, but SDL doesn't support it.
Here's a little more on SDL and main in the Windows world. It doesn't say why you need their version of main -- but you do.
I get "Undefined reference to 'SDL_main'" ...
Make sure that you are declaring main() as:
#include "SDL.h"
int main(int argc, char *argv[])
https://wiki.libsdl.org/FAQWindows

I had the same problem and had to go through a lot of manipulations.
What I have found is the simple line #define SDL_MAIN_HANDLED before calling SDL.h. SDL has his own main that's why, I think.
For example, in my .h files, I write everytime this :
#include <stdio.h>
#include <stdlib.h>
#define SDL_MAIN_HANDLED
#include <SDL.h>
#include <SDL_image.h>
#include <SDL_ttf.h>

Related

include botan 2 in compilation

i try include some botan header in compilation process
#include <botan/rng.h>
#include <botan/auto_rng.h>
#include <botan/cipher_mode.h>
#include <botan/hex.h>
#include <iostream>
int main(int argc, char** argv)
{
return 0;
}
I found that i need to compile with following command in order to build successfully
g++ app.cpp -I/usr/local/include/botan-2
i saw some folks execute
g++ app.cpp -lbotan-2
i tried it out but i get a error
'app.cpp:1:10: fatal error: botan/rng.h: No such file or directory
#include <botan/rng.h>
Am i missing anything ?
The following command:
g++ app.cpp -lbotan-2
links botan-2 with app.cpp but you still need to specify where to find the headers:
g++ app.cpp -I/usr/local/include/botan-2 -lbotan-2
Under my system, the headers for botan-2 are in /usr/include/botan-2. So, make sure you are giving the right path.

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)

unidentified reference to glutInit

I tried to compile a basic OpenGL program, simply just a blank window.
I'm using CodeLite with the g++ compiler on Linux Mint 18.1 (Ubuntu 16.04 LTS).
The code so far is:
#include <stdio.h>
#include <GL/glew.h>
#include <GL/freeglut.h>
int main(int argc, char **argv)
{
glutInit(&argc, argv);
printf("hello world\n");
return 0;
}
At first my compiler (G++ in CodeLite) gave the error
/home/USER/Projects/CodeLite/Graphical/main.cpp:7: undefined reference to `glutInit'
I downloaded all the up to date GLEW and GLUT includes from their respective websites and unpacked them to /usr/include/GL, I edited the project settings linker like this.
Now it gives the error message:
/usr/bin/ld: cannot find -lglut
Makefile:4: recipe for target 'All' failed
What can I do to fix this?
You should install the libraries from the package manager instead: libglew-dev, freeglut3-dev, libgl1-mesa-dev and libglu1-mesa-dev. The includes and binary files will be placed in the appropriate location.
If the linker cannot find the .so files, locate them with dpkg -L freeglut3-dev and add this directory in the linker command line
g++ -L/path/to/libglut.so *.o -o programname

Linking OpenGL using MinGW on Windows x86_64

I would like to develope an application using C++ and OpenGL for Windows 64bit.
I am using the following Compiler x86_64-w64-mingw32-g++.
The following code snippet (Test.cpp) is sufficient to trigger the error I get:
#include <GL/gl.h>
int main(int argn, char **argv) {
glClear(GL_COLOR_BUFFER_BIT);
}
(I know this code is meaningless, but it is sufficient to trigger the error during linking.)
I use the following Makefile:
Test:
g++ -lopengl32 -o Test Test.cpp
This yields the following error:
undefined reference to `__imp_glClear'
I have no clue what I am missing, and would be very thankfull for any advice.
For me the g++ main.cpp -o run.exe -lopengl32 seems to work just fine, so you most likely wont need -Wl,--enable-stdcall-fixup to compile it.

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

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.