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):
"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:
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:
"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:
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.