wxWidgets 未使用 VSCode (MSVC) 中的 Cmake 进行链接

发布于 2025-01-20 14:58:06 字数 3905 浏览 0 评论 0原文

问题

我在构建项目时会遇到链接器错误。该项目毫不费力地找到WXWIDGETS标题。我已经查看了WXWIDGETS的最小示例,并重新创建了它,以验证我的构建源是否正常工作。我使用VSCODE中的Cmake使用CMAKE扩展程序进行构建。

WXWIDGETS CMAKE生成了罚款,WXWIDGETS的构建似乎很好。我正在使用Visual Studio 2022 AMD64工具集进行编译。 (我仅为编译器安装了VS2022社区版)。

看起来它正在为图书馆构建DLL。我更喜欢静态库,但是对于初始运行,我并不认为这很重要。我将我的应用程序链接到WX :: NET WX :: CORE WX :: BASE。我认为这将照顾我在建造时的链接。

当我使用WXWIDGETS库构建整体应用程序时,我会发现这些链接器错误。 我在这里做错了什么?我显然没有正确配置某些内容以找到库,但是我在这里很难过。

链接器错误

[build] ProjectName.obj : error LNK2001: unresolved external symbol "protected: static class wxAppConsole * (__cdecl* wxAppConsoleBase::ms_appInitFn)(void)" -snipped-
[build] ProjectName.obj : error LNK2001: unresolved external symbol "protected: static class wxAppConsole * wxAppConsoleBase::ms_appInstance" -snipped-
[build] MainWindow.obj : error LNK2001: unresolved external symbol "void (__cdecl* wxTheAssertHandler)(class wxString const &,int,class wxString const &,class wxString const &,class wxString const &)"-snipped-
[build] MainWindow.obj : error LNK2001: unresolved external symbol "bool wxTrapInAssert" (?wxTrapInAssert@@3_NA) -snipped-
[build] MainWindow.obj : error LNK2001: unresolved external symbol "class wxMBConv * wxConvLibcPtr" (?wxConvLibcPtr@@3PEAVwxMBConv@@EA) -snipped-
[build] MainWindow.obj : error LNK2001: unresolved external symbol "public: static unsigned __int64 const wxString::npos" (?npos@wxString@@2_KB)-snipped-
[build] MainWindow.obj : error LNK2001: unresolved external symbol "class wxSize const wxDefaultSize" (?wxDefaultSize@@3VwxSize@@B)-snipped-
[build] MainWindow.obj : error LNK2001: unresolved external symbol "class wxPoint const wxDefaultPosition" (?wxDefaultPosition@@3VwxPoint@@B)-snipped-
[build] MainWindow.obj : error LNK2001: unresolved external symbol "char const * const wxFrameNameStr" (?wxFrameNameStr@@3QBDB)-snipped-
[build] C:\path\to\executable\ProjectName.exe : fatal error LNK1120: 9 unresolved externals -snipped-
[build] Build finished with exit code 1 

项目布局

非常基本的项目布局。我正在构建GUI应用程序,并且从逻辑上将UI源代码分开。

这基本上是对最小WXWIDGETS示例的娱乐活动,其中您有一个入口点(ProjectName.hpp and ProjectName.CPP提供此内容)和主窗口/帧(mainwindow.hpp和mainwindow.cpp提供此信息)。 ProjectName应用程序将执行wximplement_app(projectName),并将指针保存到MainWindow。使用OnInit函数显示MainWindow。

ProjectName/
    build/
    external/
        wxWidgets-3.1.6/
        -CMakeLists.txt
    source/
        ui/
            -MainWindow.hpp
            -MainWindow.cpp
        -ProjectName.hpp
        -ProjectName.cpp
    -CMakeLists.txt

我已经使用他们的

projectName cmakelists.txt

cmake_minimum_required(VERSION 3.23)

set(MAJOR 0)
set(MINOR 0)
set(PATCH 1)

#Setup Project
project(ProjectNameProject VERSION ${MAJOR}.${MINOR}.${PATCH}
                           DESCRIPTION "ProjectName Descrip"
                           LANGUAGES CXX)

add_executable(ProjectName WIN32 ${PROJECT_SOURCE_DIR}/source/ProjectName.cpp 
                             ${PROJECT_SOURCE_DIR}/source/ui/MainWindow.cpp)

target_include_directories(ProjectName PRIVATE ${PROJECT_SOURCE_DIR}/source)
target_include_directories(ProjectName PRIVATE ${PROJECT_SOURCE_DIR}/source/ui)

target_link_libraries(ProjectName PRIVATE wx::net wx::core wx::base)

add_subdirectory(${CMAKE_SOURCE_DIR}/external)

projectName/externals cmakelists.txt

cmake_minimum_required(VERSION 3.23)
add_subdirectory(${CMAKE_SOURCE_DIR}/external/wxWidgets-3.1.6)

Update

我更改了用于构建共享库(DLL)的CMAKE选项并将其设置为OFF。完成此操作后,它会构建一个静态库(LIB)。这似乎解决了我的链接问题。

根据igor的建议,我又一次看了最小的样本cmakelists.txt,并注意到他们使用以下行链接了

target_link_libraries(${PROJECT_NAME} ${wxWidgets_LIBRARIES}) 

我的行(下图)是静态链接并且不使用DLL的方法吗?

target_link_libraries(ProjectName PRIVATE wx::net wx::core wx::base)

如果有人可以帮助我理解为什么它在静态上修复它,我会100%接受该答案。

Problem

I am getting linker errors when building my project. The project has no trouble finding wxWidgets headers. I have looked at the wxWidgets minimal example and recreated it in order to verify that my build from source worked properly. I used cmake in VSCode to do the build using the cmake extension.

