Issue
I am trying to run wxWidget example using cmake but unable to include the headers of wxWidgets in my C++ project(CMakeLists.txt). If i run the program using the command
g++ main.cpp `wx-config --cppflags --libs` -o wxTest
the program works and display the window. But how can i do that using CMakeLists.txt file. For example, usually i create a separate folder called external-libs and then inside it create a folder with the name whateverlibraryname and then inside it create a header and src and lib folder where i place the header files, any source files and .so files respectively. But in the case of wxWidgets i have several static library files and also inside the header there are many separate folders and i don't know how to include them all. They produce the error:
fatal error: wx/wx.h: No such file or directory
#include <wx/wx.h>
I am using Ubuntu 18.04 and my project directory structure is as follows:
├── build
└── source
├── CMakeLists.txt
├── external-libraries
│ └── wxWidgets
│ ├── headers
│ │ ├── msvc
│ │ │ └── wx
│ │ └── wx
│ │ ├── android
│ │ ├── aui
│ │ ├── dfb
│ │ ├── generic
│ │ ├── gtk
│ │ ├── gtk1
│ │ ├── html
│ │ ├── meta
│ │ ├── motif
│ │ ├── msw
│ │ ├── osx
│ │ ├── persist
│ │ ├── private
│ │ ├── propgrid
│ │ ├── protocol
│ │ ├── qt
│ │ ├── ribbon
│ │ ├── richtext
│ │ ├── stc
│ │ ├── univ
│ │ ├── unix
│ │ ├── x11
│ │ ├── xml
│ │ └── xrc
│ ├── lib
├── libwx_baseu-3.1.a
│ │ ├── libwx_baseu_net-3.1.a
│ │ ├── libwx_baseu_xml-3.1.a
│ │ ├── libwx_gtk3u_adv-3.1.a
│ │ ├── libwx_gtk3u_aui-3.1.a
│ │ ├── libwx_gtk3u_core-3.1.a
│ │ ├── libwx_gtk3u_gl-3.1.a
│ │ ├── libwx_gtk3u_html-3.1.a
│ │ ├── libwx_gtk3u_propgrid-3.1.a
│ │ ├── libwx_gtk3u_qa-3.1.a
│ │ ├── libwx_gtk3u_ribbon-3.1.a
│ │ ├── libwx_gtk3u_richtext-3.1.a
│ │ ├── libwx_gtk3u_stc-3.1.a
│ │ ├── libwx_gtk3u_xrc-3.1.a
│ │ ├── libwxjpeg-3.1.a
│ │ ├── libwxregexu-3.1.a
│ │ ├── libwxscintilla-3.1.a
│ │ └── libwxtiff-3.1.a
│ └── src
├── main.cpp
Main.cpp has #include<wx/wx.h>
at the top. I am using VSCode and when i ran the program using g++(command described above) it works but doesn't workwhen i run the same program using CMake. That is how to include the header folder and use the lib folder that contains all the wxWidgets headers and library files. What should be the contents of the CMakeLists.txt files and what are the other necessary things that i have to do to make this work.
Solution
CMake has first-party support for wxWidgets, here's an example:
cmake_minimum_required(VERSION 3.20)
project(wxTest)
find_package(wxWidgets REQUIRED gl core base OPTIONAL_COMPONENTS net)
include(${wxWidgets_USE_FILE})
add_executable(wxTest main.cpp)
target_link_libraries(wxTest PRIVATE ${wxWidgets_LIBRARIES})
It's a bit unfortunate how legacy this module is. The above code would be much better off using imported targets and not need to do this weird dance with include(${wxWidgets_USE_FILE})
, but alas. At least it's documented.
There are various variables you can set to help it find your wxWidgets installation. See the documentation for more details: https://cmake.org/cmake/help/latest/module/FindwxWidgets.html
Answered By - Alex Reinking Answer Checked By - Pedro (WPSolving Volunteer)