Issue
I use cmake 3.22 and presets to configure and build my project. I have a few presets for different compilers defined like below.
How can I get the compiler libraries dir in the CMakeLists.txt file?
I can get ${CMAKE_CXX_COMPILER}
- full path to the compiler, but is it possible to retrieve the path to its libs?
Why can't I read ${GCC_DIR}
at least?
...
"configurePresets": [
{
"name": "tools",
"hidden": true,
"environment": {
"BINUTILS": "/gnu/binutils/binutils-2.37",
"GCC_DIR": "/gnu/gcc/gcc-11.2.0-rh6",
"LLVM_DIR": "/llvm/llvm-14.0",
"NINJA_DIR": "/ninja/ninja-1.7.2"
},
"generator": "Ninja"
},
...
{
"name": "gcc",
"hidden": true,
"inherits": ["tools", "common_settings"],
"environment": {
"PATH": "$env{BINUTILS}/bin:$env{GCC_DIR}/bin:$env{NINJA_DIR}/bin",
"LD_LIBRARY_PATH": "$env{GCC_DIR}/lib64",
"CC": "gcc",
"CXX": "g++"
}
},
...
"buildPresets": [
{
"name": "gcc_debug",
"configurePreset": "gcc_debug"
},
...
Solution
Why can't I read
${GCC_DIR}
at least?
Because you set GCC_DIR
as an environment variable, not a CMake (cache) variable. That syntax only considers CMake variables (both "normal" and cache).
You could write $ENV{GCC_DIR}
to read the environment variable, but I would recommend immediately caching it so that incremental builds don't have to run under the exact same environment as the first configure:
cmake_minimum_required(VERSION 3.21) # for sane cache behavior
project(example)
# ...
set(GCC_DIR "$ENV{GCC_DIR}"
CACHE PATH "Path to GCC libraries")
# ... use ${GCC_DIR} freely ...
Answered By - Alex Reinking Answer Checked By - Marilyn (WPSolving Volunteer)