0

I am trying to compile this code -

//main.cpp

#include<SDL.h>

int main(int argc,char* argv[])
{
    if(SDL_Init(SDL_INIT_VIDEO))
    {
        return 1;
    }
        SDL_Window* window=SDL_CreateWindow("Project Igloo",(1366-640)/2,(768-480)/2,640,480,SDL_WINDOW_SHOWN);
        if(!window)
        {
            SDL_Log("Error : failed to create window: %s\n",SDL_GetError());
        }
        else
        {
            SDL_Renderer* renderer=SDL_CreateRenderer(window,-1,SDL_RENDERER_PRESENTVSYNC);
            if(!renderer)
            {
                SDL_Log("Error - failed to create renderer: %s\n",SDL_GetError());
            }
            else
            {
                SDL_Rect fillrect={(640-200)/2,(480-200)/2,200,200};
                SDL_SetRenderDrawColor(renderer,0xff,0xff,0xff,0xff);
                SDL_RenderClear(renderer);
                SDL_SetRenderDrawColor(renderer,0x80,0x80,0x80,0xff);
                SDL_RenderFillRect(renderer,&fillrect);
                SDL_RenderPresent(renderer);

                SDL_Event event;
                bool running=true;
                while(running)
                {
                    while(SDL_PollEvent(&event))
                    {
                        if(event.type==SDL_QUIT)
                        running=false;
                    }

                }

                SDL_DestroyRenderer(renderer);
            }

            SDL_DestroyWindow(window);
        }
        SDL_Quit();
    return 0;
}

However, every time I get the LINK : fatal error LNK1561: entry point must be defined error.

With Visual Studio 2022 BuildTools Edition, using the commands:

cl /EHsc /Iinclude main.cpp SDL2main.lib SDL2.lib /link /out:main.exe

cl /EHsc /Iinclude main.cpp SDL2.lib SDL2main.lib /link /out:main.exe

cl /EHsc /Iinclude SDL2main.lib SDL2.lib main.cpp /link /out:main.exe

cl /EHsc /Iinclude SDL2.lib SDL2main.lib main.cpp /link /out:main.exe

I followed this link however the accepted answer told to use Console (/SUBSYSTEM:CONSOLE) which requires installing other VC edition. Can I even compile SDL2 program without using edition other than BuildTools one? Do I need to add some other information to command line?

genpfault
  • 51,148
  • 11
  • 85
  • 139
  • To anyone who may see this question, as mentioned by @Joel too, you can also use ```#define SDL_MAIN_HANDLED``` before ```#include``` and define WinMain function so that your program doesn't invoke cmdlet. You can learn more about WinMain from [here](https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-winmain) – snowman4933 Jul 01 '23 at 10:25

1 Answers1

0

In the SDL.h header, main is defined as SDL_main. Therefore, the linker can't find an entry point for your application. You just need to write #undef main before your main() function and it should work.

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
Joel
  • 721
  • 1
  • 13
  • This is not necessary and, in many cases, can be avoided. – SafelyFast Apr 20 '23 at 05:11
  • @SafelyFast What else would you do instead? I found out writing `#define SDL_MAIN_HANDLED` before `#include ` solves it too, because `main` would not be defined by SDL then. Are there better options? – Joel Apr 20 '23 at 06:10
  • It _can_ solve the issue. I have a large SDL project currently going on Windows and I don’t use this. I use a standard main definition. I don’t use MSVC but I suspect there is a way to compile it as such. When I get the chance I’ll test it. – SafelyFast Apr 20 '23 at 12:32