# Adding standard libraries to use for every link target

**URL:** https://discourse.cmake.org/t/adding-standard-libraries-to-use-for-every-link-target/2519
**Category:** Usage
**Created:** [January 12, 2021, 4:14pm UTC](https://discourse.cmake.org/t/adding-standard-libraries-to-use-for-every-link-target/2519 "2021-01-12T16:14:46Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![Stephan268](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/s/a87d85/32.png) [@Stephan268](https://discourse.cmake.org/u/Stephan268)
#### Post date: [January 12, 2021, 4:14pm UTC](https://discourse.cmake.org/t/adding-standard-libraries-to-use-for-every-link-target/2519/1 "2021-01-12T16:14:46Z")

</div>

I am attempting to build for Windows XP using VS 2015 on Windows 10. Several parts of the code require us to link against Microsofts psapi.lib in this “cross compilation” case. I have attempted to

> set (CMAKE\_C\_STANDARD\_LIBRARIES “${CMAKE\_C\_STANDARD\_LIBRARIES} psapi.lib”)

in a toolchain file but this does not work. Using

> set (CMAKE\_C\_STANDARD\_LIBRARIES\_INIT “${CMAKE\_C\_STANDARD\_LIBRARIES\_INIT} psapi.lib”)

also does not have the desired effect. In both cases psapi.lib is not used on linking.  
Have I misunderstood the documentation ( [CMAKE\_\_STANDARD\_LIBRARIES — CMake 3.19.2 Documentation](https://cmake.org/cmake/help/latest/variable/CMAKE_LANG_STANDARD_LIBRARIES.html))?

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [January 12, 2021, 4:15pm UTC](https://discourse.cmake.org/t/adding-standard-libraries-to-use-for-every-link-target/2519/2 "2021-01-12T16:15:58Z")

</div>

@brad.king

---

<div class="post-metadata">

### Author: ![brad.king](https://discourse.cmake.org/user_avatar/discourse.cmake.org/brad.king/32/11_2.png) [@brad.king](https://discourse.cmake.org/u/brad.king)
#### Post date: [January 12, 2021, 4:32pm UTC](https://discourse.cmake.org/t/adding-standard-libraries-to-use-for-every-link-target/2519/3 "2021-01-12T16:32:36Z")

</div>

See [CMake Issue 20575](https://gitlab.kitware.com/cmake/cmake/-/issues/20575).

The best solution for your use case IMO is to just use `target_link_libraries` to explicitly link `psapi.lib` to whatever target contains the code needing it. Another alternative is the [link\_libraries](https://cmake.org/cmake/help/latest/command/link_libraries.html) command.

---

<div class="post-metadata">

### Author: ![Stephan268](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/s/a87d85/32.png) [@Stephan268](https://discourse.cmake.org/u/Stephan268)
#### Post date: [January 12, 2021, 7:39pm UTC](https://discourse.cmake.org/t/adding-standard-libraries-to-use-for-every-link-target/2519/4 "2021-01-12T19:39:17Z")

</div>

We are trying to keep platform specific aspects out of the code and out of the build specifications, e.g. cmake files as much as possible. To a large degree cmake supports this approach for us. Thanks to the cmake team for this.  
Phrasing a dependency to a library that is only required on windows when “cross compiling” from Windows 10 to Windows XP in inner parts of the build specification did not exite me, so I looked for alternatives. The toolchain file seemed like a good idea for this purpose. Why I did not use link\_libraries? It did not even occur to me that this would be allowed in a toolchain file. Also I found a reference to CMAKE\_C\_STANDARD\_LIBRARIES and thought that it was intended for such a purpose.

---

<div class="post-metadata">

### Author: ![brad.king](https://discourse.cmake.org/user_avatar/discourse.cmake.org/brad.king/32/11_2.png) [@brad.king](https://discourse.cmake.org/u/brad.king)
#### Post date: [January 12, 2021, 7:50pm UTC](https://discourse.cmake.org/t/adding-standard-libraries-to-use-for-every-link-target/2519/5 "2021-01-12T19:50:05Z")

</div>

`link_libraries` shouldn’t be called from a toolchain file. It might work, but it isn’t really meant for that. My suggestion was assuming you’d modify the project code.

You should be able to set `CMAKE_C_STANDARD_LIBRARIES` as a cache entry in the toolchain file with the _complete_ list of libraries needed for that platform, including all the ones CMake normally adds by default.

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [January 12, 2021, 7:58pm UTC](https://discourse.cmake.org/t/adding-standard-libraries-to-use-for-every-link-target/2519/6 "2021-01-12T19:58:21Z")

</div>

One way that you can do this is to create your own `add_library(project_settings INTERFACE)` target that all of your targets link to. You can add `psapi.lib` here as well as any flags you’d like all targets to use as well.

---

<div class="post-metadata">

### Author: ![Stephan268](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/s/a87d85/32.png) [@Stephan268](https://discourse.cmake.org/u/Stephan268)
#### Post date: [January 12, 2021, 7:59pm UTC](https://discourse.cmake.org/t/adding-standard-libraries-to-use-for-every-link-target/2519/7 "2021-01-12T19:59:42Z")

</div>

What I did was to add psapi.dll to the existing content of CMAKE\_C\_STANDARD\_LIBRARIES (see code in first post). This did not work. I also used CMAKE\_C\_STANDARD\_LIBRARIES\_INIT in this fashion without success. I think what happens is:

1. both the variables are empty during processing of the toolchain file.
2. the contents of the variables are overwritten by some part of cmake later. Maybe the code in ./share/cmake-3.17/Modules/Platform/Windows-MSVC.cmake is the “culprit”.

In my toolchain file I also use statements such as

> set (CMAKE\_C\_FLAGS\_INIT “${CMAKE\_C\_FLAGS\_INIT} /D\_USING\_V110\_SDK71\_”)

which work as expected.

---

<div class="post-metadata">

### Author: ![brad.king](https://discourse.cmake.org/user_avatar/discourse.cmake.org/brad.king/32/11_2.png) [@brad.king](https://discourse.cmake.org/u/brad.king)
#### Post date: [January 12, 2021, 8:10pm UTC](https://discourse.cmake.org/t/adding-standard-libraries-to-use-for-every-link-target/2519/8 "2021-01-12T20:10:57Z")

</div>

Being able to set `CMAKE_C_FLAGS_INIT` in a toolchain file such that CMake’s builtin modules only append to it was added by commit [a66004bee0](https://gitlab.kitware.com/cmake/cmake/-/commit/a66004bee06023e9da4e0895ee1afbceaff33bdb) in CMake 3.7. A similar change could be made for `CMAKE_C_STANDARD_LIBRARIES`.

Lacking that, you should be able to use

```cmake
set(CMAKE_C_STANDARD_LIBRARIES "kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib psapi.lib" CACHE STRING "C std libs")

```

in your toolchain file.

---

<div class="post-metadata">

### Author: ![Stephan268](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/s/a87d85/32.png) [@Stephan268](https://discourse.cmake.org/u/Stephan268)
#### Post date: [January 12, 2021, 8:18pm UTC](https://discourse.cmake.org/t/adding-standard-libraries-to-use-for-every-link-target/2519/9 "2021-01-12T20:18:21Z")

</div>

Or I

> set (CMAKE\_EXE\_LINKER\_FLAGS\_INIT “${CMAKE\_EXE\_LINKER\_FLAGS\_INIT} psapi.lib”)  
> set (CMAKE\_SHARED\_LINKER\_FLAGS\_INIT “${CMAKE\_SHARED\_LINKER\_FLAGS\_INIT} psapi.lib”)

I do not really like either solution. On the other hand I do not like to compile for 32 Bit XP in 2020 either!

---

<div class="post-metadata">

### Author: ![brad.king](https://discourse.cmake.org/user_avatar/discourse.cmake.org/brad.king/32/11_2.png) [@brad.king](https://discourse.cmake.org/u/brad.king)
#### Post date: [January 12, 2021, 8:31pm UTC](https://discourse.cmake.org/t/adding-standard-libraries-to-use-for-every-link-target/2519/10 "2021-01-12T20:31:44Z")

</div>

Sure. For reference, being able to set `CMAKE_EXE_LINKER_FLAGS_INIT` and friends in a toolchain file was added by commit [8a98cf6432](https://gitlab.kitware.com/cmake/cmake/-/commit/8a98cf643235a4d2869e1c876f8769b3e6bcd86f) in CMake 3.7. I suggest using

```cmake
string(APPEND CMAKE_EXE_LINKER_FLAGS_INIT " psapi.lib")

```
