0

I'm trying to learn wxWidgets from zetcode's first gui sample code I made a VisualStudio 2019 empty project, applied the wxwidgets.props property sheet to my project and created files simple.cpp, simple.h, main.cpp and main.h as in the tutorial. The solution explorer shows this: Solution Explorer Screenshot Here is the content of Main.h

#pragma once
#include <wx/wx.h>

class MyApp : public wxApp
{
public:
    virtual bool OnInit();
};

DECLARE_APP(MyApp);

Here is the code of Main.cpp

#include "Main.h"
#include "Simple.h"

IMPLEMENT_APP(MyApp);

bool MyApp::OnInit()
{
    Simple* simple = new Simple(wxT("Simple"));
    simple->Show(true);

    return true;
}

Here is the code of Simple.h

#pragma once
#include <wx/wx.h>

class Simple : public wxFrame
{
public:
    Simple(const wxString& title);

};

Here is the code of Simple.cpp

#include "Simple.h"

Simple::Simple(const wxString& title)
    : wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(250, 150))
{
    Centre();
}

Unfortunately, the project would not link with the following error message:

Severity    Code    Description Project File    Line    Suppression State
Error   LNK2019 unresolved external symbol _main referenced in function "int __cdecl invoke_main(void)" (?invoke_main@@YAHXZ)   Simple  D:\devt\WxWidgets\ZetCode\MSVCRTD.lib(exe_main.obj) 1   
user1741137
  • 4,949
  • 2
  • 19
  • 28
  • Should have been taken care of by the `IMPLEMENT_APP` line in main.cpp. Make sure main.cpp has been correctly added to the project and is is being compiled and linked. – user4581301 Jan 03 '21 at 18:02
  • I have added the code explicitly and a screenshot of the solution explorer's display of the project. – user1741137 Jan 03 '21 at 18:41
  • 1
    `"Error LNK2019 unresolved external symbol _main referenced in function"` you don't have a WxWidgets problem, You have an "I can't find `main()` problem." See [error LNK2019: unresolved external symbol _main referenced](https://stackoverflow.com/q/4845410/3422102) – David C. Rankin Jan 03 '21 at 18:42
  • @user1741137, can you build and run minimal samle provided with the wxWidgets? Just open wxWidgets\samples\minimal\minimal_vcN.sln and click `Build` – Igor Jan 03 '21 at 18:46
  • I can run the minimal sample provided with wxWidgets. It's much more complicated than this though. – user1741137 Jan 03 '21 at 20:08

1 Answers1

1

Thanks to David C Rankin who led me to a solution: I went to Project>Configuration Properties>Linker>System>Subsystem and changed it from Console to Windows (/SUBSYSTEM:WINDOWS) This seems to have solved the problem, but I wish someone could explain what is the implication of what I did.

user1741137
  • 4,949
  • 2
  • 19
  • 28
  • 4
    The option /SUBSYSTEM:WINDOWS tells the linker to set WinMain as the application entry point. The wxWidgets macro wxIMPLEMENT_APP provides the WinMain function for your application. When the subsystem setting was set to console, that told the linker to use a function named main as the entry point. Since the wxIMPLEMENT_APP provides WinMain instead of main, there was no main function and the linker gave the error you were seeing. – New Pagodi Jan 03 '21 at 23:34
  • 1
    And if you _really_ wanted to use `main()` as the entry point, you could, but then you'd have to use `wxIMPLEMENT_APP_CONSOLE()` or even `wxIMPLEMENT_APP_NO_MAIN()` and define your own `main()`. – VZ. Jan 04 '21 at 12:46