← 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
SwiftUI & Metal: Rotating a Triangle with a Slider Oct 2026Connect a SwiftUI slider to an Apple Metal renderer: pass the value with @State and @Binding, turn it into a float4x4 rotation matrix, and send it to the vertex shader with setVertexBytes. Apple Metal on Your Phone: Bridging SwiftUI and MTKView Oct 2026Move Apple Metal code from a Swift playground to a real SwiftUI app for iPhone and Mac: wrap MTKView with UIViewRepresentable and NSViewRepresentable, and render every frame from an MTKViewDelegate. Vertex Descriptors in Apple Metal: Attributes, Offsets and Indexed Drawing Oct 2026Use MTLVertexDescriptor so your Swift and Metal vertex structs don't have to match, calculate attribute offsets the right way with MemoryLayout.offset(of:), and draw a square from four vertices with an index buffer.