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/integration.md 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.

Thanks.