How do I compile a wxWidgets Hello World app with CLion and the MSVC compiler toolchain without errors?

0

I am attempting to compile a basic wxWidgets (wxWidgets version 3.1.3) Hello World app with CLion and the MSVC (Visual Studio 2017 Community edition) compiler toolchain.

Using pre-compiled binaries available at https://www.wxwidgets.org/downloads/. See the first Download Windows Binaries button under Latest Development Release: 3.1.3. I am using the 64 bit version of the binaries.

Followed the instructions at https://www.wxwidgets.org/blog/2012/08/how-to-use-294-wxmsw-binaries/ on how to use the binaries.

Followed the instructions at https://www.jetbrains.com/help/clion/quick-tutorial-on-configuring-clion-on-windows.html#MSVC on how to set up CLion 20.04 to use a MSVC compiler. Architecture set to amd64, no Platform.

Hello World code, commented is what the wxIMPLEMENT_APP(MyApp) macro ultimately expands to:

#include <iostream>
#include <wx/wxprec.h>

#ifndef WX_PRECOMP
#include <wx/wx.h>
#endif

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

class MyFrame : public wxFrame {
public:
    MyFrame(const wxString &title, const wxPoint &pos, const wxSize &size);

private:
    void OnHello(wxCommandEvent &event);
    void OnExit(wxCommandEvent &event);
    void OnAbout(wxCommandEvent &event);

wxDECLARE_EVENT_TABLE();
};

enum {
    ID_Hello = 1
};

wxBEGIN_EVENT_TABLE(MyFrame, wxFrame)
                EVT_MENU(ID_Hello, MyFrame::OnHello)
                EVT_MENU(wxID_EXIT, MyFrame::OnExit)
                EVT_MENU(wxID_ABOUT, MyFrame::OnAbout)
wxEND_EVENT_TABLE()

wxIMPLEMENT_APP(MyApp);
/* This is what the macro expands to:
extern "C" int __stdcall WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, wxCmdLineArgType, int nCmdShow) {
    ;;
    return wxEntry(hInstance, hPrevInstance, 0, nCmdShow);
}
MyApp &wxGetApp() { return *static_cast<MyApp *>(wxApp::GetInstance()); }
wxAppConsole *wxCreateApp() {
    wxAppConsole::CheckBuildOptions(
            "3" "." "1" "." "3" " (" "wchar_t" ",Visual C++ " "1900" ",wx containers" ",compatible with 3.0" ")",
            "your program");
    return new MyApp;
}

wxAppInitializer wxTheAppInitializer((wxAppInitializerFunction) wxCreateApp)
 */

bool MyApp::OnInit() {
    MyFrame *frame = new MyFrame("Hello World", wxPoint(50, 50), wxSize(450, 340));
    frame->Show(true);
    return true;
}

MyFrame::MyFrame(const wxString &title, const wxPoint &pos, const wxSize &size)
        : wxFrame(NULL, wxID_ANY, title, pos, size) {
    wxMenu *menuFile = new wxMenu;
    menuFile->Append(ID_Hello, "&Hello...\tCtrl-H",
                     "Help string shown in status bar for this menu item");
    menuFile->AppendSeparator();
    menuFile->Append(wxID_EXIT);
    wxMenu *menuHelp = new wxMenu;
    menuHelp->Append(wxID_ABOUT);
    wxMenuBar *menuBar = new wxMenuBar;
    menuBar->Append(menuFile, "&File");
    menuBar->Append(menuHelp, "&Help");
    SetMenuBar(menuBar);
    CreateStatusBar();
    SetStatusText("Welcome to wxWidgets!");
}

void MyFrame::OnExit(wxCommandEvent &event) {
    Close(true);
}

void MyFrame::OnAbout(wxCommandEvent &event) {
    wxMessageBox("This is a wxWidgets' Hello world sample",
                 "About Hello World", wxOK | wxICON_INFORMATION);
}

void MyFrame::OnHello(wxCommandEvent &event) {
    wxLogMessage("Hello world from wxWidgets!");
}

The cmakelists.txt contents:

cmake_minimum_required(VERSION 3.16)
project(MyApp)

set(CMAKE_CXX_STANDARD 14)
set(SRC_FILES
    main.cpp
    )

add_definitions(-DWXUSINGDLL=1)
add_definitions(-DwxMSVC_VERSION_AUTO=1)
add_definitions(-DwxMSVC_VERSION_ABI_COMPAT=1)
add_definitions(-DUNICODE=1)
add_definitions(-D_UNICODE=1)

include_directories(
    includes
    E:/cpp/lib/wxWin
    E:/cpp/lib/wxWin/include
    E:/cpp/lib/wxWin/include/msvc
    E:/cpp/Lib/wxWin/lib/vc14x_x64_dll
    E:/cpp/Lib/wxWin/lib/vc14x_x64_dll/mswud
)
link_directories(
    E:/cpp/Lib/wxWin/lib/vc14x_x64_dll
)

add_executable(${PROJECT_NAME} ${SRC_FILES})

When I attempt to compile this, I get the error:

E:\cpp\lib\wxWin\include\wx/app.h(900): error C2144: syntax error: 'wxAppInitializer' should be preceded by ';'
E:\cpp\lib\wxWin\include\wx/app.h(900): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
NMAKE : fatal error U1077: '"C:\Apps\32\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.14.26428\bin\Hostx64\x64\cl.exe"' : return code '0x2'
Stop.
NMAKE : fatal error U1077: '"C:\Apps\32\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.14.26428\bin\HostX64\x64\nmake.exe"' : return code '0x2'
Stop.
NMAKE : fatal error U1077: '"C:\Apps\32\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.14.26428\bin\HostX64\x64\nmake.exe"' : return code '0x2'
Stop.
NMAKE : fatal error U1077: '"C:\Apps\32\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.14.26428\bin\HostX64\x64\nmake.exe"' : return code '0x2'
Stop.

