Can't link SDL2 with CLANG on Windows 10

Hello everyone,

I’m trying to compile a very simple SDL2 code in C with CLANG :

    #include <stdio.h>
    #include <stdlib.h>
    #include "SDL.h"

    int main(int argc, char **argv)
    {
    	if(SDL_Init(SDL_INIT_VIDEO) != 0)
    		exit(EXIT_FAILURE);

    	return EXIT_SUCCESS;
    }

My project tree is :

  • bin
  • include
    +SDL2
  • lib

So I use the following command :

clang main.c -L./lib -I./include/SDL2/ -lSDL2main -lSDL2

But I get :

LINK : fatal error LNK1181: can't open entry file 'SDL2main.lib'
clang: error: linker command failed with exit code 1181 (use -v to see invocation)

It’s a linking error basically but why ?

Thanks in advance

It can’t find SDL2main.lib in any of the system library directories or your supplied library directory.

I’ve tried to use it locally with the following code :

    #include <stdio.h>
    #include <stdlib.h>
    #include "SDL2/SDL.h"

    int main(int argc, char **argv)
    {
    if(SDL_Init(SDL_INIT_VIDEO) != 0)
    {
        fprintf(stderr,"ERROR : Failed to initialize SDL2\n");
        exit(EXIT_FAILURE);
    }

    return EXIT_SUCCESS;
    }

And this command (I manually specify the include and lib folders in the command below) :
image
clang main.c -std=c11 -I.\include\SDL2\ -L.\lib -lSDL2main -lSDL2 -o test.exe

But I get :

LINK : fatal error LNK1181: can't open entry file 'SDL2main.lib'
clang: error: linker command failed with exit code 1181 (use -v to see invocation)

I don’t see a “SDL2main.lib” in that directory?

Looks like clang wants its libs VC-style instead of MinGW-style, try the SDL2-devel download for VC

Hi @Daniel_Gibson !

You were right ! I downloaded the VC libs instead :

image

It is due to the fact that I use clang with MSVC W10 SDK.

But I still get an error this time :

clang main.c -std=c11 -I.\include\ -L.\lib\ -lSDL2main -lSDL2 -o pong.exe

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

I think it means that int main(int argc, char** argv) or int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nShowCmd) is missing - but that should be provided by SDL2main…
Which one the linker expects depends on whether it thinks it’s building for the “console” or “windows” subsystem; I assume that SDL2main (esp. for VC) provides WinMain, so you’ll have to figure out how to tell clang that, with VC it’s like this: https://docs.microsoft.com/en-us/cpp/build/reference/entry-entry-point-symbol