# How to prevent CMake linking system libraries?

**URL:** https://discourse.cmake.org/t/how-to-prevent-cmake-linking-system-libraries/6328
**Category:** Code
**Created:** [August 24, 2022, 12:56pm UTC](https://discourse.cmake.org/t/how-to-prevent-cmake-linking-system-libraries/6328 "2022-08-24T12:56:50Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Kulagin](https://discourse.cmake.org/user_avatar/discourse.cmake.org/kulagin/32/350_2.png) [@Kulagin](https://discourse.cmake.org/u/Kulagin)
#### Post date: [August 24, 2022, 12:56pm UTC](https://discourse.cmake.org/t/how-to-prevent-cmake-linking-system-libraries/6328/1 "2022-08-24T12:56:50Z")

</div>

I’ll use [CMake’s example project](https://cmake.org/cmake/help/latest/guide/tutorial/A%20Basic%20Starting%20Point.html) as an example. So I have this:

```auto
cmake_minimum_required(VERSION 3.10)

# set the project name
project(Tutorial)

# add the executable
add_executable(Tutorial tutorial.h)

set_target_properties(Tutorial PROPERTIES LINKER_LANGUAGE CXX)

```

After I generate the solution, when I open the solution in Visual Studio and go to `Project Properties - Configuration Properties - Linker - Input - Additional Dependencies`, I see that it links a lot of libraries :

 ![devenv_uOvRY02ufm](https://discourse.cmake.org/uploads/default/original/2X/c/c5fd5fe64311cf91c91524d82c81e261f8fc1ad4.png)

I’d like to prevent user32.lib from linking for this specific project(not for every project in the solution). I tried googling and found this thread: [How to avoid linking to system libraries](https://discourse.cmake.org/t/how-to-avoid-linking-to-system-libraries/2581). But I couldn’t find a solution.

The reason I’d like to do this is because I’m trying to not link user32.lib in my test project, so I can do the link substitution(also known as link seam) technique to be able to provide my own implementation in the test project, to mock the system calls to be able to test classes that do these system calls.  
It already works: I removed the library in the Visual Studio’s project properties(as well as added it to the list in the `Ignore Specific Default Libraries` property), but the problem is that every time the solution is regenerated, the linking of the library gets restored.

---

<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: [August 24, 2022, 1:35pm UTC](https://discourse.cmake.org/t/how-to-prevent-cmake-linking-system-libraries/6328/2 "2022-08-24T13:35:02Z")

</div>

It seems it comes from `CMAKE_<LANG>_STANDARD_LIBRARIES_INIT`. I think this is a project-wide variable and can’t really be removed for just some targets as it is part of the toolchain rather than target properties. You might be able to remove it from the list and add it where needed to all other targets though.

Cc: @brad.king in case there’s some more canonical way to do this.

---

<div class="post-metadata">

### Author: ![Kulagin](https://discourse.cmake.org/user_avatar/discourse.cmake.org/kulagin/32/350_2.png) [@Kulagin](https://discourse.cmake.org/u/Kulagin)
#### Post date: [August 24, 2022, 2:38pm UTC](https://discourse.cmake.org/t/how-to-prevent-cmake-linking-system-libraries/6328/3 "2022-08-24T14:38:24Z")

</div>

> [@ben.boeckel](#):
>
> It seems it comes from `CMAKE_<LANG>_STANDARD_LIBRARIES_INIT`. I think this is a project-wide variable and can’t really be removed for just some targets as it is part of the toolchain rather than target properties.

Thanks for the answer. I managed to fixed it.

I just tried to add `SET(CMAKE_CXX_STANDARD_LIBRARIES_INIT "")` line before `project(Tutorial)` line and it didn’t change anything: all the libraries were still there.

Then I googled `CMAKE_<LANG>_STANDARD_LIBRARIES_INIT` and found this stack overflow question: [Why CMake adds unnecessary libraries to Visual studio project?](https://stackoverflow.com/questions/41400536/why-cmake-adds-unnecessary-libraries-to-visual-studio-project).

In the comments the guy who asked the question said he could change the `CMAKE_CXX_STANDARD_LIBRARIES` variable. I set this one to be empty, too: `SET(CMAKE_CXX_STANDARD_LIBRARIES "")` and this time it worked!

Also, the whole solution keeps working: it actually still links all those libraries because of _**%(AdditionalDependencies)**_ and the _ **Inherit from parent or projects defaults** _ checkbox:

 ![devenv_2Zq9CfvuaT](https://discourse.cmake.org/uploads/default/original/2X/4/4549b3748da285b0befd00bec06039c6140b3f10.png)

And now I can actually exclude the user32.lib from linking in my test project with:  
SET\_TARGET\_PROPERTIES(${MY\_PROJECT\_NAME} PROPERTIES LINK\_FLAGS “/NODEFAULTLIB:User32.lib”)

And provide my own mock implementations.

I think it might be a mistake to explicitly link all those libraries like CMake does now, because Microsoft’s build system already links all of those by default, there’s no need to link them manually and, apparently, it only interferes if I wanna change something, like in my case. I think a good default value for this variable would be “”, because it just works out of the box and everything is still linked.

---

<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: [August 24, 2022, 5:43pm UTC](https://discourse.cmake.org/t/how-to-prevent-cmake-linking-system-libraries/6328/4 "2022-08-24T17:43:54Z")

</div>

> [@Kulagin](#):
>
> I think it might be a mistake to explicitly link all those libraries like CMake does now, because Microsoft’s build system already links all of those by default, there’s no need to link them manually and, apparently, it only interferes if I wanna change something, like in my case. I think a good default value for this variable would be “”, because it just works out of the box and everything is still linked.

There’s a compatibility hazard here in that some projects might be relying on the libraries being there. Particularly for those not using Visual Studio. But I suppose that it’s something to consider at least.

---

<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: [August 24, 2022, 5:44pm UTC](https://discourse.cmake.org/t/how-to-prevent-cmake-linking-system-libraries/6328/5 "2022-08-24T17:44:21Z")

</div>

> [@Kulagin](#):
>
> SET\_TARGET\_PROPERTIES(${MY\_PROJECT\_NAME} PROPERTIES LINK\_FLAGS “/NODEFAULTLIB:User32.lib”)

FWIW, `target_link_options` is probably better here.
