Page 1 of 1

Getting "unresolved external symbol _main" error in VS2019

Posted: Wed May 26, 2021 10:48 am
by gjoynerjr
Hello everyone.

I decided to jump back into working on some personal projects using wxWidgets, but for some reason this time I cannot get Visual Studio 2019 to cooperate on one of my systems. I cross-checked my other computer where VS2019 seems to work, but even when confirming that it's all the same I can't seem to get it running.

At this point I'm trying to just get the Hello World program in the docs up and running, but it refuses to work. The steps I followed to set it up are from the docs:
If you use MSVS 2010 or later IDE for building your project, simply add
`wxwidgets.props` property sheet to (all) your project(s) using wxWidgets
by using "View|Property Manager" menu item to open the property manager
window and then selecting "Add Existing Property Sheet..." from the context
menu in this window.

If you've created a new empty project (i.e. chose "Empty Project" in the
"Create a new project" window shown by MSVS rather than "Windows Desktop"),
you need to change "Linker|System|SubSystem" in the project properties to
"Windows", from the default "Console". You don't need to do anything else.
It doesn't seem to have any linking errors and the syntax highlighting seems good, so I guess it's linking correctly there. However, I'm getting a specific error when I try to build:

Code: Select all

unresolved external symbol _main referenced in function "int __cdecl invoke_main(void)"
To be clear, I did update the subsystem setting to Windows as per the instructions.

NOTE: I do not have any additional include directories set because of the "You don't need to do anything else." at the end of that doc section.

Any ideas? I've spent about 2 days trying to get this running and just can't seem to get it running. Any help is appreciated!

Re: Getting "unresolved external symbol _main" error in VS2019

Posted: Wed May 26, 2021 12:18 pm
by PB
The linker error you reported is exactly the same as the one caused by not doing what is described in the second paragraph of instructions you quoted.

Are you sure you set the linker subsystem to "Windows (/SUBSYSTEM:WINDOWS)"?

Re: Getting "unresolved external symbol _main" error in VS2019

Posted: Wed May 26, 2021 12:46 pm
by doublemax
At this point I'm trying to just get the Hello World program in the docs up and running,
IMHO the first step should be to build the "minimal" sample from the /samples directory using the provided VS project files.

Re: Getting "unresolved external symbol _main" error in VS2019

Posted: Thu May 27, 2021 11:23 pm
by gjoynerjr
PB wrote:
Wed May 26, 2021 12:18 pm
The linker error you reported is exactly the same as the one caused by not doing what is described in the second paragraph of instructions you quoted.

Are you sure you set the linker subsystem to "Windows (/SUBSYSTEM:WINDOWS)"?
I forgot to mention in the original post that it was the first thing I checked and set after seeing that error, but yes, it is set to Windows.
Image

doublemax wrote:
Wed May 26, 2021 12:46 pm
IMHO the first step should be to build the "minimal" sample from the /samples directory using the provided VS project files.
I tried that before and was met with issues there too, but will try it again.

Re: Getting "unresolved external symbol _main" error in VS2019

Posted: Thu May 27, 2021 11:58 pm
by gjoynerjr
I was able to compile and run the minimal project, but I'm not entirely sure how to move a minimal project to another location, rename it for my project, and have it working. I tried copy/pasting some of the files in minimal, but that led to incorrect paths (which makes sense).

Is it better to just export it as a template, or is there a better method to move the proper vcxproj/sln? I'm nowhere near an expert on Visual Studio, so just not sure what all needs to be moved from minimal for it to properly work.

Re: Getting "unresolved external symbol _main" error in VS2019

Posted: Fri May 28, 2021 12:45 am
by ONEEYEMAN
Hi,
All you have to do is copy the whole minimal sample folder somewhere.
Then open the solution from the new place, change the include and lib directories to point where wxWidgets is and you will be good to go.
If you get problem doing it - let us know and either me or someone else will help.

Thank you.

Re: Getting "unresolved external symbol _main" error in VS2019

Posted: Fri May 28, 2021 5:38 am
by PB
gjoynerjr wrote:
Thu May 27, 2021 11:23 pm
I forgot to mention in the original post that it was the first thing I checked and set after seeing that error, but yes, it is set to Windows.
This is very odd. AFAIK, the linker looks for main() only when the subsystem is set to console, it looks for WinMain() for Windows subsystem.

Using the .props file should work. But if you are desperate, FWIW, here is my guide how to set up wxWidgets project in MSVS manually: viewtopic.php?f=19&t=46700#p196105

Re: Getting "unresolved external symbol _main" error in VS2019

Posted: Fri May 28, 2021 9:22 am
by gjoynerjr
Still having issues. Here's what I did from a clean start.

I started with these instructions: https://docs.wxwidgets.org/trunk/plat_msw_binaries.html

1) I downloaded the following files:

Code: Select all

wxMSW-3.1.5_vc14x_x64_Dev.7z
wxMSW-3.1.5_vc14x_x64_ReleaseDLL.7z
wxWidgets-3.1.5-headers.7z
2) All files were extracted to C:\DevLibs\wxWidgets-3.1.5 .
The environmental variable WXWIN is set to this path. For good measure, I updated the environmental variable PATH to include

Code: Select all

