在Windows上使用Clang链接SDL2时出错" LNK1561:必须定义入口点"

时间:2016-10-05 10:21:02

标签: c++ windows batch-file clang sdl

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

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

我不想安装VS 2015来使用实验性的LLVM构建环境:(已删除链接)

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

我想做的是,有一个批处理文件,允许我定期构建和链接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)

此线程建议设置链接器子系统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

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

为了简单起见,我用来测试编译器/链接器的代码是从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链接SDL时会收到此链接器错误吗?

2 个答案:

答案 0 :(得分:4)

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

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

答案 1 :(得分:0)

  

LINK:致命错误LNK1561:必须定义入口点   clang ++。exe:错误:链接器命令失败,退出代码为1561(使用-v查看调用)

这通常发生在您没有main(...)功能时。

SDL&#34;抢断&#34;主要功能,因此int main(int, argc[]*)并非真正的切入点。

入口点在SDL2main.lib中定义。你必须告诉clang的链接器与它链接。 (注意:必须在SDL2main之前与SDL2相关联。)

另请注意,无论您在何处定义main(...)功能,都必须#include "SDL.h"