How to include wxWidgets headers in CMake project

1,187 views
Skip to first unread message

Jason Liam

unread,
Jun 17, 2021, 9:21:49 AM6/17/21
to wx-users
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.

Maarten Bent

unread,
Jun 17, 2021, 9:33:36 AM6/17/21
to wx-u...@googlegroups.com
Hi,

You need to use FindwxWidgets: https://cmake.org/cmake/help/latest/module/FindwxWidgets.html
On Linux this uses 'wx-config' internally. Then use something like this in your cmake file:

find_package(wxWidgets REQUIRED)
target_include_directories(
<YourTarget> PUBLIC ${wxWidgets_INCLUDE_DIRS})
target_compile_definitions(
<YourTarget> PUBLIC ${wxWidgets_DEFINITIONS})
target_compile_options(
<YourTarget> PRIVATE ${wxWidgets_CXX_FLAGS})
target_link_libraries(<YourTarget> ${wxWidgets_LIBRARIES})

Maarten
--
Please read https://www.wxwidgets.org/support/mlhowto.htm before posting.
---
You received this message because you are subscribed to the Google Groups "wx-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to wx-users+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/wx-users/78bc42fb-7875-46a4-b0bc-ee5242187c8en%40googlegroups.com.

Jason Liam

unread,
Jun 18, 2021, 12:35:39 AM6/18/21
to wx-u...@googlegroups.com
The program is working with the code you suggested(using find_package()) but i have a question. I usually add external c++ libraries in my CMake c++ project using the add_library() command and the target_include_directories() and then target_link_libraries() . These steps works for other libraries. Is it possible to do this for wxWidgets also? I am using Linux and i want to create an executable(for windows) and a binary(for linux) that contains everything(including external dependencies) in it. Is there a step by step guide for do this with wxWidgets library in CMake.

Igor Korot

unread,
Jun 18, 2021, 1:34:47 AM6/18/21
to wx-u...@googlegroups.com
Hi,

On Thu, Jun 17, 2021 at 11:35 PM Jason Liam <jlam...@gmail.com> wrote:
>
> The program is working with the code you suggested(using find_package()) but i have a question. I usually add external c++ libraries in my CMake c++ project using the add_library() command and the target_include_directories() and then target_link_libraries() . These steps works for other libraries. Is it possible to do this for wxWidgets also? I am using Linux and i want to create an executable(for windows) and a binary(for linux) that contains everything(including external dependencies) in it. Is there a step by step guide for do this with wxWidgets library in CMake.

CMake thingy is relatively new for wxWidgets.
You should just follow the code on the web.

The difference comes from the fact that wxWidgets is using its own
script - wx-config to build the software on *nix
and something completely different for Windows (for all its different
compilers).

JUst use the macros suggested and everything will just work out of the box.

Moreover it is officially supported by CMake (IIUC).

Thank you.
> To view this discussion on the web visit https://groups.google.com/d/msgid/wx-users/CA%2BuK%3DSA33AzS5-KFfZvrEFdEUTx2Z6fJJx3ZMCSOd30dx38rpQ%40mail.gmail.com.

Kenneth Porter

unread,
Dec 7, 2021, 10:31:31 AM12/7/21
to wx-u...@googlegroups.com
--On Thursday, June 17, 2021 4:33 PM +0200 Maarten Bent
<maart...@gmail.com> wrote:

> You need to use FindwxWidgets:
> https://cmake.org/cmake/help/latest/module/FindwxWidgets.html
> On Linux this uses 'wx-config' internally. Then use something like this
> in your cmake file:
>
> find_package(wxWidgets REQUIRED)

How does the wxWidgets installation tell CMake where it's been placed? I've
always built Visual Studio projects and added to the include and library
paths using $(WXWIN)\include (for example), with the installation location
set as a system environment variable. Now I'm trying my hand at starting
from a CMake project and letting it build my VS project.

How would I configure CMake to know this? Ideally without specifying it on
the generator command line. Is there a master CMake config directory or can
it infer from the environment variable?

Also, how does CMake learn the specific library directory for the build
type (debug, build system, architecture)?



PB

unread,
Dec 7, 2021, 11:53:19 AM12/7/21
to wx-users
Hi,

On Tuesday, December 7, 2021 at 4:31:31 PM UTC+1 SpareSimian wrote:
How does the wxWidgets installation tell CMake where it's been placed?
...

Also, how does CMake learn the specific library directory for the build
type (debug, build system, architecture)?

I believe many of CMake wxWidgets-related questions are answered in the official module documentation at https://cmake.org/cmake/help/latest/module/FindwxWidgets.html?highlight=findwxwidgets

Some questions can be answered only by looking at the module itself, for example how it presets wxWidgets_ROOT_DIR is here: https://github.com/Kitware/CMake/blob/28fa21dc833f551682dfc7c3551408be561c3c3a/Modules/FindwxWidgets.cmake#L450

Additionally, as I am not really knowledgeable about CMake, so I have always found useful CMakeGUI, which lets me see what options are actually there.

Regards,
PB
Reply all
Reply to author
Forward
0 new messages