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.
PATHanyway, which meanswhichwon't be able to find it anyway.