1

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?

6
  • Are you using the IDF extension? Commented Jan 10, 2023 at 7:59
  • You don't get symbolic links (or similar) in a common "bin" directory? If you don't get that, then it's likely the compiler won't be in the PATH anyway, which means which won't be able to find it anyway. Commented Jan 10, 2023 at 8:03
  • 1
    create a symbolic link to a directory of the compiler, update the symlink after each update of the compiler Commented Jan 10, 2023 at 8:17
  • I don't think VSCode will search for you compiler for you, that's why the json setting exists. You could write a shell script to update the json each time you reinstall the compiler, but I think rioV8's suggestion of having fixed json and a symlink is better. Commented Jan 10, 2023 at 8:43
  • I think I have an idea on how to achieve what you wish to do. I will answer this once I'm at a computer. Commented Jan 10, 2023 at 11:01

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Wow, thank you so much! what a great answer. I believe I have it working using this method, which for my use case, is definitely preferable

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.