使用MinGW64与VSCode配置wxWidgets时setup.h缺失错误排查
wxWidgets编译报错:找不到wx/setup.h
已尝试wxWiki及ChatGPT提供的多种方案,无法解决问题。仅需明确当前实现中遗漏的内容,不需要替代方案。
操作步骤
- Windows 11 x64系统通过mingw-builds-binaries安装MinGW64,版本为13.2.0-rt_v11-rev1,使用包
x86_64-13.2.0-release-win32-seh-ucrt-rt_v11-rev1.7z - 创建项目文件夹
myWxProject(记为[%project%]),并新建%project%/libs/wxWidgets(记为[%wxWidgets%])目录 - 从wxWidgets官网下载v3.2.3.5源码7z包,解压至
[%wxWidgets%]目录 - 进入
%wxWidgets%/build/msw/目录,执行命令mingw32-make -f makefile.gcc BUILD=release SHARED=0 UNICODE=1构建;切换BUILD=debug并调整包含路径后仍报错 - 在
%project%/.vscode/添加配置文件:c_cpp_properties.json:{ "configurations": [ { "name": "Win32", "includePath": [ "${workspaceFolder}\\libs\\wxWidgets\\include", "${workspaceFolder}\\libs\\wxWidgets\\lib\\gcc_lib\\mswu" ], "defines": [ "_DEBUG", "UNICODE", "__WXMSW" ], "intelliSenseMode": "gcc-x64", "compilerPath": "C:\\mingw64\\bin\\g++.exe", "cStandard": "c11", "cppStandard": "c++17" } ], "version": 4 }tasks.json:{ "version": "2.0.0", "tasks": [ { "label": "cpp-build-debug", "type": "shell", "command": "g++", "args": [ "-g", "${file}", "-o", "${workspaceFolder}\\cpp-build-debug\\${fileBasenameNoExtension}.exe", "-I", "${workspaceFolder}\\libs/wxWidgets\\include", "${workspaceFolder}\\libs/wxWidgets\\lib\\gcc_lib\\mswu", "-L", "${workspaceFolder}\\libs\\wxWidgets\\lib\\gcc_lib", "-lwxbase32u", "-lwxmsw32u_core", "-lwxmsw32u_adv" ], "group": { "kind": "build", "isDefault": true }, "problemMatcher": [ "$gcc" ] } ] }
- 创建
helloWorld.cpp文件,写入wxWidgets官方Hello World示例代码:// Start of wxWidgets "Hello World" Program #include <wx/wx.h> class MyApp : public wxApp { public: bool OnInit() override; }; wxIMPLEMENT_APP(MyApp); class MyFrame : public wxFrame { public: MyFrame(); private: void OnHello(wxCommandEvent& event); void OnExit(wxCommandEvent& event); void OnAbout(wxCommandEvent& event); }; enum { ID_Hello = 1 }; bool MyApp::OnInit() { MyFrame *frame = new MyFrame(); frame->Show(true); return true; } MyFrame::MyFrame() : wxFrame(nullptr, 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!"); } - 执行
cpp-build-debug任务
报错信息
In file included from %project%\libs/wxWidgets\include/wx/defs.h:45, from %project%\libs/wxWidgets\include/wx/wx.h:14, from %project%\helloWorld.cpp:2: %project%\libs/wxWidgets\include/wx/platform.h:159:10: fatal error: wx/setup.h: No such file or directory 159 | #include "wx/setup.h" | ^~~~~~~~~~~~ compilation terminated.
已确认内容
setup.h存在于%wxWidgets%/lib/gcc_lib/mswu/wx/目录- 该目录已加入
c_cpp_properties.json的包含路径 - 未使用源码解压后
%wxWidgets%/include/msvc下Visual Studio专用的setup.h
补充信息
进入wxWidgets示例目录执行mingw32-make -f makefile.gcc编译minimal程序时,出现相同报错。
遗漏的关键内容
- tasks.json编译参数错误:在
tasks.json的args列表中,${workspaceFolder}\\libs/wxWidgets\\lib\\gcc_lib\\mswu这个包含路径前缺少-I参数。g++仅会将紧跟-I的路径视为包含目录,后续未带-I的路径会被当作源文件处理,导致编译时无法找到该目录下的wx/setup.h。
正确的参数写法应为:"-I", "${workspaceFolder}\\libs/wxWidgets\\include", "-I", "${workspaceFolder}\\libs/wxWidgets\\lib\\gcc_lib\\mswu", - 构建与编译的配置不匹配:构建wxWidgets时使用
BUILD=release,但编译项目时使用-g(debug模式),即使后续切换BUILD=debug,需确保tasks.json中链接的库为debug版本(如-lwxbase32ud、-lwxmsw32u_core_d等),否则可能引发其他隐性问题,但当前报错的直接原因是第一个点。
内容的提问来源于stack exchange,提问作者Rahul Raman
相关产品推荐
相关产品推荐

