# Installing using VCPKG

**URL:** <https://discourse.libsdl.org/t/installing-using-vcpkg/28685>\
**Category:** SDL Development\
**Created:** [February 4, 2021, 2:02pm UTC](https://discourse.libsdl.org/t/installing-using-vcpkg/28685 "2021-02-04T14:02:37Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![theundergroundsorcer](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/t/c4cdca/32.png) [@theundergroundsorcer](https://discourse.libsdl.org/u/theundergroundsorcer)\
**Post date:** [February 4, 2021, 2:02pm UTC](https://discourse.libsdl.org/t/installing-using-vcpkg/28685/1 "2021-02-04T14:02:37Z")

</div>

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

 ![image](https://discourse.libsdl.org/uploads/default/original/2X/5/5823dc8726a5d6d8bab91a75d14e703f2108c0b0.png)

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

---

<div class="post-metadata">

**Author:** ![Erik\_Yuzwa](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/erik_yuzwa/32/505_2.png) [@Erik\_Yuzwa](https://discourse.libsdl.org/u/Erik_Yuzwa)\
**Post date:** [February 4, 2021, 11:37pm UTC](https://discourse.libsdl.org/t/installing-using-vcpkg/28685/2 "2021-02-04T23:37:27Z")

</div>

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; }` ?

---

<div class="post-metadata">

**Author:** ![theundergroundsorcer](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/t/c4cdca/32.png) [@theundergroundsorcer](https://discourse.libsdl.org/u/theundergroundsorcer)\
**Post date:** [February 4, 2021, 11:50pm UTC](https://discourse.libsdl.org/t/installing-using-vcpkg/28685/3 "2021-02-04T23:50:01Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Daniel\_Gibson](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/daniel_gibson/32/3010_2.png) [@Daniel\_Gibson](https://discourse.libsdl.org/u/Daniel_Gibson)\
**Post date:** [February 5, 2021, 2:04am UTC](https://discourse.libsdl.org/t/installing-using-vcpkg/28685/4 "2021-02-05T02:04:13Z")

</div>

> 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?).

---

<div class="post-metadata">

**Author:** ![theundergroundsorcer](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/t/c4cdca/32.png) [@theundergroundsorcer](https://discourse.libsdl.org/u/theundergroundsorcer)\
**Post date:** [February 7, 2021, 8:35am UTC](https://discourse.libsdl.org/t/installing-using-vcpkg/28685/5 "2021-02-07T08:35:54Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![thomak](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/t/e79b87/32.png) [@thomak](https://discourse.libsdl.org/u/thomak)\
**Post date:** [February 8, 2021, 11:43pm UTC](https://discourse.libsdl.org/t/installing-using-vcpkg/28685/6 "2021-02-08T23:43:14Z")

</div>

Had the same problem, found this: [vcpkg/integration.md at master · microsoft/vcpkg · GitHub](https://github.com/microsoft/vcpkg/blob/master/docs/users/integration.md)

“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

---

<div class="post-metadata">

**Author:** ![theundergroundsorcer](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/t/c4cdca/32.png) [@theundergroundsorcer](https://discourse.libsdl.org/u/theundergroundsorcer)\
**Post date:** [February 10, 2021, 10:56am UTC](https://discourse.libsdl.org/t/installing-using-vcpkg/28685/7 "2021-02-10T10:56:00Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![didier\_broska](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@didier\_broska](https://discourse.libsdl.org/u/didier_broska)\
**Post date:** [April 28, 2021, 1:24pm UTC](https://discourse.libsdl.org/t/installing-using-vcpkg/28685/8 "2021-04-28T13:24:10Z")

</div>

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.

![image](https://discourse.libsdl.org/uploads/default/original/2X/b/b41ce47abaa87fe1d6c99131042435665d4a0bb1.png)

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!

---

<div class="post-metadata">

**Author:** ![kbridge](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/k/5fc32e/32.png) [@kbridge](https://discourse.libsdl.org/u/kbridge)\
**Post date:** [February 25, 2023, 3:27pm UTC](https://discourse.libsdl.org/t/installing-using-vcpkg/28685/9 "2023-02-25T15:27:31Z")

</div>

Your solution works.

Also note:

- Make sure your `main` function’s signature is `int main(int argc, char *args[])`, not the simplified form `int main()`
- Do a **Re** build after changing your `main` function’s signature. Otherwise, you may still stuck in the link error. (In my case: `unresolved external symbol SDL_main referenced in function main_getcmdline`)
