无法编译 wxWidgets Hello World

Cannot compile wxWidgets Hello World

因此,我试图编译 wxWidgets 提供的 hello world 示例,但没有成功。我将逐步解释我所做的事情,直到我卡在当前位置。

正在编译 wxWidgets

  1. 首先,我选择的编译器是 MinGw-w64,由于 wxWidgets 建议从源代码构建他们的库(从 wxWidgets download 下载),我决定使用 CMake。
  2. 我在 wxWidgets 的根目录中创建了一个名为 cmake 的目录(在我的例子中是 C:\C++\wxWidgets-3.1.3)。我还创建了 bin 目录来存储编译后的二进制文件。
  3. 然后我导航到 cmake 目录和 运行 以下 CMake 配置命令:
    $ cmake -G "MinGW Makefiles" .. -D "wxBUILD_SHARED=OFF" -D "wxBUILD_MONOLITHIC=OFF" -D "CMAKE_BUILD_TYPE=debug" -D "CMAKE_C_COMPILER=gcc.exe" -D "CMAKE_CXX_COMPILER=g++.exe"
    然后我开始构建以下命令:
    $ cmake --build . --target all
    生成的.a文件(静态库文件)被放置在bin 目录.
  4. 我用 -D "CMAKE_BUILD_TYPE=release" 参数重复了步骤 3。
  5. 为了方便,将生成的lib\gcc_x64_lib\mswulib\gcc_x64_lib\mswud目录放入include目录(因为它们包含setup.h 头文件).

因此我的 C:\C++\wxWidgets-3.1.3\bin 目录包含以下文件:

libwxexpat.a   libwxpngd.a        libwxtiff.a   wxbase31u_net.a   wxmsw31u_adv.a   wxmsw31u_media.a     wxmsw31u_stc.a      wxmsw31ud_core.a      wxmsw31ud_qa.a        wxmsw31ud_xrc.a
libwxexpatd.a  libwxregexu.a      libwxtiffd.a  wxbase31u_xml.a   wxmsw31u_aui.a   wxmsw31u_propgrid.a  wxmsw31u_webview.a  wxmsw31ud_gl.a        wxmsw31ud_ribbon.a    wxrc.exe*
libwxjpeg.a    libwxregexud.a     libwxzlib.a   wxbase31ud.a      wxmsw31u_core.a  wxmsw31u_qa.a        wxmsw31u_xrc.a      wxmsw31ud_html.a      wxmsw31ud_richtext.a
libwxjpegd.a   libwxscintilla.a   libwxzlibd.a  wxbase31ud_net.a  wxmsw31u_gl.a    wxmsw31u_ribbon.a    wxmsw31ud_adv.a     wxmsw31ud_media.a     wxmsw31ud_stc.a
libwxpng.a     libwxscintillad.a  wxbase31u.a   wxbase31ud_xml.a  wxmsw31u_html.a  wxmsw31u_richtext.a  wxmsw31ud_aui.a     wxmsw31ud_propgrid.a  wxmsw31ud_webview.a

正在创建 Hello World 项目

我使用以下文件结构创建了简单的 wxWidgets hello world 项目:

app
|   build
|   headers
|   sources
|   |    main.cpp
|   CMakeLists.txt

main.cpp的内容:

// wxWidgets "Hello World" Program
// For compilers that support precompilation, includes "wx/wx.h".
#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();

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

enum {
    ID_Hello = 1
};

wxIMPLEMENT_APP(MyApp);

bool MyApp::OnInit() {
    MyFrame *frame = new MyFrame();
    frame->Show(true);
    return true;
}

MyFrame::MyFrame(): wxFrame(NULL, wxID_ANY, "Hello World") {
    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!");

    Bind(wxEVT_MENU, &MyFrame::OnHello, this, ID_Hello);
    Bind(wxEVT_MENU, &MyFrame::OnAbout, this, wxID_ABOUT);
    Bind(wxEVT_MENU, &MyFrame::OnExit, this, wxID_EXIT);
}

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

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

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

CMakeLists.txt的内容:

cmake_minimum_required(VERSION 2.8)

project(hello_world)

if (MINGW)
    add_compile_options(--static)
endif()

set(wx_libraries_path "C:\C++\wxWidgets-3.1.3\bin")

