# Use non-default libs with non-default compiler on Linux

**URL:** https://discourse.cmake.org/t/use-non-default-libs-with-non-default-compiler-on-linux/15883
**Category:** Code
**Created:** [October 22, 2026, 6:26am UTC](https://discourse.cmake.org/t/use-non-default-libs-with-non-default-compiler-on-linux/15883 "2026-10-22T06:26:51Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![ixsci](https://discourse.cmake.org/user_avatar/discourse.cmake.org/ixsci/32/2041_2.png) [@ixsci](https://discourse.cmake.org/u/ixsci)
#### Post date: [October 22, 2026, 6:26am UTC](https://discourse.cmake.org/t/use-non-default-libs-with-non-default-compiler-on-linux/15883/1 "2026-10-22T06:26:51Z")

</div>

I’m trying to build an app with non-system-wide clang & gcc. And while for gcc I just have to set the correct compiler and everything works correctly, with clang I have a different story. I use the following preset (only var parts):

```json
"cacheVariables": {
        "CMAKE_C_COMPILER": "clang-23",
        "CMAKE_CXX_COMPILER": "clang++-23",
        "CMAKE_CXX_FLAGS": "--gcc-install-dir=/opt/gcc-16.2.0/lib/gcc/x86_64-linux-gnu/16.2.0"
      }

```

If I build the app it will link against an undesirable system-wide libstdc++: `-L/usr/lib/gcc/x86_64-linux-gnu/15`. As I understand it, this link is inferred from `CMAKE_CXX_IMPLICIT_LINK_DIRECTORIES ` which looks like this for me:

```cmake
set(CMAKE_CXX_IMPLICIT_LINK_DIRECTORIES "/usr/lib/gcc/x86_64-linux-gnu/15;/usr/lib64;/lib/x86_64-linux-gnu;/lib64;/usr/lib/x86_64-linux-gnu;/lib;/usr/lib")

```

I can fix it by providing the link path as follows:

```json
"cacheVariables": {
        "CMAKE_C_COMPILER": "clang-23",
        "CMAKE_CXX_COMPILER": "clang++-23",
        "CMAKE_CXX_FLAGS": "--gcc-install-dir=/opt/gcc-16.2.0/lib/gcc/x86_64-linux-gnu/16.2.0",
        "CMAKE_EXE_LINKER_FLAGS": "-L/opt/gcc-16.2.0/lib64"
      }

```

And now the implicit var will look like:

```cmake
set(CMAKE_CXX_IMPLICIT_LINK_DIRECTORIES "/opt/gcc-16.2.0/lib64;/opt/gcc-16.2.0/lib/gcc/x86_64-linux-gnu/16.2.0;/lib/x86_64-linux-gnu;/lib64;/usr/lib/x86_64-linux-gnu;/usr/lib64;/lib;/usr/lib")

```

After that change, the app links against a desirable library and everything works.

Is this the way to do it, variable-wise? Or is there a better way to achieve what I want?  
While I showed a preset here I will most likely do it with a toolchain file so my question is only about variables/commands to use, not where I should set them.
