1

I am writing a sample "Hello World" code from the wxWidgets documentation. I had installed the wxwidgets from source with gtk configured. After that I had installed it system wide. Below were the CLI commands entered by me:

mkdir gtk-build
cd gtk-build
../configure –-with-gtk
make
sudo su
make install
ldconfig
exit
cd samples
make

The executables in the samples directory work fine for me. Also I see that a wx directory is created in usr/local/include where I see a "wx.h" file. But when I include it in my .cpp file and compile using g++, I get following error:

hello.cpp:1:9: fatal error: wx/wx.h: No such file or directory
    1 | #include<wx/wx.h>

Do I need to link the wx folder while compiling in any way? How to do that? Also if I want to use a CMakeList to build and compile the code, what arguments apart from add executable will I need to run the basic "hello world" code using wxWidgets?

The code is used from official documentation as follows:

#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);

};

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

enum{
    ID_Hello = 1
};

MyFrame::MyFrame() : wxFrame(nullptr, wxID_ANY, "Hello World"){
    wxMenu *menuFile = new wxMenu;
    menuFile->Append(ID_Hello, "&Hello...\tCtrl-H", "Hello string is shown in status bar");
    menuFile->AppendSeparator();
    menuFile->Append(wxID_EXIT);
    
    xwMenu *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 widgets");
    
    Bind(wxEVT_MENU, &MyFrame::OnHello, this, ID_Hello);
    Bind(wxEVT_MENU, &MyFrame::OnExit, this, wxID_EXIT);
    Bind(wxEVT_MENU, &MyFrame::OnAbout, this, wxID_ABOUT);
    }
    
void myFrame::OnExit(wxCommandEvent& event){
    Close(true);
}


void myFrame::OnAbout(wxCommandEvent& event){
    wxMessageBox("About Hello World!", wxOK | wxICON_INFORMATION);
}


void myFrame::OnHello(wxCommandEvent event){
    wxLogMessage("Hello world from widgets");
}

I am compiling the code using CMakeList.txt as follows:

cmake_minimum_required(VERSION 3.10)
project(Basic)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED True)
add_executable(base hello.cpp)
target_include_directories(base PUBLIC "${PROJECT_BINARY_DIR}")

I am performing an out of source build using command cmake --build .

10
  • 1
    Compile command?
    – sweenish
    Jun 2 at 4:26
  • @sweenish add_executable(test hello.cpp) and the cmake--build . Jun 2 at 4:28
  • I missed that you're using cmake. Given that this is a build issue, the CMakeLists.txt seems relevant. edit your question.
    – sweenish
    Jun 2 at 4:31
  • 1
    I don't see find_package(wxWidgets ... anywhere in your code.
    – arrowd
    Jun 2 at 5:18
  • 1
    Your compiler does not look in /usr/local/include by default. But you should use the shipped CMake configuration file as @arrowd said.
    – Botje
    Jun 2 at 7:36

1 Answer 1

0

I found I had to repeat the WXCXXFLAGS in my Makefile.

WXCXX      := $(shell wx-config --cxx)
WXCXXFLAGS := $(shell wx-config --cxxflags)

.cpp.o:
    $(WXCXX) $(CXXFLAGS) $(WXCXXFLAGS) $(WXCXXFLAGS) -o $@ -c $<

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.