Search code examples
c++visual-studiosdlsdl-2vcpkg

VS auto-linking against SDL2 libraries installed with vcpkg on Windows


To the best of my knowledge, this isn't a duplicate of an existing question. This question is specifically about Visual Studio's auto-linking SDL2 libraries.

I've installed SDL2 (x64-windows variant) with vcpkg:

vcpkg install sdl2 --triplet x64-windows

And I've made vpkg libraries available to Visual Studio:

vcpkg integrate install

My VS 2019 project is configured to use the Console subsystem, and my main program looks like that:

#define SDL_MAIN_HANDLED
#include <SDL2/SDL.h>

int main(int, char*[])
{
}

Why do I need to specify SDL_MAIN_HANDLED? It seems that auto-linking with SDLmain2.lib doesn't happen for some reason?

If I don't specify SDL_MAIN_HANDLED, linking fails:

unresolved external symbol main referenced in function "int __cdecl invoke_main(void)" (?invoke_main@@YAHXZ)

I've also tried adding extern "C" on main() declaration but to no avail.

I've written many apps with SDL2 but this is the first time I'm using vcpkg to locate it.


Solution

  • It appears to be a deliberate decision made by those who created the package.

    If you look at the package description file, you can see that SDL2main.lib is being moved into the manual-link directory. I'm not familiar with vcpkg, so I don't know how exactly you can "manually link" against it, but I assume it's possible.