I’ve been trying to learn C++ and I started to learn about GUIs, but I’ve encountered some problems.
I am using wxWidgets 3.2.5, downloaded it by source on the official website but I can’t get It to work – whenever I try to run the example you can download on GitHub (https://github.com/lszl84/wx_cmake_template/tree/master) It says 'wx/wxprec.h'
file not found.
You can correct the error by including the complete path to the #include, but the nested #include in the .h files will now give you file not found (ex. I corrected 'wx/wx.h'
to /Users/alessandrodoro/wx_cmake_template/wxWidgets-3.2.5/include/wx/wx.h"
, but inside wx.h there are ~81 #include 'wx/...'
and every single one of them gives me error).
I tried including the workspace to path in C/C++ configurations as ${workspaceFolder}/src/**
/Users/alessandrodoro/wx_cmake_template/wxWidgets-3.2.5/include/wx/**
But it does not seem to change anything.
I’m using Mac Ventura 13.4.1 (M1) with VSCode
My main.cpp file is:
// wxWidgets "Hello world" Program
// For compilers that support precompilation, includes "wx/wx.h".
//#include <wx/wxprec.h>
#include "/Users/alessandrodoro/wx_cmake_template/wxWidgets-3.2.5/include/wx/wxprec.h"
#ifndef WX_PRECOMP
//#include <wx/wx.h>
#include "/Users/alessandrodoro/wx_cmake_template/wxWidgets-3.2.5/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);
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, ginger!");
}
My c_cpp_properties.json reads:
{
"configurations": [
{
"name": "Mac",
"includePath": [
"${workspaceFolder}/src/**",
"/Users/alessandrodoro/wx_cmake_template/wxWidgets-3.2.5/include/wx/**"
],
"defines": [
"__WXOSX_COCOA__",
"__WXMAC__",
"__WXOSX__"
],
"macFrameworkPath": [],
"compilerPath": "/usr/bin/clang++",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "clang-x64"
},
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/src/**",
"${workspaceFolder}/build/stage/include/**"
],
"defines": [
"__WXMSW__",
"_UNICODE",
"_DEBUG"
],
"compilerPath": "cl.exe",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "msvc-x64"
},
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/src/**",
"${workspaceFolder}/build/stage/include/wx-3.1/**",
"${workspaceFolder}/build/subprojects/Build/wxWidgets_external/lib/wx/include/gtk3-unicode-static-3.1/**"
],
"defines": [
"__WXGTK__"
],
"compilerPath": "g++",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "gcc-x64"
}
],
"version": 4
}
The launch.json is
{
"version": "0.2.0",
"configurations": [
{
"name": "(lldb-mac) Launch",
"type": "lldb",
"request": "launch",
"program": "${workspaceFolder}/build/subprojects/Build/wx_cmake_template_core/main.app",
"cwd": "${workspaceFolder}",
"preLaunchTask": "Build with CMake"
},
{
"name": "(lldb-linux) Launch",
"type": "lldb",
"request": "launch",
"program": "${workspaceFolder}/build/subprojects/Build/wx_cmake_template_core/main",
"cwd": "${workspaceFolder}",
"preLaunchTask": "Build with CMake"
},
{
"name": "(msvc) Launch",
"type": "cppvsdbg",
"request": "launch",
"program": "${workspaceFolder}\build\subprojects\Build\wx_cmake_template_core\Debug\main.exe",
"cwd": "${workspaceFolder}",
"preLaunchTask": "Build with CMake"
}
]
}
I saw an answer that said to try g++ `wx-config --cxxflags` -o out *.cpp `wx-config --libs`
but it generates this error:
tempCodeRunnerFile.cpp:2:2: error: unterminated conditional directive
#ifndef WX_PRECOMP
I am new to C++ and have little to no idea of what I’m doing – I tried following all the tutorials available to my situation but with no success.
All the help is appreciated.