multiple definition of `main' working with SDL_main - c++

Basically im trying to create a game with SDL2 and Opengl, but I cant get passed this error. I've tried changing SDL_main(int argc, char *argv[]) to regular main but nothing changes. I know this error is due to the SDL_main library but I don't know how to fix it. I've been searching info for some hours now but can't figure it out, the only thing related to this problem I've found is this post, which didn't fix my problem.
Common.h
#ifndef _COMMON_H
#define _COMMON_H
/*=======STANDARD TEMPLATE LIBRARY=======*/
#include <iostream>
#include <string>
#include <cstdlib>
#include <memory>
#include <ctime>
#include <vector>
using namespace std;
/*=======GLU/GLEW=======*/
#include <GL/glew.h>
#include <GL/glu.h>
#include <GL/gl.h>
/*=======SDL2=======*/
#include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h>
#include <SDL2/SDL_image.h>
/*=======CLASS DEFINITIONS=======*/
#endif
Main.cpp
#include "common.h"
bool isRunning = true;
int SDL_main(int argc, char *argv[])
{
//Error Checking/Initialisation
if (SDL_Init(SDL_INIT_NOPARACHUTE & SDL_INIT_EVERYTHING) != 0) {
SDL_Log("Unable to initialize SDL: %s\n", SDL_GetError());
return -1;
}
else
{
//Specify OpenGL Version (4.2)
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
SDL_Log("SDL Initialised");
}
//Create Window Instance
SDL_Window* window = SDL_CreateWindow(
"Game Engine",
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
640,
480,
SDL_WINDOW_OPENGL
);
//Check that the window was succesfully created
if(window == NULL)
{
//Print error, if null
printf("Could not create window: %s\n", SDL_GetError());
return 1;
}
else
SDL_Log("Window Successful Generated");
//Map OpenGL Context to Window
SDL_GLContext glContext = SDL_GL_CreateContext(window);
//Render
//Swap Render Buffers
SDL_GL_SwapWindow(window);
//Free up resources
SDL_GL_DeleteContext(glContext);
SDL_Quit();
return 0;
}
Error output
./src/lib/SDL2main.lib(x64/Release/SDL_windows_main.obj):(.text[main]+0x0): multiple definition of `main'
C:/Program Files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/lib/../lib/libmingw32.a(lib64_libmingw32_a-crt0_c.o):crt0_c.c:(.text.startup+0x0): first defined here
Makefile
all:
g++ -I ./src/include -L ./src/lib -o ./Builds/Win_Build/engine main.cpp -lmingw32 -lSDL2main -lSDL2 -lmingw32 -lopengl32 -lglew32 -lSDL2main -lSDL2 -lSDL2_image -lSDL2_ttf --verbose
Lib folder
EDIT:
I fixed it by redownloading the libraries and using the .a extension ones. And also I changed the version of Glew to 64 bits. My new lib folder:

Related

SDL2 and KISS_SDL trouble linking on macOS