The line in question from app.h:

__unused extern wxAppInitializer wxTheAppInitializer;

I dare not alter app.h as it is part of wxWidgets. Although, as an experiment, I commented out __unused and got these errors:

LINK Pass 1: command "C:\Apps\32\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.14.26428\bin\Hostx64\x64\link.exe /nologo @CMakeFiles\Manager.dir\objects1.rsp /out:Manager.exe /implib:Manager.lib /pdb:E:\cpp\dev\Manager\cmake-build-debug-msvc15\Manager.pdb /version:0.0 /machine:x64 /debug /INCREMENTAL /subsystem:console -LIBPATH:E:\cpp\Lib\wxWin\lib\vc14x_x64_dll kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib /MANIFEST /MANIFESTFILE:CMakeFiles\Manager.dir/intermediate.manifest CMakeFiles\Manager.dir/manifest.res" failed (exit code 1120) with the following output:
MSVCRTD.lib(exe_main.obj) : error LNK2019: unresolved external symbol main referenced in function "int __cdecl invoke_main(void)" (?invoke_main@@YAHXZ)
Manager.exe : fatal error LNK1120: 1 unresolved externals
NMAKE : fatal error U1077: 'C:\Apps\32\JetBrains\apps\CLion\ch-0\201.7223.86\bin\cmake\win\bin\cmake.exe' : return code '0xffffffff'
Stop.
NMAKE : fatal error U1077: '"C:\Apps\32\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.14.26428\bin\HostX64\x64\nmake.exe"' : return code '0x2'
Stop.
NMAKE : fatal error U1077: '"C:\Apps\32\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.14.26428\bin\HostX64\x64\nmake.exe"' : return code '0x2'
Stop.
NMAKE : fatal error U1077: '"C:\Apps\32\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.14.26428\bin\HostX64\x64\nmake.exe"' : return code '0x2'
Stop.

How do I eliminate this problem?


EDIT #1

I replaced the headers with those of the wxWidgets-3.1.3.7z downloaded archive and got these new errors:

LINK Pass 1: command "C:\Apps\32\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.14.26428\bin\Hostx64\x64\link.exe /nologo @CMakeFiles\HelloWorld.dir\objects1.rsp /out:HelloWorld.exe /implib:HelloWorld.lib /pdb:E:\cpp\dev\Manager\cmake-build-debug-msvc15\HelloWorld.pdb /version:0.0 /machine:x64 /debug /INCREMENTAL /subsystem:console -LIBPATH:E:\cpp\Lib\wxWin\lib\vc14x_x64_dll kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib /MANIFEST /MANIFESTFILE:CMakeFiles\HelloWorld.dir/intermediate.manifest CMakeFiles\HelloWorld.dir/manifest.res" failed (exit code 1120) with the following output:
MSVCRTD.lib(exe_main.obj) : error LNK2019: unresolved external symbol main referenced in function "int __cdecl invoke_main(void)" (?invoke_main@@YAHXZ)
HelloWorld.exe : fatal error LNK1120: 1 unresolved externals
NMAKE : fatal error U1077: 'C:\Apps\32\JetBrains\apps\CLion\ch-0\201.7223.86\bin\cmake\win\bin\cmake.exe' : return code '0xffffffff'
Stop.
NMAKE : fatal error U1077: '"C:\Apps\32\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.14.26428\bin\HostX64\x64\nmake.exe"' : return code '0x2'
Stop.
NMAKE : fatal error U1077: '"C:\Apps\32\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.14.26428\bin\HostX64\x64\nmake.exe"' : return code '0x2'
Stop.
NMAKE : fatal error U1077: '"C:\Apps\32\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.14.26428\bin\HostX64\x64\nmake.exe"' : return code '0x2'
Stop.
c++
visual-studio
cmake
wxwidgets
clion
asked on Stack Overflow May 18, 2020 by J Vines • edited May 21, 2020 by J Vines

2 Answers

0

You're using a modified version of wxWidgets, the official one doesn't have any __unused before the wxTheAppInitializer declaration. I have no idea how did it get there, but you need to remove it or, to be completely sure, remove all your local headers and replace them with the actual headers from wxWidgets-3.1.3-headers.7z file you downloaded.

answered on Stack Overflow May 18, 2020 by VZ.
0

Problems Solved!

Appended to the cmakelists.txt file:

set_target_properties(${PROJECT_NAME} PROPERTIES
    LINK_FLAGS /SUBSYSTEM:WINDOWS
    )

It now builds and runs with the following errors: - Reported in CLion's Run window:

Process finished with exit code -1073741515 (0xC0000135)
  • When the program is run from the terminal (cmd): System Error messages reporting:
The code execution cannot proceed because
wxmsw313ud_core_vc14x_x64.dll was not found. Reinstalling the
program may fix this problem.

and

The code execution cannot proceed because
wxbase313ud_vc14x_x64.dll was not found. Reinstalling the
program may fix this problem.

Placed these files in same folder as HelloWorld.exe and the problems are solved. As shown in Edit #1, above, replacing the headers with those of the wxWidgets-3.1.3.7z downloaded archive is also required.

answered on Stack Overflow May 21, 2020 by J Vines • edited May 23, 2020 by J Vines

User contributions licensed under CC BY-SA 3.0