How to link to wxWidgets static libs

I am attempting to link my project to the wxWidgets library by employing the following sequence of CMake commands:

# Linking to libraray
set(wxWidgets_ROOT_DIR "${CMAKE_SOURCE_DIR}/lib/wxWidgets-3.2.2.1")

set(wxWidgets_USE_STATIC ON)

find_package(
    
    wxWidgets REQUIRED
    
    gl core base
)

include(${wxWidgets_USE_FILE})

target_link_libraries(${PROJECT_NAME} ${wxWidgets_LIBRARIES})

I can successfully build the project in Visual Studio 2022 using MSVC 64-bit without any issues. However, wxWidgets continues to prioritize dynamic libraries despite the presence of the command “wxWidgets_USE_STATIC” turned to ON, instructing it to link to static libraries. How can I address this problem?

Since you are setting wxWidgets_USE_STATIC, I’d guess there are some CMake files in wxWidgets package that are utilizing this variable. What is the package of wxWidgets that you are using, what are its contents? Are you using a pre-built package or are you bundling their sources into your project?

Also, looking at their installation module (are those the right sources?), I believe they produce a CMake config, so you should be able to use CONFIG mode in your find_package() statement. And you are probably missing COMPONENTS keyword there too (not sure if it can be omitted).

There should be a wxWidgetsConfig.cmake within the package (assuming that you are using a pre-built package), and you can check there why this condition isn’t satisfied.

Anyway, there is a chance that it will “just work” after you add CONFIG keyword to your find_package() statement.

1 Like

I am utilizing a pre-built version of wxWidgets specifically designed for Microsoft Visual Studio 2022 (MSVC), configured for a 64-bit environment. The version is the Stable Release (3.2.2.1). To obtain the required header and library files, I simply clicked on the “Windows Binaries” button and downloaded them. You can find the relevant page at the following link:

wxWidgets download page



To the best of my knowledge, the wxWidgets library does not have a configuration file, and the find_package() command relies on the FindwxWidgets method to configure the library. You can find further details on this process on the official CMake page:

FindwxWidgets documentation



Based on the documentation, one can enable the wxWidgets_USE_STATIC variable by setting it to “ON” to instruct the library to use static libraries instead of shared libraries. However, for some unknown reason, this configuration does not take effect. Regrettably, I have been unable to identify the cause of this issue.

To obtain the required header and library files, I simply clicked on the “Windows Binaries”

I’ve checked that download, and indeed it doesn’t contain any CMake files at all. It’s rather strange that they have it in their repository but don’t use them in actual packages.

Okay then, reading that documentation that you linked, I see that:

> There are two search branches: a windows style and a unix style

[...]

> For unix style it uses the wx-config utility. You can select [...] static/shared [...] by turning ON/OFF the following variables

And then there goes this wxWidgets_USE_STATIC among others. But for “windows style” there is no such variable, so I would assume that is why it doesn’t have any effect in your setup.

1 Like

This is quite unfortunate. So, what is the proper way to instruct CMake to utilize static libraries?


Should I employ the add_library() function and manually add every *.h and *.cpp file separately while specifying the STATIC keyword like the following:


add_library(

	${LIBRARY_NAME}    # In this case "wxWidgets"
	
	STATIC
	
	${HEADER_FILES}    # List of header files.
	${SOURCE_FILES}    # List of cpp files. 
)

There are no *.h / *.cpp files in the download that you linked (Windows Binaries).

But in general, that is what I would do - I’d build it from sources, yes. Especially that wxWidgets people say so themselves:

we always recommend building the library from source

But even in that case you wouldn’t need to do add_library() - that is already handled by wxWidgets project files. After you build and install it, I bet you will have a CMake config too, and so you’ll be able to find_package(wxWidgets CONFIG REQUIRED).

To make it easier, I’d probably recommend you to use this vcpkg port. Looking at the amount of stuff they’ve patched there, I suspect wxWidgets isn’t a very trivial project to be built from sources.

1 Like