if(CMAKE_BUILD_TYPE STREQUAL "Debug")
    include_directories("C:\C++\wxWidgets-3.1.3\include\mswud")
    find_library(libwxexpat       NAMES libwxexpatd        PATH ${wx_libraries_path})
    find_library(libwxjpeg        NAMES libwxjpegd         PATH ${wx_libraries_path})
    find_library(libwxpng         NAMES libwxpngd          PATH ${wx_libraries_path})
    find_library(libwxregexu      NAMES libwxregexud       PATH ${wx_libraries_path})
    find_library(libwxscintilla   NAMES libwxscintillad    PATH ${wx_libraries_path})
    find_library(libwxtiff        NAMES libwxtiffd         PATH ${wx_libraries_path})
    find_library(libwxzlib        NAMES libwxzlibd         PATH ${wx_libraries_path})
    find_library(wxbase31u        NAMES wxbase31ud         PATH ${wx_libraries_path})
    find_library(wxbase31u_xml    NAMES wxbase31ud_net     PATH ${wx_libraries_path})
    find_library(wxbase31u_net    NAMES wxbase31ud_xml     PATH ${wx_libraries_path})
    find_library(wxmsw31u_adv     NAMES wxmsw31ud_adv      PATH ${wx_libraries_path})
    find_library(wxmsw31u_aui     NAMES wxmsw31ud_aui      PATH ${wx_libraries_path})
    find_library(wxmsw31u_core    NAMES wxmsw31ud_core     PATH ${wx_libraries_path})
    find_library(wxmsw31u_gl      NAMES wxmsw31ud_gl       PATH ${wx_libraries_path})
    find_library(wxmsw31u_html    NAMES wxmsw31ud_html     PATH ${wx_libraries_path})
    find_library(wxmsw31u_media   NAMES wxmsw31ud_media    PATH ${wx_libraries_path})
    find_library(wxmsw31u_propgri NAMES wxmsw31ud_propgrid PATH ${wx_libraries_path})
    find_library(wxmsw31u_qa      NAMES wxmsw31ud_qa       PATH ${wx_libraries_path})
    find_library(wxmsw31u_ribbon  NAMES wxmsw31ud_ribbon   PATH ${wx_libraries_path})
    find_library(wxmsw31u_richtex NAMES wxmsw31ud_richtext PATH ${wx_libraries_path})
    find_library(wxmsw31u_stc     NAMES wxmsw31ud_stc      PATH ${wx_libraries_path})
    find_library(wxmsw31u_webview NAMES wxmsw31ud_webview  PATH ${wx_libraries_path})
    find_library(wxmsw31u_xrc     NAMES wxmsw31ud_xrc      PATH ${wx_libraries_path})
endif()

if(CMAKE_BUILD_TYPE STREQUAL "Release")
    include_directories("C:\C++\wxWidgets-3.1.3\include\mswu")
    find_library(libwxexpat       NAMES libwxexpat       PATH ${wx_libraries_path})
    find_library(libwxjpeg        NAMES libwxjpeg        PATH ${wx_libraries_path})
    find_library(libwxpng         NAMES libwxpng         PATH ${wx_libraries_path})
    find_library(libwxregexu      NAMES libwxregexu      PATH ${wx_libraries_path})
    find_library(libwxscintilla   NAMES libwxscintilla   PATH ${wx_libraries_path})
    find_library(libwxtiff        NAMES libwxtiff        PATH ${wx_libraries_path})
    find_library(libwxzlib        NAMES libwxzlib        PATH ${wx_libraries_path})
    find_library(wxbase31u        NAMES wxbase31u        PATH ${wx_libraries_path})
    find_library(wxbase31u_xml    NAMES wxbase31u_xml    PATH ${wx_libraries_path})
    find_library(wxbase31u_net    NAMES wxbase31u_net    PATH ${wx_libraries_path})
    find_library(wxmsw31u_adv     NAMES wxmsw31u_adv     PATH ${wx_libraries_path})
    find_library(wxmsw31u_aui     NAMES wxmsw31u_aui     PATH ${wx_libraries_path})
    find_library(wxmsw31u_core    NAMES wxmsw31u_core    PATH ${wx_libraries_path})
    find_library(wxmsw31u_gl      NAMES wxmsw31u_gl      PATH ${wx_libraries_path})
    find_library(wxmsw31u_html    NAMES wxmsw31u_html    PATH ${wx_libraries_path})
    find_library(wxmsw31u_media   NAMES wxmsw31u_media   PATH ${wx_libraries_path})
    find_library(wxmsw31u_propgri NAMES wxmsw31u_propgri PATH ${wx_libraries_path})
    find_library(wxmsw31u_qa      NAMES wxmsw31u_qa      PATH ${wx_libraries_path})
    find_library(wxmsw31u_ribbon  NAMES wxmsw31u_ribbon  PATH ${wx_libraries_path})
    find_library(wxmsw31u_richtex NAMES wxmsw31u_richtex PATH ${wx_libraries_path})
    find_library(wxmsw31u_stc     NAMES wxmsw31u_stc     PATH ${wx_libraries_path})
    find_library(wxmsw31u_webview NAMES wxmsw31u_webview PATH ${wx_libraries_path})
    find_library(wxmsw31u_xrc     NAMES wxmsw31u_xrc     PATH ${wx_libraries_path})
