0

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

enter image description here

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:

enter image description here

Tharteon
  • 58
  • 6
  • 2
    What's up with copypasting the flags multiple times? Also, you having `.lib` files is suspicious, since normally those are used with MSVC, not GCC. SDL2 ships `.a` files for GCC. – HolyBlackCat Jun 05 '22 at 09:28
  • I copied an old "SDL library" folder i had in my computer, maybe should've checked it more thoroughly. Should I delete the ```.lib``` files? – Tharteon Jun 05 '22 at 09:31
  • 1
    Yes. In fact, I would delete everything and re-download the libraries, just in case. – HolyBlackCat Jun 05 '22 at 09:32
  • Yeah I changed it, now it says ```C:/Program Files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping incompatible ./src/lib/libSDL2main.a when searching for -lSDL2main``` for every one of the libraries. It cannot find them. – Tharteon Jun 05 '22 at 09:40
  • IIRC that's when you try linking 32-bit and 64-bit object files together. – Quentin Jun 05 '22 at 09:41
  • Where did you get those .a files? The files you've downloaded should have two versions of it, x32 and x64, try both. – HolyBlackCat Jun 05 '22 at 09:41
  • It works, i had to use 64bits version of SDL2 and Glew. I just needed to redownload them all and put only the ones i needed. – Tharteon Jun 05 '22 at 09:54

0 Answers0