在Windows上使用Clang链接SDL2时发生错误“ LNK1561:必须定义入口点”

rad*_*ore 5 c++ windows sdl batch-file clang

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

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

我希望不必安装VS 2015即可使用实验性LLVM构建环境:(已删除链接)

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

我想做的是拥有一个批处理文件,该文件允许我定期构建并与clang链接,以确保我的代码可以安全地提交。

甚至链接简单的单个文件SDL2应用程序时,我都会收到以下链接器错误:

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

该线程建议设置链接器子系统SDL2:LNK1561:必须定义入口点,尽管我不确定从命令行进行编译时该如何做。据我了解,未指定时默认值应为CONSOLE。

根据此线程,我的主入口函数的形式为int main(int argc,char * argv []):为什么SDL定义了main宏?

这是我正在使用的蝙蝠文件:

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
Run Code Online (Sandbox Code Playgroud)

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

为简单起见,我用来测试编译器/链接器的代码直接从懒惰的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;
}
Run Code Online (Sandbox Code Playgroud)

有谁知道为什么在Windows下使用clang链接SDL时为什么会收到此链接器错误?

kel*_*tar 6

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

clang++ -std=c++11 main.cpp -I./include/SDL2 -L./lib -lSDL2main -lSDL2 -Xlinker /subsystem:windows
Run Code Online (Sandbox Code Playgroud)