endif()

include_directories("C:\C++\wxWidgets-3.1.3\include")
link_directories("C:\C++\wxWidgets-3.1.3\bin")

add_executable(app sources/main.cpp)

message(STATUS "${libwxexpat}")
target_link_libraries(app PRIVATE
    ${libwxexpat}
    ${libwxjpeg}
    ${libwxpng}
    ${libwxregexu}
    ${libwxscintilla}
    ${libwxtiff}
    ${libwxzlib}
    ${wxbase31u}
    ${wxbase31u_xml}
    ${wxbase31u_net}
    ${wxmsw31u_adv}
    ${wxmsw31u_aui}
    ${wxmsw31u_core}
    ${wxmsw31u_gl}
    ${wxmsw31u_html}
    ${wxmsw31u_media}
    ${wxmsw31u_propgrid}
    ${wxmsw31u_qa}
    ${wxmsw31u_ribbon}
    ${wxmsw31u_richtext}
    ${wxmsw31u_stc}
    ${wxmsw31u_webview}
    ${wxmsw31u_xrc}
)

创建项目文件后,我执行了以下 CMake 命令来配置项目:

cmake -G "MinGW Makefiles" .. -D "CMAKE_C_COMPILER=gcc.exe" -D "CMAKE_CXX_COMPILER=g++.exe" -D "CMAKE_BUILD_TYPE=Debug" -D "CMAKE_VERBOSE_MAKEFILE:BOOL=ON"
然后我尝试使用以下 CMake 命令构建项目但失败了:
cmake --build .

编译错误

不幸的是,程序编译失败,输出如下:

"C:\Program Files\CMake\bin\cmake.exe" -E cmake_progress_start C:\Users\Oiltanker\Git\Pepega-Inject\installer\build\CMakeFiles C:\Users\Oiltanker\Git\Pepega-Inject\installer\build\CMakeFiles\progress.marks
C:/C++/MingGW-w64/mingw64/bin/mingw32-make.exe -f CMakeFiles\Makefile2 all
mingw32-make.exe[1]: Entering directory 'C:/Users/Oiltanker/Git/Pepega-Inject/installer/build'
C:/C++/MingGW-w64/mingw64/bin/mingw32-make.exe -f CMakeFiles\app.dir\build.make CMakeFiles/app.dir/depend
mingw32-make.exe[2]: Entering directory 'C:/Users/Oiltanker/Git/Pepega-Inject/installer/build'
"C:\Program Files\CMake\bin\cmake.exe" -E cmake_depends "MinGW Makefiles" C:\Users\Oiltanker\Git\Pepega-Inject\installer C:\Users\Oiltanker\Git\Pepega-Inject\installer C:\Users\Oiltanker\Git\Pepega-Inject\installer\build C:\Users\Oiltanker\Git\Pepega-Inject\installer\build C:\Users\Oiltanker\Git\Pepega-Inject\installer\build\CMakeFiles\app.dir\DependInfo.cmake --color=
Dependee "C:\Users\Oiltanker\Git\Pepega-Inject\installer\build\CMakeFiles\app.dir\DependInfo.cmake" is newer than depender "C:/Users/Oiltanker/Git/Pepega-Inject/installer/build/CMakeFiles/app.dir/depend.internal".
Dependee "C:/Users/Oiltanker/Git/Pepega-Inject/installer/build/CMakeFiles/CMakeDirectoryInformation.cmake" is newer than depender "C:/Users/Oiltanker/Git/Pepega-Inject/installer/build/CMakeFiles/app.dir/depend.internal".
Scanning dependencies of target app
mingw32-make.exe[2]: Leaving directory 'C:/Users/Oiltanker/Git/Pepega-Inject/installer/build'
C:/C++/MingGW-w64/mingw64/bin/mingw32-make.exe -f CMakeFiles\app.dir\build.make CMakeFiles/app.dir/build
mingw32-make.exe[2]: Entering directory 'C:/Users/Oiltanker/Git/Pepega-Inject/installer/build'
[ 50%] Building CXX object CMakeFiles/app.dir/sources/main.cpp.obj
C:\C++\MingGW-w64\mingw64\bin\g++.exe   @CMakeFiles/app.dir/includes_CXX.rsp -g   --static -o CMakeFiles\app.dir\sources\main.cpp.obj -c C:\Users\Oiltanker\Git\Pepega-Inject\installer\sources\main.cpp
[100%] Linking CXX executable app.exe
"C:\Program Files\CMake\bin\cmake.exe" -E cmake_link_script CMakeFiles\app.dir\link.txt --verbose=1
"C:\Program Files\CMake\bin\cmake.exe" -E remove -f CMakeFiles\app.dir/objects.a
C:\C++\MingGW-w64\mingw64\bin\ar.exe cr CMakeFiles\app.dir/objects.a @CMakeFiles\app.dir\objects1.rsp
C:\C++\MingGW-w64\mingw64\bin\g++.exe -g   -Wl,--whole-archive CMakeFiles\app.dir/objects.a -Wl,--no-whole-archive  -o app.exe -Wl,--out-implib,libapp.dll.a -Wl,--major-image-version,0,--minor-image-version,0 @CMakeFiles\app.dir\linklibs.rsp
C:/C++/wxWidgets-3.1.3/bin/wxbase31ud.a(dlmsw.cpp.obj): In function `GetFileVersion':
C:/C++/wxWidgets-3.1.3/src/msw/dlmsw.cpp:67: undefined reference to `GetFileVersionInfoSizeW'
      .......