I am having trouble installing and linking KISS_SDL into my C++ project.
I am running macOS v11.2.3 and have installed SDL2, SDL2_image and SDL2_ttf with brew.
Below is the code in main.cpp that will open a simple SDL2 window:
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_ttf.h>
#include <iostream>
#include "kiss_sdl.h"
int main()
{
if(SDL_Init(SDL_INIT_VIDEO) < 0)
{
std::cout << "Failed to initialize the SDL2 library\n";
return -1;
}
SDL_Window * window = SDL_CreateWindow("SDL2 Window",
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
680, 480,
SDL_WINDOW_SHOWN);
if(!window)
{
std::cout << "Failed to create window\n";
return -1;
}
SDL_Surface * window_surface = SDL_GetWindowSurface(window);
if(!window_surface)
{
std::cout << "Failed to get the surface from the window\n";
return -1;
}
SDL_UpdateWindowSurface(window);
bool isquit = false;
SDL_Event event;
while (!isquit) {
if (SDL_PollEvent( & event)) {
if (event.type == SDL_QUIT) {
isquit = true;
}
}
}
}
Using the terminal command below I attempt to compile and link the required libraries.
$ g++ -Wall -o main main.cpp -lSDL2 -lSDL2_image -lSDL2_ttf
When I compile without the "#include "kiss_sdl.h"" The program compiles and executes as expected. When I add this include I am getting the following error:
In file included from main.cpp:6:
./kiss_sdl.h:34:10: fatal error: 'SDL.h' file not found
#include <SDL.h>
^~~~~~~
1 error generated.
Here is the includes from kiss_sdl.h, I have added a comment in to line 34 where the code is not working:
#ifndef _kiss_sdl_h
#define _kiss_sdl_h
#ifndef RESDIR
#define RESDIR ""
#endif
#if defined(_MSC_VER)
#include <SDL.h>
#include <SDL_ttf.h>
#include <SDL_image.h>
#elif defined(__APPLE__)
#include <SDL.h> /* Line 34 */
#include <SDL_ttf.h>
#include <SDL_image.h>
#else
#include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h>
#include <SDL2/SDL_image.h>
#endif
#include <sys/types.h>
#ifdef _MSC_VER
#include <direct.h>
#include <io.h>
#else
#include <unistd.h>
#include <dirent.h>
#endif
#include <sys/stat.h>
#include <string.h>
#include <stdio.h>
Please look at the issues in the project https://github.com/actsl/kiss_sdl , issue #21 was started based on this. In brief, this is building using a command line only, without using makefile. But the g++ command line that was used is wrong. In Mac, the headers and libraries are provided by framework, but this is missing in the command line. Because of the framework there is also no need to specify SDL2 directory. This has worked for a number of people using Mac, and the framework is specified in the makefile. This was a short answer, it is going to be tested in Mac, but the person doing that is likely busy and has not enough time for that right now.

Undefined reference when using glew and mingw?

