优雅的 VS 代码 C/C++ 解决方案包括用于智能感知和构建项目的路径 [英] Elegant solution to VS code C/C++ include path for both intelliSense and building project

查看:33
本文介绍了优雅的 VS 代码 C/C++ 解决方案包括用于智能感知和构建项目的路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何创建用于构建和调试 C/C++ 项目的 task.jsonlaunch.json,这些参数可以从 c_cpp_configuration.json 自动加载?(包含路径、编译器路径等参数)

How to create task.json and launch.json for building and debugging a C/C++ project, that parameters can be loaded automatically from c_cpp_configuration.json? (parameters such as include path, compiler path)

环境:

Windows 10.0.19041
VS 代码 1.52.1
VS Code 扩展:ms-vscode.cpptools 1.2.2

windows 10.0.19041
VS Code 1.52.1
VS Code extension: ms-vscode.cpptools 1.2.2

假设我有以下文件结构:

Lets say I have the following file structure:

./
 |
 |--- .vscode/
 |
 |--- inc/
 |     |--- header1.h
 |     |--- header2.h
 |
 |--- src/
 |     |--- implementation1.c
 |     |--- implementation2.c
 |
 |--- main.c

我在全局 settings.json 中设置了包含路径:

And I have the include path set in global settings.json:

"C_Cpp.default.includePath": ["${workspaceFolder}/inc"],

智能感知工作正常.我可以从 main.c 中的参考中找到标头的定义.而且我知道我可以在没有 vs 代码的情况下成功构建项目:

The intelliSense works fine. I can find the definition of headers from the reference in main.c. And I know I can have the project built successfully without vs code:

gcc -g .\*.c .\src\*.c -I .\inc

所以,如果我想用 vs code 构建这个项目,我必须像这样设置 task.json:

So, if I want to build this project with vs code, I have to set task.json like this:

...
      "args": [
        "-g",
        "${workspaceFolder}\\*.c", "${workspaceFolder}\\src\\*.c",
        "-I", "${workspaceFolder}\\inc",
        "-o", "${fileDirname}\\${fileBasenameNoExtension}.exe"
      ]
...

对于智能感知和项目构建工作,我必须在 settings.json(c_cpp_configuration.json) 和 task.json 配置路径>.
我想要的是一个优雅的解决方案,我只需要为 intelliSense 和项目构建工作设置一次包含路径.是否可以?如果我可以在没有额外扩展的情况下解决这个问题,那就太棒了.但是您有一个可以替换 ms-vscode.cpptools 的扩展并实现这一点,这也很棒.

For both intelliSense and project build working, I have to configure the paths at settings.json(c_cpp_configuration.json) and task.json.
What I want is a elegant solution that I only have to set the include paths once for both intelliSense and project build working. Is it possible? It would be awesome if I can solve this without extra extensions. But you have a extension that can replace the ms-vscode.cpptools and do the trick, that would also be great.

推荐答案

这就是我要做的:卸载 Microsoft 的 C/C++ 扩展并将其替换为 Clangd 用于代码补全和 本机调试 用于调试.我对这两者有更好的体验,包括更简单的配置.

Here's what I do: uninstall Microsoft's C/C++ extension and replace it with Clangd for code completion and Native Debug for debugging. I had better experience with those two, including easier configuration.

Clangd 由一个名为 compile_commands.json,它只是每个源文件的编译器标志列表.

Clangd is configured by a single file called compile_commands.json, which is just a list of compiler flags for each source file.

然后,开始使用合适的构建系统.例如,CMake 可以立即生成此文件.对于 Make,有一个工具可以生成它.紧要关头,你可以自己写.

Then, start using a proper build system. CMake, for example, can generate this file out of the box. For Make, there's a tool to generate it. In a pinch, you can write it yourself.

这篇关于优雅的 VS 代码 C/C++ 解决方案包括用于智能感知和构建项目的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