mosra / magnum

Lightweight and modular C++11 graphics middleware for games and data visualization
https://magnum.graphics/
Other
4.74k stars 439 forks source link

CorradeMain.lib - unresolved external symbol main #600

Closed ChrisHawkridge closed 1 year ago

ChrisHawkridge commented 1 year ago

Hi there, I am on Windows and using Visual Studio 2022 building and MFC based application. I installed the magnum package via vcpkg.

vcpkg install magnum

I use the magnum code in a DLL which is then used in my main EXE, however when I compile I eventually get to this error

CorradeMain.lib(CorradeMain.cpp.obj) : error LNK2001: unresolved external symbol main
C:\MyProject\x64\Release\MyProject.exe : fatal error LNK1120: 1 unresolved externals

I found the below section in the Corrade docs, but didn't really get anywhere with it.

Usage without CMake When you don't use CMake, simply linking to the CorradeMain.lib is not enough to achieve the desired effect — additionally you need to tell the linker to choose a different entry point. That's done by passing /ENTRY:wmainCRTStartup to it for console apps and /ENTRY:wWinMainCRTStartup for GUI apps.

Are there additional parameters that I need to pass to vcpkg install to target windows?

mosra commented 1 year ago

Hi,

the /ENTRY:wWinMainCRTStartup is meant to be passed to the compiler command line. If you use the VS IDE, somewhere in the project settings there's an option for it. I'm not on Windows and I don't use VS so I can't really give you any screenshots, hopefully this Microsoft documentation could help you find the option? The SubSystem should be set to Windows instead of Console. You may also want to enable Unicode (hopefully this SO answer would help).

Alternatively you can choose to not link to CorradeMain at all -- given that you have a MFC application, it's doing most of the system interaction anyway, and things done by CorradeMain might be clashing with it.

ChrisHawkridge commented 1 year ago

Thanks for the suggestions. SubSystem is set to Windows across all projects as well as using Unicode. I'm not explicitly linking to CorradeMain so it seemed odd that it was automatically being linked.

I ended up changing the option vcpkg -> Use AutoLink to No and that has solved it.

mosra commented 1 year ago

Uh oh, didn't expect vcpkg linker automagic to cause this. Good to know :)

Actually, now I see what was the root cause -- CorradeMain is expecting your code to provide a main() implementation (which is what Magnum applications generally have), but since you're using MFC instead, you don't really have it. So then CorradeMain is completely useless for your use case.

I'll update the documentation to point out this use case. (Edit: done in mosra/corrade@dd71a3bd7ce4204b7e6c94a86ab6068d45cf17fd.)