The wxWidgets cmake generated fine and the build of wxwidgets appears to have gone fine. I am using the Visual Studio 2022 amd64 toolset to do the compile. (I installed VS2022 Community Edition for the compiler only).

It looks like it is building DLL for the libraries. I would prefer static libraries but for an initial run I didnt think it mattered. I link my application to wx::net wx::core wx::base as recommended. I thought that this would take care of linking when I go to build.

When I go to build my overall application utilizing the wxWidgets library, I get these linker errors.
What am I doing wrong here? Im obviously not configuring something correctly for it to find the library, but I am kind of stumped here.

Linker Errors

[build] ProjectName.obj : error LNK2001: unresolved external symbol "protected: static class wxAppConsole * (__cdecl* wxAppConsoleBase::ms_appInitFn)(void)" -snipped-
[build] ProjectName.obj : error LNK2001: unresolved external symbol "protected: static class wxAppConsole * wxAppConsoleBase::ms_appInstance" -snipped-
[build] MainWindow.obj : error LNK2001: unresolved external symbol "void (__cdecl* wxTheAssertHandler)(class wxString const &,int,class wxString const &,class wxString const &,class wxString const &)"-snipped-
[build] MainWindow.obj : error LNK2001: unresolved external symbol "bool wxTrapInAssert" (?wxTrapInAssert@@3_NA) -snipped-
[build] MainWindow.obj : error LNK2001: unresolved external symbol "class wxMBConv * wxConvLibcPtr" (?wxConvLibcPtr@@3PEAVwxMBConv@@EA) -snipped-
[build] MainWindow.obj : error LNK2001: unresolved external symbol "public: static unsigned __int64 const wxString::npos" (?npos@wxString@@2_KB)-snipped-
[build] MainWindow.obj : error LNK2001: unresolved external symbol "class wxSize const wxDefaultSize" (?wxDefaultSize@@3VwxSize@@B)-snipped-
[build] MainWindow.obj : error LNK2001: unresolved external symbol "class wxPoint const wxDefaultPosition" (?wxDefaultPosition@@3VwxPoint@@B)-snipped-
[build] MainWindow.obj : error LNK2001: unresolved external symbol "char const * const wxFrameNameStr" (?wxFrameNameStr@@3QBDB)-snipped-
[build] C:\path\to\executable\ProjectName.exe : fatal error LNK1120: 9 unresolved externals -snipped-
[build] Build finished with exit code 1 

Project Layout

Pretty basic project layout. I am building a GUI application and I am logically separating out the ui source code.

This is basically a recreation of the minimal wxWidgets example where you have an entry point (Projectname.hpp and ProjectName.cpp provide this) and a main window/frame (MainWindow.hpp and MainWindow.cpp provide this). The ProjectName application will do the wxIMPLEMENT_APP(ProjectName) and will hold a pointer to a MainWindow. The MainWindow is shown using the OnInit Function.

ProjectName/
    build/
    external/
        wxWidgets-3.1.6/
        -CMakeLists.txt
    source/
        ui/
            -MainWindow.hpp
            -MainWindow.cpp
        -ProjectName.hpp
        -ProjectName.cpp
    -CMakeLists.txt

I have compiled the wxWidgets library using their cmake instructions for using a subdirectory in cmake so I could add it to my project. Here are the contents of the CMakeLists.txt's

ProjectName CMakeLists.txt

cmake_minimum_required(VERSION 3.23)

set(MAJOR 0)
set(MINOR 0)
set(PATCH 1)

#Setup Project
project(ProjectNameProject VERSION ${MAJOR}.${MINOR}.${PATCH}
                           DESCRIPTION "ProjectName Descrip"
                           LANGUAGES CXX)

add_executable(ProjectName WIN32 ${PROJECT_SOURCE_DIR}/source/ProjectName.cpp 
                             ${PROJECT_SOURCE_DIR}/source/ui/MainWindow.cpp)

target_include_directories(ProjectName PRIVATE ${PROJECT_SOURCE_DIR}/source)
target_include_directories(ProjectName PRIVATE ${PROJECT_SOURCE_DIR}/source/ui)

target_link_libraries(ProjectName PRIVATE wx::net wx::core wx::base)

add_subdirectory(${CMAKE_SOURCE_DIR}/external)

ProjectName/externals CMakeLists.txt

cmake_minimum_required(VERSION 3.23)
add_subdirectory(${CMAKE_SOURCE_DIR}/external/wxWidgets-3.1.6)

Update

I changed the Cmake option for building a shared library (DLL) and set it to OFF. When this is done, it builds a static library (LIB). This seemed to fix my linking issues.

Per Igor's suggestion, I took another look at the minimal sample CMakeLists.txt and noticed they used the following line for linking

target_link_libraries(${PROJECT_NAME} ${wxWidgets_LIBRARIES}) 

Perhaps my line (below) is the way to link statically and does not work with DLLs?

target_link_libraries(ProjectName PRIVATE wx::net wx::core wx::base)

If someone could help me understand why having it build statically fixed it, I would 100% accept that answer.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

萤火眠眠 2025-01-27 14:58:06

我更改了构建共享库(DLL)的CMAKE选项并将其设置为关闭。完成此操作后,它会构建一个静态库(LIB)。这似乎解决了我的链接问题。

不幸的是,我看不到任何人能够鸣叫以帮助理解为什么这是解决方案,但是我将选择这作为将来其他人的答案。

如果我看到任何更新回答了“为什么”的问题,我将接受他们的答案。

I changed the Cmake option for building a shared library (DLL) and set it to OFF. When this is done, it builds a static library (LIB). This seemed to fix my linking issues.

Unfortunately I do no see anyone able to chime in to help understand why this is the solution, but I am going to select this as an answer for others coming to this in the future.

If I see any updates that answer the question of "why" I will accept their answer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文