gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 14:02:26 25 4
gpt4 key购买 nike

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

这样做的原因是试图让我的开发环境与其他使用 OSX 和 XCode(使用 clang 编译)的团队成员保持一致。由于 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)

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

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

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

最佳答案

MSVC 链接器使用 /subsystem:windows 选项来设置 GUI 模式(并使用 WinMain 而不是 main),所以你需要通过它:

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

关于c++ - 在 Windows 上使用 Clang 链接 SDL2 时出错 "LNK1561: entry point must be defined",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39871384/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com