I am using Eclipse, I had originally downloaded the binary from the website until someone pointed out that I needed to build it from source to make it work with mingw, so I did and I got these files: glew32.dll, libglew32.a, and libglew32.dll.a
I dropped the glew32.dll into the debug folder, and linked the libraries but it did not work.
The weird part: GLenum status = glewInit(); works but glClearColorand glClear do not work and I get a undefined reference to error when I try to call them.
Please see these screenshots: http://imgur.com/a/L8iNb and http://imgur.com/a/nYoWD
C++.cpp
#include <iostream>
#include "classHeaders\display.h"
#include "GL\glew.h"
int main(int argv, char** args){
display x(800,600,"something");
while(!x.isClosed()){
glClearColor(0.0f,0.15f,0.3f,1.0f); //undefined reference to ERROR here
glClear(GL_COLOR_BUFFER_BIT); //undefined reference to ERROR here
x.Update();
}
return 0;
}
display.cpp
#include "classHeaders\display.h"
#include "GL\glew.h"
#include <iostream>
display::display(int width, int height, const std::string& title){
SDL_Init(SDL_INIT_EVERYTHING);
SDL_GL_SetAttribute(SDL_GL_RED_SIZE,8);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE,8);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE,8);
SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE,8);
SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE,32);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER,1);
m_window = SDL_CreateWindow(title.c_str(),SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,width,height, SDL_WINDOW_OPENGL);
m_glContext = SDL_GL_CreateContext(m_window);
GLenum status = glewInit(); //NO ERRORS OCCUR
if(status != GLEW_OK){
std::cerr << "glew failed to initialize" << std::endl;
}
m_isClosed = false;
}
display::~display(){
SDL_GL_DeleteContext(m_glContext);
SDL_DestroyWindow(m_window);
SDL_Quit();
}
bool display::isClosed(){
return m_isClosed;
}
void display::Update(){
SDL_GL_SwapWindow(m_window);
SDL_Event e;
while(SDL_PollEvent(&e)){
if(e.type == SDL_QUIT){
m_isClosed = true;
}
}
}
display.h
#ifndef DISPLAY_H_
#define DISPLAY_H_
#include <string>
#include "SDL2\SDL.h"
#undef main /*need to put this in or else it gives me "undefined reference to WinMain" ERROR*/
class display{
public:
display(int width, int height, const std::string& title);
void Update();
bool isClosed();
virtual ~display();
private:
display(const display& other){}
display& operator=(const display& other){}
SDL_Window* m_window;
SDL_GLContext m_glContext;
bool m_isClosed;
};
#endif /* DISPLAY_H_ */
To set up GLEW, a current OpenGL Context is needed (see Creating an OpenGL Context (WGL) for more information).
Create OpenGL context and window
A OpenGL Context and a window can easily created by SDL, GLFW or GLUT (see Initializing GLEW for more information).
Initilize SDL
If you are using SDL you have to create the window and you have to create the OpenGL context.
SDL_Window *window = SDL_CreateWindow(""OGL window", SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED, width, height, SDL_WINDOW_OPENGL );
SDL_GLContext glContext = SDL_GL_CreateContext( window );
Note, you should check for errors with SDL_GetError.
The OpenGL context has to become the current context before you use it. Use SDL_GL_MakeCurrent therefor.
SDL_GL_MakeCurrent( window, glContext );
Initilize GLUT
To set up GLUT you have to use glutInit and can follow the instructions of initializing glew.
glutInit(&argc, argv);
glutCreateWindow("OGL window");
Initilize GLFW
Note, glfwInit returns GLFW_TRUE if succeded:
if ( glfwInit() != GLFW_TRUE )
return;
GLFWwindow *wnd = glfwCreateWindow( width, height, "OGL window", nullptr, nullptr );
if ( wnd == nullptr )
{
glfwTerminate();
return;
}
glfwMakeContextCurrent( wnd );
After you have created an OpenGL Context and you have made it become the current context, you have to initialize glew.
Set up GLEW
Note that glewInit returns GLEW_OK if succeded:
if ( glewInit() != GLEW_OK )
return;
To link the GLEW library correctly you have to set up proper preprocessor definitions:
On Windows, you also need to define the GLEW_STATIC preprocessor token when building a static library or executable, and the GLEW_BUILD preprocessor token when building a dll
See also the answer to GLEW Linker Errors (undefined reference to `__glewBindVertexArray')
So basically to solve this problem you want to download the source from the glew website and compiler it yourself. You use the command prompt to get in the directory of the folder you downloaded and execute these commands line by line in order:
gcc -DGLEW_NO_GLU -O2 -Wall -W -Iinclude -DGLEW_BUILD -o src/glew.o -c src/glew.c
gcc -nostdlib -shared -Wl,-soname,libglew32.dll -Wl,--out-implib,lib/libglew32.dll.a -o lib/glew32.dll src/glew.o -L/mingw/lib -lglu32 -lopengl32 -lgdi32 -luser32 -lkernel32
and finnally:
gcc-ar cr lib/libglew32.a src/glew.o (though the "gcc-" may not be needed, it was for me)
Once you're done with that left click on your project and go to Properties, then under C/C++ Build go to settings, then under MinGW C++ Linker click in Libraries. Once you're there make sure your Library search path is correct (the place where Eclipse looks for your libraries) then in Libraries enter these one by one: glew32 opengl32 glu32 glew32.dll SDL2 SDL2main SDL2_test
Also when you compiled from source there should be a glew32 with a .dll not a .a extension in your lib folder inside the glew folder you downloaded from the website, drop that in into your debug (where your .exe is created). Do the same for the .dllnot the .dll.a for SDL and also make sure you have your include folders for both glew and SDL set up under the GCC C++ Compiler (also under your settings for the C/C++ Builder). It should work now.

how to use GLFW with openGL?

I'm trying to compile a short openGl and GLFW code but I'm failing in..
I've included all the .h like this :
#include <stdio.h>
#include <stdlib.h>
#include <GL/glew.h>
#include <GL/glfw2.h>
I'm on linux so I've made a :
sudo zypper install libglfw2
When I'm compiling like this :
g++ main.cpp -lGLU
I have :
main.cpp:14:22: fatal error: GL/glfw2.h:Any file or directory
#include <GL/glfw2.h>
^
compilation terminated.
I've also tried to include #include <GL/glfw3.h> but without success.
This is my complet code :
#include <stdio.h>
#include <stdlib.h>
#include <GL/glew.h>
#include <GL/glfw2.h>
int main()
{
if (glfwInit() == false)
{
fprintf(stderr, "GLFW failed to initialise.\n");
return (-1);
}
glfwWindowHint(GLFW_FSAA_SAMPLES, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GLFWwindow* window;
window = glfwCreateWindow(640, 480, "OPENGL", NULL, NULL);
if (!window)
{
fprintf(stderr, "Window failed to create\n");
glfwTerminate();
return (-1);
}
glfwMakeContextCurrent(window);
glewExperimental = true;
if (glewInit() != GLEW_OK)
{
fprintf(stderr, "Failed to initialize GLEW\n");
return (-1);
}
return (0);
}
Could someone know what I have to do to compile my code ?
Thanks
There is no GL/glfw2.h and never has been, and the code above uses the GLFW 3 API. See Renamed library and header file for more information about header paths.
There is no reason to use GLFW 2 for new projects, so I would avoid the libglfw2 package. Instead see if there is a GLFW 3 package.

Why does glGetString(GL_VERSION) return null / zero instead of the OpenGL version?

I'm on Linux Mint 13 XFCE. My problem is that when I run in terminal the command:
glxinfo | grep "OpenGL version"
I get the following output:
OpenGL version string: 3.3.0 NVIDIA 295.40
But when I run the glGetString(GL_VERSION) in my application then the result is null. Why doesn't this code get the gl_version?
#include <stdio.h>
#include <GL/glew.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include <GL/glext.h>
int main(int argc, char **argv) {
glutInit(&argc, argv);
glewInit();
printf("OpenGL version supported by this platform (%s): \n",
glGetString(GL_VERSION));
}
glutInit() doesn't create a GL context or make one current. You need a current GL context for glewInit() and glGetString() to work.
Try this:
#include <GL/glew.h>
#include <GL/glut.h>
#include <cstdio>
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutCreateWindow("GLUT");
glewInit();
printf("OpenGL version supported by this platform (%s): \n", glGetString(GL_VERSION));
}
You can also use glfw in order to create GL context and then query the version:
Include this files:
#include "GL/glew.h"
#include "GLFW/glfw3.h"
And then you can do:
// Initialise GLFW
glewExperimental = true; // Needed for core profile
if (!glfwInit())
{
return "";
}
// We are rendering off-screen, but a window is still needed for the context
// creation. There are hints that this is no longer needed in GL 3.3, but that
// windows still wants it. So just in case.
glfwWindowHint(GLFW_VISIBLE, GL_FALSE); //dont show the window
// Open a window and create its OpenGL context
GLFWwindow* window;
window = glfwCreateWindow(100, 100, "Dummy window", NULL, NULL);
if (window == NULL) {
return "";
}
glfwMakeContextCurrent(window); // Initialize GLEW
if (glewInit() != GLEW_OK)
{
return "";
}
std::string versionString = std::string((const char*)glGetString(GL_VERSION));

Game Program in c++ and SDL. 'SLD_Flip' error

I have started to make a game in C++ with SDL. My Code is:
main.cpp
#include "SDL/SDL.h"
#include "gameSystem.h"
int main(int argc, char *args[])
{
gameSystem systemHandler;
SDL_Surface *buffer;
SDL_Event event;
while(event.type != SDL_QUIT)
{
SDL_PollEvent(&event);
SLD_Flip(buffer);
}
}
gameSystem.cpp
#include "SDL/SDL.h"
#include "gameSystem.h"
gameSystem::gameSystem()
{
SDL_Init(SDL_INIT_EVERYTHING);
SDL_WM_SetCaption("GameName", NULL);
SDL_Surface *buffer;
bool fullscreen = false;
if (fullscreen == true)
{buffer = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE | SDL_FULLSCREEN);}
else
{buffer = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE);}
}
gameSystem::~gameSystem()
{
SDL_Quit();
}
gameSystem.h
class gameSystem
{
public:
gameSystem();
~gameSystem();
private:
SDL_Surface *buffer;
};
My linker options include: -lmingw32 -lSDLmain -lSDL -lwinmm -sdl_Mixer
I get the following error: "error: 'SLD_Flip' was not declared in this scope" on line 12 of main.cpp.
All the other SDL functions seem to be fine.
Does anyone know how to fix this problem?
You misspelled it. It should be SDL_Flip, not SLD_Flip.
Also, you haven't initialized your variable buffer in main. You are not setting its value inside of gameSystem::gameSystem(); the variable is out of scope.