Installing using VCPKG

A question - when installing SDL2 using vcpkg and visual studio and trying to follow the LazyFoo Tutorials, I get some linking errors, namely

With some help and googling, I was able to make it compile and window to display by adding #define SDL_MAIN_HANDLED before the other includes.
However, what is not clear to me, do I need to add SDL_SetMainReady() in the beginning of my main function?
As far as I am aware I need to run it as a work around for SDL_Init failure. Does it mean that if I run SDL_Init manually and do all the checks myself, then it is unnecessary?

  • Thanks.

PS: There is no problem when I link everything manually, myself. The problem is only with vcpkg installation

I’ve never had to set that #define myself, and I’m on Windows. (Unless that’s a new thing?)

Are you linking to both sdl2.lib and sdl2main.lib, and calling your entry point int main(int argc, char* argv[]) { return 0; } ?

The problem occurs only when using vcpkg. (Package manager for c++).
There are 2 workarounds - to add the #define SDL_MAIN_HANDLED or manually link the sdl2main.lib (sdl2.lib is linked automatically). Manually linking, in my opinion, kills the purpose of using the package manager. Hence I am curious, whethe using the #define option is enough, or extra work might be needed.

sdl2.lib is linked automatically

Are you building with cmake and you’re talking about which libs the sdl2-config.cmake (or SDL2Config.cmake?) that comes with the package links against by default?
Otherwise please explain what you mean with “linked automatically” (by whom?).

after you type vcpkg install - most of times, one can type #include and everything will compile just fine, without extra plumbing (at least in visual studio). (I do not use CMake). There are some exceptions, SDL is apparently one of them.

With SDL2, I need to manually link sdl2main.lib (no need to link other libraries) in order not to get linking errors. The other solution is to write #define SDL_MAIN_HANDLED.

My question, is will there be different/quirky behaviou if I choose to put #define SDL_MAIN_HANDLED route instead of linking sdl2main.lib.

Had the same problem, found this: vcpkg/ at master · microsoft/vcpkg · GitHub

“For the vast majority of libraries, this is all you need to do – just File → New Project and write code! However, some libraries perform conflicting behaviors such as redefining main() . Since you need to choose per-project which of these conflicting options you want, you will need to add those libraries to your linker inputs manually.”

Then it even lists SDL2 as an example, funny

So you need to add the correct paths for SDL2main.lib manually and also the file itself as linker input. The paths are depending on your configuration and platform

For example, the SDL2main.lib for Release|x64 is at vcpkg\installed\x64-windows\lib\manual-link

Thanks, I am aware of that.

My second question, that I would like answered is whether adding #define SDL_MAIN_HANDLED work without surprises - or there is additional setup required if I do that. Is adding SDL_MainSetReady() necessary in this case.


Hello, I installed SDL v1 as part of an apprenticeship. (The yard dates a little …)

So I use as configuration VS2019 v16.9.4 and vcpkg 2021-01-13-d67989bce1043b98092ac45996a8230a059a2d7e.

I installed the SDL version 1.2.15#13 and the x64-windows triplet.

The SDL redefines the main so the lib SDLmain.lib is manually linked by vcpkg. Run vcpkg owns manual-link to see which libs have a manual link.


My project setup is as follows:

  • Configuration Properties> vcpkg> Triplet = x64-windows
  • Here is that I write only in C - Configuration Properties> C / C ++ > Advanced > Compile As = Compile as C Code (/TC)
  • In the case of debugging - Configuration Properties > Linker > Input > Additional Dependencies = SDLmaind.lib
  • In the case of the Release - Configuration Properties > Linker > Input > Additional Dependencies = SDLmain.lib
  • In the case of debugging and see the messages in the console - Configuration Properties > Linker > System > SubSystem = Console (/SUBSYSTEM:CONSOLE)
  • In the case of the Release and not seeing the console - Configuration Properties > Linker > System > SubSystem = Windows (/SUBSYSTEM:WINDOWS)

So the #define SDL_MAIN_HANDLED is never used.

I think this setup by replacing SDLmain.lib and SDLmaind.lib with SDL2main.lib and SDL2maind.lib will work without issue.

Hope this can help for others!