Re – LINK : fatal error LNK1561: entry point must be defined while compiling SDL2 project with VS2022 Buildtools

huangapple go评论51阅读模式
英文:

Re - LINK : fatal error LNK1561: entry point must be defined while compiling SDL2 project with VS2022 Buildtools

问题

//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;
}
英文:

I am trying to compile this code -

//main.cpp

#include&lt;SDL.h&gt;

int main(int argc,char* argv[])
{
    if(SDL_Init(SDL_INIT_VIDEO))
    {
        return 1;
    }
        SDL_Window* window=SDL_CreateWindow(&quot;Project Igloo&quot;,(1366-640)/2,(768-480)/2,640,480,SDL_WINDOW_SHOWN);
        if(!window)
        {
            SDL_Log(&quot;Error : failed to create window: %s\n&quot;,SDL_GetError());
        }
        else
        {
            SDL_Renderer* renderer=SDL_CreateRenderer(window,-1,SDL_RENDERER_PRESENTVSYNC);
            if(!renderer)
            {
                SDL_Log(&quot;Error - failed to create renderer: %s\n&quot;,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,&amp;fillrect);
                SDL_RenderPresent(renderer);

                SDL_Event event;
                bool running=true;
                while(running)
                {
                    while(SDL_PollEvent(&amp;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?

答案1

得分: 0

SDL.h 头文件中,main 被定义为 SDL_main。因此,链接器找不到应用程序的入口点。只需要在 main() 函数之前写上 #undef main 即可。

英文:

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.

huangapple
  • 本文由 发表于 2023年4月19日 16:06:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76052095.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定