在 Windows "LNK1561: entry point must be defined" 上使用 Clang 链接 SDL2 时出错

Error when Linking SDL2 using Clang on Windows "LNK1561: entry point must be defined"

我正尝试在 Windows 上使用 clang 来编译和 link SDL2 应用程序。

这样做的原因是为了尽量使我的开发环境与使用 OSX 和 XCode 的其他团队成员保持一致(使用 clang 编译)。由于 Visual C++ 编译器比 clang 编译器严格得多,我可能会提交不会在 clang 下编译的更改。

我宁愿不必安装 VS 2015 即可使用实验性 LLVM 构建环境:(link 已删除)

我已经在 windows 上安装了 LLVM/clang 工具(不是从源代码构建的,只是从这里下载的二进制文件:(link 已删除))并且可以成功构建和 运行 "hello world" 使用 clang 的控制台应用程序。

我想做的是有一个批处理文件,允许我定期构建和 link with clang 以确保我的代码可以安全提交。

即使是 link 一个简单的单文件 SDL2 应用程序,我也会收到以下 linker 错误:

LINK : fatal error LNK1561: entry point must be defined
clang++.exe: error: linker command failed with exit code 1561 (use -v to see invocation)

此线程建议设置链接器子系统 SDL2: LNK1561: entry point must be defined 尽管我不确定从命令行编译时如何设置。据我了解,未指定时默认应为 CONSOLE。

我的主要入口点函数的形式是 int main(int argc, char* argv[]) 按照这个线程:Why SDL defines main macro?

这是我正在使用的 bat 文件:

CALL "C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin\vcvars32.bat"
clang++ -std=c++11 main.cpp -I./include/SDL2 -L./lib -lSDL2main -lSDL2

据我所知,包含目录和库目录是正确的。 linker 可以找到库,编译器可以看到包含文件。

为了简单起见,我用来测试 compiler/linker 的代码是直接从 lazy foo 的介绍教程中提取的:http://lazyfoo.net/tutorials/SDL/01_hello_SDL/index2.php

/*This source code copyrighted by Lazy Foo' Productions (2004-2015)
and may not be redistributed without written permission.*/

//Using SDL and standard IO
#include <SDL.h>
#include <stdio.h>

//Screen dimension constants
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;

int main( int argc, char* args[] )
{
    //The window we'll be rendering to
    SDL_Window* window = NULL;

    //The surface contained by the window
    SDL_Surface* screenSurface = NULL;

    //Initialize SDL
    if( SDL_Init( SDL_INIT_VIDEO ) < 0 )
    {
        printf( "SDL could not initialize! SDL_Error: %s\n", SDL_GetError() );
    }
    else
    {
        //Create window
        window = SDL_CreateWindow( "SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN );
        if( window == NULL )
        {
            printf( "Window could not be created! SDL_Error: %s\n", SDL_GetError() );
        }
        else
        {
            //Get window surface
            screenSurface = SDL_GetWindowSurface( window );

            //Fill the surface white
            SDL_FillRect( screenSurface, NULL, SDL_MapRGB( screenSurface->format, 0xFF, 0xFF, 0xFF ) );

            //Update the surface
            SDL_UpdateWindowSurface( window );

            //Wait two seconds
            SDL_Delay( 2000 );
        }
    }

    //Destroy window
    SDL_DestroyWindow( window );

    //Quit SDL subsystems
    SDL_Quit();

    return 0;
}

有谁知道为什么我在 windows 下使用 clang linking SDL 时会收到这个 linker 错误?

MSVC 链接器使用 /subsystem:windows 选项来设置 GUI 模式(并使用 WinMain 而不是 main),因此您需要传递它:

clang++ -std=c++11 main.cpp -I./include/SDL2 -L./lib -lSDL2main -lSDL2 -Xlinker /subsystem:windows

LINK : fatal error LNK1561: entry point must be defined clang++.exe: error: linker command failed with exit code 1561 (use -v to see invocation)

当您没有 main(...) 函数时,通常会发生这种情况。

SDL "steals" main 函数,所以你的 int main(int, argc[]*) 并不是真正的入口点。

入口点在SDL2main.lib中定义。你必须用它告诉 clang 的 linker 到 link。 (注意:必须link和SDL2mainSDL2之前。)

另请注意,无论您在哪里定义 main(...) 函数,都必须 #include "SDL.h".