5

Im creating my first program with C++ and wxwidgets. When I try to compile the project I get errors.

LNK2019 unresolved external symbol _main referenced in function "int __cdecl invoke_main(void)" (?invoke_main@@YAHXZ)

LNK1120 1 unresolved externals

I have compiled the wxwidgets my self in Visual Studio.

After compiling I created a new C++ empty project in Visual Studio.

I went to configuration and added includes directories:

Configuration properties -> C/C++ -> General -> Additional include directories:

C:\Users\user\source\repos\wxWidgets\include; C:\Users\user\source\repos\wxWidgets\include\msvc

Configuration properties -> Linker -> Additional Library Directories:

C:\Users\user\source\repos\wxWidgets\lib\vc_lib

Then I added 2 classes, cApp and cMain.

cApp.h

#pragma once

#include "wx/wx.h"
#include "cMain.h"

class cApp : public wxApp
{
public:
    cApp();
    ~cApp();

private: 
    cMain* m_frame1 = nullptr;

public: 
    virtual bool OnInit();
};

cApp.cpp

#include "cApp.h"

wxIMPLEMENT_APP(cApp);

cApp::cApp() {

}

cApp::~cApp() {

}

bool cApp::OnInit() {
    m_frame1 = new cMain();
    m_frame1->Show();

    return true;
}

cMain.h

#pragma once

#include "wx/wx.h"

class cMain : public wxFrame
{
public:
    cMain();
    ~cMain();
};

cMain.cpp

#include "cMain.h"

cMain::cMain() : wxFrame(nullptr, wxID_ANY, "MyProgram") {

}

cMain::~cMain() {

}
Ðаn
  • 10,934
  • 11
  • 59
  • 95
Europa
  • 974
  • 12
  • 40
  • No, I dont have a int main(). Where can I define it? Im using wxWidgets. – Europa Jan 21 '20 at 22:15
  • 1
    I only have the two classes. Im following this tutorial: https://www.youtube.com/watch?v=FOIbK4bJKS8 – Europa Jan 21 '20 at 22:17
  • I see from the documentation that `wxWidgets` is supposed to implement `int main()` for you. I am not sure why its not doing that. – drescherjm Jan 21 '20 at 22:21
  • 1
    Yes, becuase of "wxIMPLEMENT_APP(cApp)". – Europa Jan 21 '20 at 22:24
  • You seem to be missing `DECLARE_APP(cApp)` in your header for `cApp`. Not sure if that is needed its in the example code: [https://wiki.wxwidgets.org/Hello_World](https://wiki.wxwidgets.org/Hello_World) – drescherjm Jan 21 '20 at 22:32
  • Hmmm. The error message says it can't find _main but at 22:18 in the video you linked to it says it should be looking for _WinMain. The difference is that _WinMain is used by Windows Desktop Application projects and _main is used by Windows Console Application projects. Why does it think you want a console app? – Jerry Jeremiah Jan 21 '20 at 22:57

2 Answers2

4

You have 2 problems (after the edit due to the comment below) the following problem:

  1. You're building your application as a console mode application and not a GUI one. While it is possible to use wxWidgets from console applications too, this is probably not what you're trying to do, so ensure that the "Linker|System|SubSystem" option in the properties dialog of your project is set to "Windows".
  2. You don't have wxIMPLEMENT_APP(cApp); macro in your code. Again, it is perfectly possible to avoid it, but this is probably not your goal here, so just add this line. This macro is what defines main or WinMain for your application, depending on the platform.
VZ.
  • 21,740
  • 3
  • 39
  • 42
0

Your linker wants to find the main entry point (main is the default entry point). The wxIMPLEMENT_APP(cApp) macro on the Windows architecture has the WinMain entry point.

I have three quick solutions.

  1. You can write a macro #define WinMain main before macro wxIMPLEMENT_APP(cApp)

  2. You can tell the linker the name of the entry point( In your case, the name of the entry point is WinMain).

project properties -> Configuration Properties -> Linker -> Advanced and on the right pane of the dialog is a place for the name of the entry point function.

  1. Your Visual Studio project has a Console SubSystem installed (this SubSystem looks for the default main entry point). You must change the SubSystem value to WINDOWS (this SubSystem looks for the WinMain entry point).

Project properties -> Configuration Properties -> Linker -> System and then set the SubSystem property to Windows (/SUBSYSTEM:WINDOWS)