[英]Error when Linking SDL2 using Clang on Windows “LNK1561: entry point must be defined”
我試圖在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:必須定義入口點,盡管我不確定在從命令行進行編譯時如何執行此操作。 據我了解,未指定時默認值應為CONSOLE。
我的主要入口點函數的形式是int main(int argc,char * argv []),因為這個線程: 為什么SDL定義主宏?
這是我正在使用的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時會收到此鏈接器錯誤?
MSVC鏈接器使用/subsystem:windows
選項來設置GUI模式(並使用WinMain
而不是main
),因此您需要傳遞它:
clang++ -std=c++11 main.cpp -I./include/SDL2 -L./lib -lSDL2main -lSDL2 -Xlinker /subsystem:windows
LINK:致命錯誤LNK1561:必須定義入口點clang ++。exe:錯誤:鏈接器命令失敗,退出代碼為1561(使用-v查看調用)
當您沒有main(...)
函數時,通常會發生這種情況。
SDL“竊取”主函數,因此你的int main(int, argc[]*)
並不是真正的入口點。
入口點在SDL2main.lib
定義。 你必須告訴clang的鏈接器與它鏈接。 (注意:必須在SDL2
之前與SDL2main
鏈接。)
另請注意,無論您在哪里定義main(...)
函數,都必須#include "SDL.h"
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.