Home > Software engineering >  Dynamic compiler path in c_cpp_properties.json VSCode settings file
Dynamic compiler path in c_cpp_properties.json VSCode settings file

Time:01-11

I have a c_cpp_properties.json in my .vscode folder, and it's working properly. However, there is a line in it that is system specific:

"compilerPath": "~/.espressif/tools/xtensa-esp32-elf/esp-2021r2-patch5-8.4.0/xtensa-esp32-elf/bin/xtensa-esp32-elf-gcc"

And while I can assume default install for the .espressif tools, unfortunately the distributor of the toolchain changes the path on every version (I.e. .../esp-2021r2-patch5-8.4.0/...)

I'm wondering if there's a way to use something like

which xtensa-esp32-elf-gcc

in the json so this will update with the toolchain updates?

CodePudding user response:

I'll start by mentioning that the most easiest way to do this is via the creation / or update of a symbolic link in /usr/bin as rioV8 mentioned in the comments.

The command which xyz will look through the path environment variable anyway, so unless you have a symbolic link under one of the paths listed in path, it won't be as easy.

That being said I can imagine that you may want to (for some specific reason) switch the compiler from time to time (for example when debugging historic versions) and for example you do this by having predefined environments where you specify the path.

So to do this I looked into how VS Code's CMake Tools parse the c_cpp_properties.json file and passing anything other than a string to compilerPath doesn't look like a good idea, however if we take a look at Lines 430-451 of this file. We can see what has priority when deciding which CXX / C compiler to use.

Based on that - what you can do is go to your CMakeLists.txt file and before you define the project you can add this snippet for your own CXX search:

execute_process(COMMAND your_script_or_command_to_get_cxx_path_here RESULT_VARIABLE which_cxx OUTPUT_STRIP_TRAILING_WHITESPACE)
set(CMAKE_CXX_COMPILER ${which_cxx})

project(TEST)

#... rest of your CMakeLists.txt

For example:

execute_process(COMMAND which g   RESULT_VARIABLE which_cxx OUTPUT_STRIP_TRAILING_WHITESPACE)

After you clean and generate a new build folder, it should override whatever is in c_cpp_properties.json

But in all honesty, I would just stick to the symbolic link.

  • Related