0

I'm not sure the term for what SDL2 is doing, but I was reading that it "hijacks" main.

I have a project called CoolGame and a project which I want to be reusable across many other games I make, called Engine.

Here is my engine.cpp

#include "engine.h"
using namespace engine;

Engine::Engine()
{
    window = NULL;
    renderer = NULL;
}

Engine::Engine(const char* title)
{
    SDL_Init(SDL_INIT_EVERYTHING);
    window = SDL_CreateWindow(title, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 600, 400, SDL_WINDOW_SHOWN);
    renderer = SDL_CreateRenderer(window, -1, 0);
}

void Engine::RenderThisFrame()
{
    SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);

    SDL_RenderClear(renderer);

    SDL_RenderPresent(renderer);
}

Some of you may see where I am going. I am trying to make a nice Game Engine wrapper for sdl2. Here is my main in coolgame

#include "engine.h"
#include <iostream>
#undef main
using namespace std;
using namespace engine;

int main(int argc, const char* argv[]) {
    cout << "break main." << endl;
    Engine e = Engine("title");

    e.RenderThisFrame();
    return 0;
}

This does not work. I also have tried #undef in engine.h and I get the same error as below. LNK2019 unresolved external symbol _SDL_main referenced in function _main_utf8

When I take SDL.h and put it directly in coolgame it works as expected.

Is there anyway to stop SDL2 from hijacking main? Or is there any way to hoist that main hijack to the coolgame project? If that makes any sense

For austerity here is

engine.h

#ifndef ENGINE_CODE
#define ENGINE_CODE

#include "SDL.h"
#undef main
namespace engine {
    class Engine {
    public:
        Engine();
        Engine(const char* title);

        void RenderThisFrame();
    private:
        SDL_Window* window;
        SDL_Renderer* renderer;
    };
}
#endif

Edit

I am using Visual Studio 2019 for my IDE if that matters, I can provide C++ build tools version if requested.

Edit

I don't believe this question is exactly answered by another question. Here's why.

I have added SDL_MAIN_HANDLED and removed the #undefine main and it still didn't work.

I then moved all my engine code into the coolgame project with SDL_MAIN_HANDLED and main running as expected. Lo and behold it works, it has something to do with the projects being referenced. So this does not fix my original question. I am adding a Visual Studio tag.these are all errors. I have added the engine folder to the include directories in coolgame and I have also added packages/.../sdl to the include directories

christopher clark
  • 2,026
  • 5
  • 28
  • 47
  • 1
    Possible duplicate of [I'm using the SDL functions without the SDL\_main be defined. Is that fine?](https://stackoverflow.com/questions/34079288/im-using-the-sdl-functions-without-the-sdl-main-be-defined-is-that-fine) – Retired Ninja Jun 07 '19 at 01:09
  • This may help as well: https://wiki.libsdl.org/SDL_SetMainReady – Retired Ninja Jun 07 '19 at 01:10
  • I have tried both solutions, and they did not seem to help. I am getting the same error. @RetiredNinja – christopher clark Jun 07 '19 at 02:19
  • If you `#undef main` you shouldn't link with `SDL2main`. Fix one of these. – keltar Jun 07 '19 at 07:44
  • @keltar I have removed the undef, in order to test this all, I moved everything to one project in visual studio and got the code running with `SDL_MAIN_HANDLED` i think the problem is actually the way I am linking projects together in visual studio. the `Engine` project can no longer find sdl2. and yes sdl2 is referenced in the engine project. I have decided to move on for now. – christopher clark Jun 07 '19 at 11:01
  • SDL2 and SDL2main are separate libraries. "unresolved _SDL_main" caused by linking `SDL2main`, which only handles replaced `main` function. If you want your own main - you don't link `SDL2main` (and define `SDL_MAIN_HANDLED` or undef main). Basically you informed SDL2 "hey I have my own main, please don't replace it with SDL_main", but you still link with `SDL2main`, which expects `SDL_main`, which isn't there. – keltar Jun 07 '19 at 11:33
  • @keltar I'm not sure how I can unlink SDL2main in Visual Studio. – christopher clark Jun 07 '19 at 11:35
  • @christopherclark isn't it in your project settings in "linker/input/additional dependencies"? How did you initially addeed SDL2 there? – keltar Jun 07 '19 at 11:43
  • @keltar it is added as a nuget package. Visual studio is doing some magic and appears to take care of all that. – christopher clark Jun 07 '19 at 11:45

0 Answers0