# How to link to wxWidgets static libs

**URL:** <https://discourse.cmake.org/t/how-to-link-to-wxwidgets-static-libs/8876>\
**Category:** Code\
**Tags:** os:windows\
**Created:** [September 3, 2023, 9:37pm UTC](https://discourse.cmake.org/t/how-to-link-to-wxwidgets-static-libs/8876 "2023-09-03T21:37:02Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Saviz](https://discourse.cmake.org/user_avatar/discourse.cmake.org/saviz/32/6194_2.png) [@Saviz](https://discourse.cmake.org/u/Saviz)\
**Post date:** [September 3, 2023, 9:37pm UTC](https://discourse.cmake.org/t/how-to-link-to-wxwidgets-static-libs/8876/1 "2023-09-03T21:37:02Z")

</div>

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

```auto
# 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?

---

<div class="post-metadata">

**Author:** ![retif](https://discourse.cmake.org/user_avatar/discourse.cmake.org/retif/32/1776_2.png) [@retif](https://discourse.cmake.org/u/retif)\
**Post date:** [September 4, 2023, 9:26am UTC](https://discourse.cmake.org/t/how-to-link-to-wxwidgets-static-libs/8876/2 "2023-09-04T09:26:36Z")

</div>

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](https://github.com/wxWidgets/wxWidgets/blob/master/build/cmake/install.cmake) (_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](https://github.com/wxWidgets/wxWidgets/blob/master/build/cmake/wxWidgetsConfig.cmake.in#L37) isn’t satisfied.

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

---

<div class="post-metadata">

**Author:** ![Saviz](https://discourse.cmake.org/user_avatar/discourse.cmake.org/saviz/32/6194_2.png) [@Saviz](https://discourse.cmake.org/u/Saviz)\
**Post date:** [September 4, 2023, 7:46pm UTC](https://discourse.cmake.org/t/how-to-link-to-wxwidgets-static-libs/8876/3 "2023-09-04T19:46:05Z")

</div>

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](https://www.wxwidgets.org/downloads/)

  
  

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](https://cmake.org/cmake/help/latest/module/FindwxWidgets.html)

  
  

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.

---

<div class="post-metadata">

**Author:** ![retif](https://discourse.cmake.org/user_avatar/discourse.cmake.org/retif/32/1776_2.png) [@retif](https://discourse.cmake.org/u/retif)\
**Post date:** [September 5, 2023, 8:17am UTC](https://discourse.cmake.org/t/how-to-link-to-wxwidgets-static-libs/8876/4 "2023-09-05T08:17:00Z")

</div>

> 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:

```plaintext
> 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.

---

<div class="post-metadata">

**Author:** ![Saviz](https://discourse.cmake.org/user_avatar/discourse.cmake.org/saviz/32/6194_2.png) [@Saviz](https://discourse.cmake.org/u/Saviz)\
**Post date:** [September 6, 2023, 1:55am UTC](https://discourse.cmake.org/t/how-to-link-to-wxwidgets-static-libs/8876/5 "2023-09-06T01:55:38Z")

</div>

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:

  

```auto
add_library(

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

```

---

<div class="post-metadata">

**Author:** ![retif](https://discourse.cmake.org/user_avatar/discourse.cmake.org/retif/32/1776_2.png) [@retif](https://discourse.cmake.org/u/retif)\
**Post date:** [September 6, 2023, 2:43pm UTC](https://discourse.cmake.org/t/how-to-link-to-wxwidgets-static-libs/8876/6 "2023-09-06T14:43:17Z")

</div>

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](https://github.com/microsoft/vcpkg/tree/master/ports/wxwidgets). Looking at the amount of stuff they’ve patched there, I suspect wxWidgets isn’t a very trivial project to be built from sources.
