软件及资源包
下载并安装Vscode 代码编辑器 软件安装后还需在vscode的插件管理中安装C/C++
开发插件
下载并安装MinGW-w64 Windows下的GNU编译器 下载编译器的版一定跟后面下载的wxWidgets的软件包编译使用的版本保持一致
从wxWidgets 下载下图几压缩包
资源包截图
Header Files
是wxWidgets的头文件,Development Files
是编译所需的一些库文件件和动态库,Release DLLs
是编译后的软件,发布给用户时需一起打包发送。不然用户有可能会报缺少dll文件的错误
软件包安装 将上面下载的wxWidgets头文件和开发包,都解压到同一个文件夹下(这里我们以D:\wxWidgets_3.1.5
为例)。解压后目录结构如下:
1 2 3 4 5 6 7 8 wxWidgets_3.1.5/ ├── include │ ├── msvc <---> vc用的头文件我们用MinGW不用该目录 │ └── wx └── lib └── gcc810_dll ├── mswu └── mswud
将D:\wxWidgets_3.1.5\lib\gcc810_dll
添加到环境变量path
中 ,方便生成的应用程序正接运行。
MinGW-w64安装 将上面下工的i686-5.4.0-release-win32-dwarf-rt_v5-rev0.7z
解压到D:\mingw-64\i686-8.1.0-release-win32-sjlj-rt_v6-rev0
目录下。
将D:\mingw-64\i686-8.1.0-release-win32-sjlj-rt_v6-rev0\mingw32\bin
添加到环境变量path
中 ,这样在终端就可以直接使用MinGW的命令了,vscode也可以自动检测了编译器路径。
Vscode编译文件 新建一个工程文件夹,使用vscode打开目录。在vscode中新建一个Hello_wxWidgets.cpp
文件,并粘贴如下代码:
Hello_wxWidgets.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 #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!" ); }
2. 配置Vscode的C/C++编辑属性 在vscode中按 F1 或 Ctrl +Shift +P 输入:c/c++: edit configurations (ui)
将会自在.vscode
目录下生成一个c_cpp_properties.json
文件。:
c_cpp_properties.json
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 { "configurations" : [ { "name" : "Win32" , "includePath" : [ "${workspaceFolder}/**" , "D:\\wxWidgets_3.1.5\\include" , "D:\\wxWidgets_3.1.5\\lib\\gcc810_dll\\mswud" ] , "defines" : [ "_DEBUG" , "UNICODE" , "_UNICODE" ] , "compilerPath" : "D:\\mingw-64\\i686-8.1.0-release-win32-sjlj-rt_v6-rev0\\mingw32\\bin\\gcc.exe" , "cStandard" : "gnu17" , "cppStandard" : "gnu++14" , "intelliSenseMode" : "windows-gcc-x86" } ] , "version" : 4 }
注意:
如果是vc编译就需要添加D:\wxWidgets_3.1.5\include\msvc
如果是gcc编译就需要添加D:\wxWidgets_3.1.5\lib\gcc810_dll\mswud
gcc810_dll\mswud
对应debug
头文件,gcc810_dll\mswu
对应release
头文件
3. 配置编译任务 在vscode中按 F1 或 Ctrl +Shift +P 输入:tasks: configure task
如下图:
tasks.json
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 { "version" : "2.0.0" , "tasks" : [ { "type" : "cppbuild" , "label" : "C/C++: g++.exe 生成活动文件" , "command" : "D:\\mingw-64\\i686-8.1.0-release-win32-sjlj-rt_v6-rev0\\mingw32\\bin\\g++.exe" , "args" : [ "-fdiagnostics-color=always" , "-g" , "${file}" , "-o" , "${fileDirname}\\${fileBasenameNoExtension}.exe" , "-pipe" , "-Wall" , "-ID:\\wxwidgets_3.1.5\\include" , "-ID:\\wxWidgets_3.1.5\\lib\\gcc810_dll\\mswud" , "-LD:\\wxWidgets_3.1.5\\lib\\gcc810_dll" , "-lwxbase31ud" , "-lwxmsw31ud_core" , "-mwindows" ] , "options" : { "cwd" : "${fileDirname}" } , "problemMatcher" : [ "$gcc" ] , "group" : "build" , "detail" : "编译器: D:\\mingw-64\\i686-8.1.0-release-win32-sjlj-rt_v6-rev0\\mingw32\\bin\\g++.exe" } ] }
-ID:\\wxwidgets_3.1.5\\include
和-ID:\\wxWidgets_3.1.5\\lib\\gcc810_dll\\mswud
添加头文件目录,编译的时候会从这些目录中去找头文件。
-LD:\\wxWidgets_3.1.5\\lib\\gcc810_dll
添加库文件目录,链接的时修改会从这些目录中去找库文件。
-lwxbase31ud
和-lwxmsw31ud_core
是链接时引用的库文件。
-mwindows
表示生成windows应用程序,不配置的话启动程序会有一个黑框出现。
-pipe
表示编译时使用管道
-Wall
表示编译时输出全部警告
可以看到编译生成的Hello_wxWidgets.exe
成功运行起来了。