C:/C++/wxWidgets-3.1.3/src/common/geometry.cpp:350: undefined reference to `wxDataInputStream::Read32()'
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[2]: *** [CMakeFiles\app.dir\build.make:110: app.exe] Error 1
mingw32-make.exe[2]: Leaving directory 'C:/Users/Oiltanker/Git/Pepega-Inject/installer/build'
mingw32-make.exe[1]: *** [CMakeFiles\Makefile2:78: CMakeFiles/app.dir/all] Error 2
mingw32-make.exe[1]: Leaving directory 'C:/Users/Oiltanker/Git/Pepega-Inject/installer/build'
mingw32-make.exe: *** [Makefile:86: all] Error 2```

GetFileVersionInfoSizeW()住在version.dll,所以你需要link和version.lib才能得到它。

wxDataInputStream::Read32() 更令人惊讶,它确实应该出现在您 link 使用的 wxbase 库中。尝试使用 nmobjdump.

检查它是否真的存在

最重要的是 Vadim 所说的 - 所有你玩 CMake 和朋友编译 wxWidgets 都是不必要的。

wxWidgets 已经提供了Makefile 来编译库。存储在 wxWidgets\build\msw.

所以你所能做的就是:

cd C:\C++\wxWidgets-3.1.3\buid\msw
mingw64-make -f makefile.gcc BUILD="debug"

然后因为 wxWidgets 已经为人们提供了最小的样本来尝试你要做的就是:

cd C:\C++\wxWidgets\samples\minimal
mingw64-make -f makefile.gcc BUILD="debug"
./minimal.exe

这样就简单多了,可以节省你很多时间。

最重要的是 - 只是一个小建议 - 看起来你正在链接很多你并不真正需要的库。对于像这样的最小示例,您真正需要的是 wxbase 和 wxcore。其他的都不需要。

谢谢。

我设法解决了我的问题。在我开始在互联网上搜索答案后,我发现 wxWidgets 项目 CMakeLists.txt 示例不起作用,因为它需要设置一些变量。正确设置后,我成功编译了hello world example。

这是 CMakeLists.txt 文件的内容:

project(hello_world)
cmake_minimum_required(VERSION 2.8)

aux_source_directory(src SRC_LIST)

set(wxWidgets_ROOT_DIR "C:/C++/wxWidgets")
set(wxWidgets_LIB_DIR "C:/C++/wxWidgets/lib/gcc_x64_lib")
set(wxWidgets_LIBRARIES "C:/C++/wxWidgets/lib")
set(wxWidgets_INCLUDE_DIRS "C:/C++/wxWidgets/include/wx")

set(wxWidgets_CONFIGURATION mswud)
find_package(wxWidgets COMPONENTS core base REQUIRED)
include(${wxWidgets_USE_FILE})

add_executable(${PROJECT_NAME} ${SRC_LIST})
target_link_libraries(${PROJECT_NAME} ${wxWidgets_LIBRARIES})

详细过程

我删除了 wxWidgets 并重新编译了它们。

  1. 这次 wxWidgets 源代码位于 C:\C++\wxWidgets-source 目录
  2. 我使用以下命令将 install 目标构建到 C:\C++\wxWidgets 目录:
    $ cmake --build . --target install
    您可以通过更改 wxINSTALL_PREFIX 定义来指定安装的目的地在 setup.h 文件中,并在 cmake_install.cmake 文件中警告 CMAKE_INSTALL_PREFIX 定义。
    • debug 配置的情况下,setup.h 文件将位于 <cmake_building_directory>\lib\gcc_x64_lib\mswud\wx
    • release 配置的情况下,setup.h 文件将位于 <cmake_building_directory>\lib\gcc_x64_lib\mswu\wx

我还被告知wxWidgets可能有一个特定的链接顺序并且find_package功能自动解决,是否属实我不知道。