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

2

6

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.

Linguistician answered 26/9, 2020 at 15:4 Comment(4)
Does this answer your question? I'm using the SDL functions without the SDL_main be defined. Is that fine?Tardigrade
Hi @S.M., I'm aware of this post and it doesn't answer my question.Lorenzen
It answers the question "Why do I need to specify SDL_MAIN_HANDLED?", does not it?Tardigrade
It does but in a different context. I know exactly what SDL_MAIN_HANDLED does and what it's for; what I can't figure out is why I need to use it in that specific context, and whether there's a better way to do things.Lorenzen
I
4

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.

Isotone answered 26/9, 2020 at 16:51 Comment(2)
Thanks, that's super helpful! Will have to figure out how to manually link that lib then.Lorenzen
This helped: github.com/microsoft/vcpkg/issues/12474Lorenzen
U
7

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.

Unterwalden answered 7/7, 2022 at 19:48 Comment(0)
I
4

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.

Isotone answered 26/9, 2020 at 16:51 Comment(2)
Thanks, that's super helpful! Will have to figure out how to manually link that lib then.Lorenzen
This helped: github.com/microsoft/vcpkg/issues/12474Lorenzen

© 2022 - 2024 — McMap. All rights reserved.