# How to avoid linking to system libraries

**URL:** https://discourse.cmake.org/t/how-to-avoid-linking-to-system-libraries/2581
**Category:** Usage
**Tags:** os:linux
**Created:** [January 20, 2021, 8:27am UTC](https://discourse.cmake.org/t/how-to-avoid-linking-to-system-libraries/2581 "2021-01-20T08:27:16Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![nabw](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/n/f08c70/32.png) [@nabw](https://discourse.cmake.org/u/nabw)
#### Post date: [January 20, 2021, 8:27am UTC](https://discourse.cmake.org/t/how-to-avoid-linking-to-system-libraries/2581/1 "2021-01-20T08:27:16Z")

</div>

Hello everyone, first post!  
Anyway, I am configuring the deal.II library on a server, and there is a nasty linking issue ([https://github.com/dealii/dealii/issues/11245](https://github.com/dealii/dealii/issues/11245)). We have two Boost libraries, one in /lib64 and the one I want (the ‘external’).  
Configuration goes without trouble, but libraries are linked preferently to the objects in /lib64 whenever they are found. In a small test case I can unset implicit libraries and it fixed this (see linked issue):

UNSET(CMAKE\_CXX\_IMPLICIT\_INCLUDE\_DIRECTORIES)  
UNSET(CMAKE\_CXX\_IMPLICIT\_LINK\_DIRECTORIES)  
UNSET(CMAKE\_C\_IMPLICIT\_INCLUDE\_DIRECTORIES)  
UNSET(CMAKE\_C\_IMPLICIT\_LINK\_DIRECTORIES)

but the same fix does not work for the entire library, as implicit libraries still get built and the spurious links happen anyway. I have tried:

1. Unsetting the implicit cmake variables as I’ve shown in the beginning of the cmake configuration
2. Same thing but setting to “” with SET(… “”)
3. Set includes and links to be set BEFORE:  
SET(CMAKE\_INCLUDE\_DIRECTORIES\_BEFORE ON)  
SET(CMAKE\_LINK\_DIRECTORIES\_BEFORE ON)

Nothing works, whenever I check the resulting links with ‘ldd’, I always see that cmake preferred linking to /lib64. How can I avoid this?

Best regards

---

<div class="post-metadata">

### Author: ![fenrir](https://discourse.cmake.org/user_avatar/discourse.cmake.org/fenrir/32/734_2.png) [@fenrir](https://discourse.cmake.org/u/fenrir)
#### Post date: [January 20, 2021, 8:34am UTC](https://discourse.cmake.org/t/how-to-avoid-linking-to-system-libraries/2581/2 "2021-01-20T08:34:40Z")

</div>

The ldd does not show you which library has been used during build time but rather what the runtime linker finds. And this is influenced by few things like RPATH, or the LD\_LIBRARY\_PATH (and few others).  
Check your build commands (-DCMAKE\_VERBOSE\_MAKEFILE=ON) to find out what is used at build time.

---

<div class="post-metadata">

### Author: ![nabw](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/n/f08c70/32.png) [@nabw](https://discourse.cmake.org/u/nabw)
#### Post date: [January 20, 2021, 10:12am UTC](https://discourse.cmake.org/t/how-to-avoid-linking-to-system-libraries/2581/3 "2021-01-20T10:12:33Z")

</div>

Thank you for the suggestion, I didn’t know about that option. I created the make output here:

> **[MAKE\_OUTPUT](https://nabw.site/nextcloud/index.php/s/7yaoetPwLsgF54x)**
>
> Nextcloud - a safe home for all your data

  
and could not find any references to /usr/lib whatsoever. The only place where this reference is mentioned is in the CMakeFiles/3.17.1/CMakeCXXCompiler.cmake file, where I can see all default directories (/lib, /usr/lib, etc) in the CMAKE\_CXX\_IMPLICIT\_INCLUDE\_DIRECTORIES and CMAKE\_CXX\_IMPLICIT\_INCLUDE\_DIRECTORIES variables. The library I am finding can be traced back to the implicit link directories (this is a very narrowed down output):

set(CMAKE\_CXX\_IMPLICIT\_LINK\_DIRECTORIES “/MyDir/glm-0.9.7.1/lib64;many other externals with same structure(symengine, sundials, suite-sparse, etc);/lib64;/usr/lib64; again many external libraries (open ssl, zlib, etc)”)

Implicit include directories look similar, meaning that they have some system libraries in between.

---

<div class="post-metadata">

### Author: ![nabw](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/n/f08c70/32.png) [@nabw](https://discourse.cmake.org/u/nabw)
#### Post date: [January 20, 2021, 10:20am UTC](https://discourse.cmake.org/t/how-to-avoid-linking-to-system-libraries/2581/4 "2021-01-20T10:20:03Z")

</div>

Found this link: [http://cmake.3232098.n2.nabble.com/setting-LINKER-LANGUAGE-still-adds-lstdc-td7581940.html](http://cmake.3232098.n2.nabble.com/setting-LINKER-LANGUAGE-still-adds-lstdc-td7581940.html)  
I am testing it to see if it works, but I think that implicit calls should be called last by default, I have found many references to this problem and solutions are very ad-hoc.

---

<div class="post-metadata">

### Author: ![fenrir](https://discourse.cmake.org/user_avatar/discourse.cmake.org/fenrir/32/734_2.png) [@fenrir](https://discourse.cmake.org/u/fenrir)
#### Post date: [January 20, 2021, 10:46am UTC](https://discourse.cmake.org/t/how-to-avoid-linking-to-system-libraries/2581/5 "2021-01-20T10:46:38Z")

</div>

I took a quick look at the output - the header directory for boost seems to be set correctly (-isystem …/boost-1.72.0-…) but the link directory is not (while it is set for, for example, zlib ( -L …/zlib-1.2.11-…/lib)). That would indicate that the link directory for boost is not added correctly.

---

<div class="post-metadata">

### Author: ![nabw](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/n/f08c70/32.png) [@nabw](https://discourse.cmake.org/u/nabw)
#### Post date: [January 20, 2021, 12:56pm UTC](https://discourse.cmake.org/t/how-to-avoid-linking-to-system-libraries/2581/6 "2021-01-20T12:56:19Z")

</div>

Thanks for helping! So I reconfigured and recompiled by setting the boost directory explicitly. By doing this, when I go to the pkgconfig directory, I see a new line with the link path:

> -L/MyBoost/lib -lboost\_iostreams-mt -lboost\_serialization-mt -lboost\_system-mt -lboost\_thread-mt

but ldd states otherwise:

> $ ldd lib/libdeal\_II.so | grep boost  
> libboost\_iostreams-mt.so.1.53.0 =\> /lib64/libboost\_iostreams-mt.so.1.53.0  
> libboost\_serialization-mt.so.1.53.0 =\> /lib64/libboost\_serialization-mt.so.1.53.0  
> libboost\_regex-mt.so.1.53.0 =\> /lib64/libboost\_regex-mt.so.1.53.0

The package is found merely by using FIND\_PACKAGE and then included with INCLUDE. How can I further specify where to pick up the library?

---

<div class="post-metadata">

### Author: ![fenrir](https://discourse.cmake.org/user_avatar/discourse.cmake.org/fenrir/32/734_2.png) [@fenrir](https://discourse.cmake.org/u/fenrir)
#### Post date: [January 20, 2021, 2:41pm UTC](https://discourse.cmake.org/t/how-to-avoid-linking-to-system-libraries/2581/7 "2021-01-20T14:41:19Z")

</div>

So now it really should be about two things: rpath and runtime environment. Your linker line seems to define RPATH for various libs like zlib: `-Wl,-rpath -Wl,....../zlib-1.2.11-....../lib`. Does it now define a similar thing for boost?  
And even if so, if something is linked indirectly (a dependency of boost library itself) it may still be picked up from system location. In this case you either need to control that dependency build (and set its RPATH correctly) or you set LD\_LIBRARY\_PATH environment variable to point to your libraries.

There’s also a possibility re-configure dynamic linker but it’s system-wide and you almost for sure don’t want it.

---

<div class="post-metadata">

### Author: ![nabw](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/n/f08c70/32.png) [@nabw](https://discourse.cmake.org/u/nabw)
#### Post date: [February 2, 2021, 11:12pm UTC](https://discourse.cmake.org/t/how-to-avoid-linking-to-system-libraries/2581/8 "2021-02-02T23:12:23Z")

</div>

I’m adding this for future reference as I could not solve this: I have seen similar issues and they are always related to using spack in a cluster, so this is very much dependent on how such an environment was built. Of course, more customization makes this harder and harder to track… at least in my experience.  
My final solution was to stop using spack 🙂.
