如何使用 CLion 和 MSVC 编译器工具链正确编译 wxWidgets Hello World 应用程序?

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

我正在尝试使用 CLion 和 MSVC(Visual Studio 2017 社区版)编译器工具链编译一个基本的 wxWidgets(wxWidgets 版本 3.1.3)Hello World 应用程序。

使用 https://www.wxwidgets.org/downloads/ 上可用的 pre-compiled 个二进制文件。请参阅最新开发版本:3.1.3 下的第一个下载 Windows 二进制文件按钮。我正在使用 64 位版本的二进制文件。

按照 https://www.wxwidgets.org/blog/2012/08/how-to-use-294-wxmsw-binaries/ 中有关如何使用二进制文件的说明进行操作。

按照 https://www.jetbrains.com/help/clion/quick-tutorial-on-configuring-clion-on-windows.html#MSVC 中有关如何设置 CLion 20.04 以使用 MSVC 编译器的说明进行操作。架构设置为 amd64,无平台。

Hello World 代码,注释是 wxIMPLEMENT_APP(MyApp) 宏最终扩展为:

#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!");
}

cmakelists.txt内容:

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})

当我尝试编译它时,出现错误:

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\Microsoft Visual Studio17\Community\VC\Tools\MSVC.14.26428\bin\Hostx64\x64\cl.exe"' : return code '0x2'
Stop.
NMAKE : fatal error U1077: '"C:\Apps\Microsoft Visual Studio17\Community\VC\Tools\MSVC.14.26428\bin\HostX64\x64\nmake.exe"' : return code '0x2'
Stop.
NMAKE : fatal error U1077: '"C:\Apps\Microsoft Visual Studio17\Community\VC\Tools\MSVC.14.26428\bin\HostX64\x64\nmake.exe"' : return code '0x2'
Stop.
NMAKE : fatal error U1077: '"C:\Apps\Microsoft Visual Studio17\Community\VC\Tools\MSVC.14.26428\bin\HostX64\x64\nmake.exe"' : return code '0x2'
Stop.

来自 app.h 的相关行:

__unused extern wxAppInitializer wxTheAppInitializer;

我不敢改动app.h因为它是wxWidgets的一部分。尽管作为实验,我注释掉了 __unused 并得到了这些错误:

LINK Pass 1: command "C:\Apps\Microsoft Visual Studio17\Community\VC\Tools\MSVC.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\JetBrains\apps\CLion\ch-01.7223.86\bin\cmake\win\bin\cmake.exe' : return code '0xffffffff'
Stop.
NMAKE : fatal error U1077: '"C:\Apps\Microsoft Visual Studio17\Community\VC\Tools\MSVC.14.26428\bin\HostX64\x64\nmake.exe"' : return code '0x2'
Stop.
NMAKE : fatal error U1077: '"C:\Apps\Microsoft Visual Studio17\Community\VC\Tools\MSVC.14.26428\bin\HostX64\x64\nmake.exe"' : return code '0x2'
Stop.
NMAKE : fatal error U1077: '"C:\Apps\Microsoft Visual Studio17\Community\VC\Tools\MSVC.14.26428\bin\HostX64\x64\nmake.exe"' : return code '0x2'
Stop.

如何消除这个问题?


编辑#1

我用 wxWidgets-3.1.3.7z 下载的存档替换了 headers 并得到了这些新错误:

LINK Pass 1: command "C:\Apps\Microsoft Visual Studio17\Community\VC\Tools\MSVC.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\JetBrains\apps\CLion\ch-01.7223.86\bin\cmake\win\bin\cmake.exe' : return code '0xffffffff'
Stop.
NMAKE : fatal error U1077: '"C:\Apps\Microsoft Visual Studio17\Community\VC\Tools\MSVC.14.26428\bin\HostX64\x64\nmake.exe"' : return code '0x2'
Stop.
NMAKE : fatal error U1077: '"C:\Apps\Microsoft Visual Studio17\Community\VC\Tools\MSVC.14.26428\bin\HostX64\x64\nmake.exe"' : return code '0x2'
Stop.
NMAKE : fatal error U1077: '"C:\Apps\Microsoft Visual Studio17\Community\VC\Tools\MSVC.14.26428\bin\HostX64\x64\nmake.exe"' : return code '0x2'
Stop.

您使用的是修改版的wxWidgets,官方版在wxTheAppInitializer声明之前没有任何__unused。我不知道它是怎么到那里的,但你需要删除它,或者,为了完全确定,删除所有本地 headers 并将它们替换为 wxWidgets-3.1.3-headers.7z 文件中的实际 headers你下载了。

问题已解决!

附加到 cmakelists.txt 文件:

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

它现在构建并 运行s 出现以下错误: - 在 CLion 的 运行 window:

中报告
Process finished with exit code -1073741515 (0xC0000135)
  • 当程序来自终端 (cmd) 运行 时:系统错误消息报告:
The code execution cannot proceed because
wxmsw313ud_core_vc14x_x64.dll was not found. Reinstalling the
program may fix this problem.

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

将这些文件放在与 HelloWorld.exe 相同的文件夹中,问题就解决了。如上面的编辑 #1 所示,还需要将 headers 替换为 wxWidgets-3.1.3.7z 下载的存档。