VSCode IntelliSense 无法理解 CMake add_definitions

vac*_*bun 2 cmake visual-studio-code vscode-extensions

VSCode IntelliSense 无法理解 CMake add_definitions。

为此我能做什么呢。

当我在 CMakeList.txt 中使用 add_definitions 时。

add_definitions(-DTEST_READING_DEV_ID)
Run Code Online (Sandbox Code Playgroud)

在我使用的c文件中

#ifdef TEST_READING_DEV_ID
...
...
#endif
Run Code Online (Sandbox Code Playgroud)

代码块看起来是灰色的。

vac*_*bun 5

使用cmake生成文件compile_commands.json。将其添加到CMakeList.txt

target_compile_definitions(app PRIVATE CMAKE_EXPORT_COMPILE_COMMANDS=1)
Run Code Online (Sandbox Code Playgroud)

构建项目。

将文件添加compile_commands.json到 VSCode 扩展设置文件c_cpp_properties.json

"compileCommands": "${workspaceFolder}/build/compile_commands.json"
Run Code Online (Sandbox Code Playgroud)

本文介绍了 c_cpp_properties.json 设置文件的方案。

https://code.visualstudio.com/docs/cpp/c-cpp-properties-schema-reference

现在,代码块不再是灰色的。