I loaded a project with VS Code onto a Linux server, and got the following error with cmake
. (From the cmake
extension.)
Tell CMake where to find the compiler by setting either the environment
variable "CXX" or the CMake cache entry CMAKE_CXX_COMPILER to the full path
to the compiler, or to the compiler name if it is in the PATH.
I then installed g++-14
and gcc-14
. This did not fix the problem.
I then installed build-essential
, which pulled a copy of gcc-13
and g++-13
via apt
.
The default compilers on the system are GCC Version 13.
$ gcc --version
Version 13...
$ g++ --version
Version 13...
After reloading the VS Code window, cmake
no longer complains. However, the wrong compiler versions are being used. (I know the wrong versions are being used because I can build the project from the command line, however the linter in VS Code complains of missing include paths.)
What should I do to set the CXX compiler correctly?
Should I modify the CMakeList.txt file?
Should I change some configuration within VS Code?
Note that the final build is done inside a Docker container, and the default compiler version inside the container is Version 14. This is why the command line script succeeds, but the VS Code linter thinks there are include path errors.
I had to do a whole bunch of stuff to get this working.
Firstly in the settings.json
: Note: Because I am doing remote development via ssh
, I needed to modify the Remote version of settings.json
.
{
"cmake.configureSettings": {
"CMAKE_C_COMPILER": "/usr/bin/gcc-14",
"CMAKE_CXX_COMPILER": "/usr/bin/g++",
"CMAKE_BUILD_TYPE": "Debug"
}
}
Secondly, in the CMakePresets.json
{
"version": 3,
"configurePresets": [
{
"name": "remote gcc",
"displayName": "Remote gcc",
"description": "Use Remote GCC toolchain",
"generator": "Unix Makefiles",
"binaryDir": "${sourceDir}/out/build/${presetName}",
"cacheVariables": {
"CMAKE_CXX_STANDARD": "23",
"CMAKE_C_COMPILER": "/usr/bin/gcc-14",
"CMAKE_CXX_COMPILER": "/usr/bin/g++-14",
"CMAKE_BUILD_TYPE": "Debug"
}
}
],
"buildPresets": [
{
"name": "remote-gcc",
"configurePreset": "remote-gcc"
}
]
}
This gives a hint as to which compiler cmake
is using.
cat build/CMakeCache.txt | grep -i compiler
# example output...
CMAKE_CXX_COMPILER:FILEPATH=/usr/bin/g++
CMAKE_C_COMPILER:FILEPATH=/usr/bin/gcc
You can also inspect View->Output, and select CMake from the dropdown. Look for log messages which show which compiler is used.
Thirdly, in my Dockerfile
, I needed to add this
cmake -DCMAKE_CXX_COMPILER=/path/to/g++
Technically, this is seperate to the VS Code & Intellisense issue, but it may be worth knowing about.
Fourth, you may need to delete the build cache. (Output directory.)
You can do it manually or with CTRL+SHIFT+P
"CMake: Delete Cache and Reconfigure".
Fifth: create .vscode/c_cpp_properties.json
{
"configurations": [
{
"name": "Remote GCC",
"compilerPath": "/usr/bin/g++-14",
"cppStandard": "c++23",
"intelliSenseMode": "linux-gcc-x64",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [],
"cStandard": "gnu17"
}
],
"version": 4
}
Open Command Pelette