user2435124 user2435124 - 2 months ago 43
C++ Question

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

I am trying to use clang on Windows to compile and link an SDL2 application.

The reason for this is to try and keep my development environment consistent with other team members who are using OSX with XCode (compiling with clang). Since the Visual C++ compiler is much less strict than the clang compiler I could potentially be committing changes that will not compile under clang.

I would prefer not to have to install VS 2015 to use the experimental LLVM build environment: (link removed)

I have installed the LLVM/clang tools on windows (not built from source, just downloaded binaries from here: (link removed)) and can successfully build and run a "hello world" console application using clang.

What I would like to do, is to have a batch file that allows me to build and link with clang periodically to make sure my code will be safe to commit.

When linking even a simple single file SDL2 application, I receive the following linker error:

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


This thread suggests setting the Linker Subsystem SDL2: LNK1561: entry point must be defined although I'm not sure how to do that when compiling from the command line. As I understand, the default should be CONSOLE when not specified.

My main entry point function is in the form int main(int argc, char* argv[]) as per this thread: Why SDL defines main macro?

Here is the bat file I am using:

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


The include and library directories are correct as far as I can tell. The linker can find the libraries and the compiler can see the include files.

To keep things simple, the code I am using to test the compiler/linker has been pulled straight from lazy foo's intro tutorial found here: 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;
}


Does anyone know why I would be receiving this linker error when linking SDL using clang under windows?

Answer

MSVC linker uses /subsystem:windows option to set GUI mode (and use WinMain instead of main), so you'll need to pass it:

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