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

Viewed 1929

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.

2 Answers

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.

Linking SDL2 manual-link libraries while using vcpkg with VS

My answer is a follow-up to @HolyBlackCat, thanks for help.

In your case, the directory is x64-windows, by default it is x86-windows.

Right-click on your project -> Properties -> Configuration Properties


First step

Go to: VC++ Directories -> Library Directories

For Debug configuration, add:

$(VCPKG_ROOT)\installed\x64-windows\debug\lib\manual-link

For Release configuration, add:

$(VCPKG_ROOT)\installed\x64-windows\lib\manual-link

Second step

Go to: Linker -> Input -> Additional Dependencies

For Debug configuration, add:

SDL2maind.lib

For Release configuration, add:

SDL2main.lib

Now you should not be bother by "main() redefinition" errors.

Related