← Blog

wxWidgets with CMake FetchContent

Simplify your wxWidgets build with CMake FetchContent. Learn to automatically download, build, and link dependencies in a single CMakeLists.txt.

FetchContent, introduced in CMake 3.11, simplifies the process of downloading and building dependencies. It is particularly useful for integrating libraries like wxWidgets into your project. This is the modern alternative to the ExternalProject superbuild approach covered in an earlier tutorial.

Easy C++ Dependency Management with CMake Play video: Easy C++ Dependency Management with CMake

Here are the key steps to building CMakeLists.txt using this approach:

  1. Specify the minimum CMake version required, noting that FetchContent_MakeAvailable needs at least CMake 3.14.
  2. Define your project and the languages it supports.
  3. Include the FetchContent module and declare wxWidgets as a dependency.
  4. Automatically download, build, and link wxWidgets to your project.
  5. Create your executable and link it to wxWidgets.
cmake_minimum_required(VERSION 3.14 FATAL_ERROR)

project(wx_cmake_fetchcontent_template LANGUAGES CXX)

include(FetchContent)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

set(wxBUILD_SHARED OFF)

message(STATUS "Fetching wxWidgets...")

FetchContent_Declare(
   wxWidgets
   GIT_REPOSITORY https://github.com/wxWidgets/wxWidgets.git
   GIT_TAG v3.2.11
   GIT_SHALLOW ON
)
FetchContent_MakeAvailable(wxWidgets)

set(SRCS main.cpp)

add_executable(main WIN32 ${SRCS})
target_link_libraries(main PRIVATE wxcore wxnet)

Building Your Application

Follow these steps to build the project:

Debug

  1. Create a build directory & configure the build:

    cmake -S. -Bbuild
    
  2. Build the project:

    cmake --build build -j
    

This will create a build directory and compile all necessary artifacts there. The main executable will be located in build/.

Release

For release build use --config Release on Windows:

cmake -S. -Bbuild
cmake --build build -j --config Release

Artifacts for both configurations will be generated in the build directory.

On Mac or Linux you’ll need to maintain two build trees:

cmake -S. -Bbuild -DCMAKE_BUILD_TYPE=Debug
cmake --build build -j
cmake -S. -Bbuild-rel -DCMAKE_BUILD_TYPE=Release
cmake --build build-rel -j

This approach also works seamlessly for creating Debug versions, ensuring you can manage both configurations efficiently.

Handling High DPI Scaling

To ensure proper scaling for high DPI monitors, include the necessary info plist file on Mac and Manifest file on Windows. Check out my additional tutorials for detailed guides on these configurations.

Conclusion

This simplified CMake setup streamlines the development process across platforms and makes dependency management and application compilation much easier. The complete CMake template is available on my GitHub

Happy coding!

Written by Luke. I build custom software for businesses, made for the way your team works. Fixed price, delivered in days or weeks. I've been shipping software since 2008, co-founded Codahead, a software house in Kraków, and I've published C++ tutorials on YouTube for six years.

Recent posts

All
Your First Triangle in Apple Metal: Shaders and Vertex Buffers Oct 2026Draw a triangle with Apple Metal in a Swift playground: write the vertex and fragment shaders, build the render pipeline, send vertices with setVertexBytes or an MTLBuffer, and get the memory alignment right. Your Business Deserves a Native App Oct 2026Many of your team's apps are web pages in disguise, and you pay for it in waiting. When Shopify rebuilt its shopping app as native, it started opening twice as fast on Android. Here's what native could do for your business. XFCE on 4K Displays: Correct Scaling in Five Settings Oct 2026XFCE's Window Scaling option only scales GTK apps. Here are the five settings that make a 4K display look right in GTK, Qt, Firefox and Emacs, plus fractional scaling.