WXWIN\lib
WXWIN\lib\vc14x_x64_dll
WXWIN\lib\vc14x_x64_dll\mswu
WXWIN\lib\vc14x_x64_dll\mswud
3) I created a new, empty project.

4) I copy/pasted the Hello World example into a new cpp file.

5) In Property Manager, I added the wxwidgets.props file.

At this point, I performed the steps in your (PB) linked guide, only changing any "vc_dll" to "vc14x_x64_dll" and I did not adjust PATH as noted in 5.1 as I cannot find that setting.

This has led to a new error when trying to build:

Code: Select all

wxWidgets libraries not found under "C:\DevLibs\wxWidgets-3.1.5\lib"
Any ideas if there's a step I missed or anything to check? I know that samples/minimal did run correctly (though that got nuked when I started over for this, but will redownload), but just a little worried of future problems if this basic setup is fighting like this. May just be overly concerned though.

Appreciate all the help!

Re: Getting "unresolved external symbol _main" error in VS2019

Posted: Fri May 28, 2021 10:30 am
by PB
gjoynerjr wrote:
Fri May 28, 2021 9:22 am
For good measure, I updated the environmental variable PATH to include

Code: Select all

WXWIN\lib
WXWIN\lib\vc14x_x64_dll
WXWIN\lib\vc14x_x64_dll\mswu
WXWIN\lib\vc14x_x64_dll\mswud
Adding anything besides WXWIN\lib\vc14x_x64_dll to the PATH is useless. BTW, I assume you actually used %WXWIN% instead of WXWIN when adding the folder to PATH. But this only helps the executable to find the DLLs when run, it has nothing to do with building it.
gjoynerjr wrote:
Fri May 28, 2021 9:22 am
5) In Property Manager, I added the wxwidgets.props file.
At this point, I performed the steps in your (PB) linked guide, only changing any "vc_dll" to "vc14x_x64_dll" and I did not adjust PATH as noted in 5.1 as I cannot find that setting.
Do not do that, either use the .props way or the manual way, never mix them. The settings for PATH in MSVS is where it is described, but you do not need to do this if you already added the folder to the system-wide PATH.

Re: Getting "unresolved external symbol _main" error in VS2019

Posted: Fri May 28, 2021 3:36 pm
by PB
FWIW, I just tried the below and I was able to build the project successfully.

Step 1
I downloaded files

Code: Select all

wxMSW-3.1.5_vc14x_x64_ReleaseDLL.7z
wxMSW-3.1.5_vc14x_x64_Dev.7z
wxWidgets-3.1.5.7z
Step 2
I extracted the three downloaded archives to originally empty folder c:\wxWidgets.

Step 3
I ensured MSVS was not running and set environment variable WXWIN to point to folder c:\wxWidgets.

Step 4
I launched MSVS, from menu selected File/New/Project, in the New Project dialog chose "Windows Desktop Application" and let MSVS create the project.

Step 5
In the new project I used menu View/Property Manager, in the Property Manager window right clicked on the project root, chose "Add Existing Property Sheet..." from the popup menu and selected file c:\wxWidgets\wxwidgets.props.

Step 6
I replaced the content of the only cpp file in the project with the complete code from Hello World example at https://docs.wxwidgets.org/trunk/overvi ... world.html

Step 7
I switched the active build configuration from x86 to x64 (as I installed only 64-bit libraries) and successfully built the project.

Step 8
The built application does not run because I did not tell it where to find wxWidgets DLL. But that has nothing to with building the project and is easy to fix. Either copy the DLLs to the folder with the executable or in the project's Configuration properties / Debugging set Environment to

Code: Select all

PATH=$(WXWIN)\lib\vc14x_x64_dll
I am running MSVS 2019 Community v16.10.0 on Windows 10.

If you followed steps 1-7 exactly and still failed to build the project, my guess would be that your MSVS install is somehow broken. I think it could be caused by modifying its global configuration, be it by hand or installing a vcpkg package. I am not sure if you can simply and successfully mix wxWidgets installed globally by vcpgk and installed some other way.

Re: Getting "unresolved external symbol _main" error in VS2019

Posted: Sat May 29, 2021 12:46 am
by gjoynerjr
Following those steps got it running! I tried doing the same with an empty project and it came up with the _main error again, so I'm assuming that creating an empty Windows Desktop App sets something to make it happy.

Regardless, I have a starting point to see if I can get my old project up and running again. Really appreciate the help!

Re: Getting "unresolved external symbol _main" error in VS2019

Posted: Sat May 29, 2021 7:12 am
by PB
gjoynerjr wrote:
Sat May 29, 2021 12:46 am
Following those steps got it running! I tried doing the same with an empty project and it came up with the _main error again, so I'm assuming that creating an empty Windows Desktop App sets something to make it happy.
This is because Empty Project template uses Console and not Windows linker subsystem, it is even tagged so in the templates list.

Re: Getting "unresolved external symbol _main" error in VS2019

Posted: Tue Jun 01, 2021 10:15 pm
by gjoynerjr
PB wrote:
Sat May 29, 2021 7:12 am
This is because Empty Project template uses Console and not Windows linker subsystem, it is even tagged so in the templates list.
You mean the subsystem setting, correct? It seems odd that it still doesn't work even after changing it to Windows, unless there's something else I'm missing behind the scenes. Either way, I've got my old project up and running again, so all is well. :) Appreciate the help!