Quence
Quence

Reputation: 48

Elegant solution to VS code C/C++ include path for both intelliSense and building project

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)

Enviroments:

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

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

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

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

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"
      ]
...

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.

Upvotes: 0

Views: 2240

Answers (2)

Ryuzaki Lawliet
Ryuzaki Lawliet

Reputation: 61

I would suggest you, go with this extension:

CPH Judge

As it will clear all the problems of the debugger and all u can input direct values in input and get output!!

Pros:

  1. It has a test case option as we have in online exams, so you can type a and to the test case given in the question and check your answer, accordingly!
  1. You can run multiple test cases also!!

Upvotes: -1

HolyBlackCat
HolyBlackCat

Reputation: 96719

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 is configured by a single file called compile_commands.json, which is just a list of compiler flags for each source file.

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.

Upvotes: 2

Related Questions