Multi Process C Open and Read File

Cover image for Setup Visual Studio Code for Multi-File C++ Projects

Talha Balaj

Talha Balaj

Posted on • Updated on

Setup Visual Studio Lawmaking for Multi-File C++ Projects

C++ is a statically-typed general-purpose linguistic communication middle-level programming language and superset of the C programming linguistic communication used everywhere but mainly in systems programming and embedded systems. It's as well used in making games with the Unreal engine.

In this article, I will show how to gear up upward C++ Compiler with Visual Studio Code. I'm writing this tutorial because I didn't discover tutorials with full information in a single place. So here are all the things I have collected from the Internet. I will cover the post-obit things.

Table Of Contents

  • Installing c++ compiler and build-tools
  • Installing VSCode and C++ Extension for VSCode
  • Setting upwardly projection directory
  • Setting up Makefile and adding C++ files
  • Setting upwards VSCode tasks.json
    • Configuring Trouble Matcher
  • Setting upwardly gdb as Debugger

Prerequisites

  • A C++ Compiler (like yard++, clang, etc)
  • make build tool.
  • JSON know-how
  • Visual Studio Code (you can get it here)

I will exist using Linux Operating Arrangement If you are on Windows try this tutorial. For macOS, for the most part, It volition be the aforementioned excluding the installing compiler role. You can cheque it here.

Let's go started.

Compiler and Build Tools Installation

I will be using GNU Compiler Collection so to install gcc, g++, gdb, and make. Y'all can run the following command in the final if you using Ubuntu.

                          $                            sudo              apt              install              build-essential gdb                      

Enter fullscreen mode Exit fullscreen mode

on Fedora, you can run

                          $                            sudo              yum              install              make gcc gcc-c++ kernel-devel                      

Enter fullscreen manner Exit fullscreen fashion

on Arch-based Linux, you can run

                          $                            sudo              pacman              -Syu              base-devel                      

Enter fullscreen style Exit fullscreen mode

Installing VSCode and C++ Extension

Instaling the VSCode is fairly uncomplicated, check out this website and check for instructions to install VSCode on your computer.

One time VSCode is installed, open it. You should see the post-obit screen. Information technology may differ because I take changed my theme and have several other extensions.

Screen1
Now, to install C++ extension goto extensions from the activity bar.

Screen2
Search for C++ in the search bar and install C/C++ Extension By Microsoft.

Screen 3
Now let's movement to ready our project.

Setting Up Project Directory

Having a manageable directory structure is important. It tin assistance y'all rapidly empathize where you lot should look for a file. I opted for the following structure because information technology is uncomplicated.

            ├── bin ├── include └── src                      

Enter fullscreen fashion Exit fullscreen style

The bin directory will comprise the executable after compilation, the include directory will contain all the header files, src will contain all the (as you accept guessed) source files.

Setting up Makefile and adding C++ files

Now that we have a directory structure let's write the Makefile. Open VSCode in your projection directory. Earlier that, Let's see what is a Makefile.

Makefile is read by the make utility which executes the tasks defined in Makefile when the files are modified. We will be using it to build our C++ lawmaking. Y'all can learn more near Makefile and make here.

For our purposes, following Makefile should piece of work well. so create a Makefile in the root of your project directory. It should spell exactly the same with a capital M.

                          CXX              :=              thou++              CXX_FLAGS              :=              -std              =c++17              -ggdb              BIN              :=              bin              SRC              :=              src              INCLUDE              :=              include              LIBRARIES              :=              EXECUTABLE              :=              main              all              :              $(BIN)/$(EXECUTABLE)              run              :              clean all              clear     ./$(BIN)/$(EXECUTABLE)              $(BIN)/$(EXECUTABLE)              :              $(SRC)/*.cpp              $(CXX)              $(CXX_FLAGS)              -I              $(INCLUDE)              $^              -o              $@              $(LIBRARIES)              make clean              :              -              rm              $(BIN)/*                      

Enter fullscreen fashion Get out fullscreen fashion

If you want to add additional libraries, yous tin can add it afterwards the LIBRARIES variable. You tin can also change the name of the executable by irresolute the EXECUTABLE variable. You tin tinker with CXX_FLAGS to modify the compiler'due south behavior (i.e. C++ version) but be sure yous don't remove -ggdb flag or you won't exist able to debug the plan correctly.

Later on creating the file, our directory structure should look like this:

            ├── bin ├── include ├── lib ├── Makefile └── src                      

Enter fullscreen manner Go out fullscreen mode

Now that we accept our base ready permit'south add some C++ files. I volition create a HelloWorld class that has a say() function that will print out (y'all guessed it) "Howdy, World". Let's create files in our project. I'thousand gonna utilise the command line, y'all can utilise VSCode or your file managing director.

                          $                            touch              include/HelloWorld.hpp src/HelloWorld.cpp src/main.cpp                      

Enter fullscreen style Exit fullscreen mode

Paste the post-obit code in the respective files.

Add following to HelloWorld.hpp.

                          #include <iostream>                            form              HelloWorld              {              public:              void              say              ();              };                      

Enter fullscreen mode Leave fullscreen manner

Add following to HelloWorld.cpp.

                          #include "HelloWorld.hpp"                            void              HelloWorld              ::              say              ()              {              std              ::              cout              <<              "Hello, World"              ;              }                      

Enter fullscreen mode Exit fullscreen mode

Add following to main.cpp.

                          #include "HelloWorld.hpp"                            int              master              ()              {              HelloWorld              earth              ;              earth              .              say              ();              return              0              ;              }                      

Enter fullscreen manner Exit fullscreen way

Testing our setup

Now, nosotros have all our project all set upwardly, we accept added some test files. allow's test everything out. Open a last in the project directory and run make.

            ~/Documents/cpp_tut                                                            ▶ make               chiliad++              -std              =c++17              -ggdb              -Iinclude              src/HelloWorld.cpp src/master.cpp              -o              bin/main                      

Enter fullscreen manner Exit fullscreen mode

At present, in the output, we will say the exact control that nosotros would have run if we weren't using make. make has got our dorsum. We don't have to write the whole control each fourth dimension.

Full explanation of this can exist found here.

Setting up VSCode tasks.json

It is however a hurting to open up a last and type brand to build our lawmaking. We can do better than that. For this, we volition be using VSCode's task runner. With this, nosotros can map VSCode'southward build keyboard shortcut to task by making it of type build. The easiest way to fix tasks is to printing ctrl+shift+b. It runs the build task, but as nosotros don't accept any yet it will enquire us to gear up a build task. Later on pressing the push y'all should see the following.

Note: If you printing the shortcut while a C++ file is open, y'all volition encounter VSCode's default C++ build tasks. We don't want to utilise those. So, close the C++ and printing the shortcut once more

4

Select 'Configure Build task'

5

Select 'Create tasks.json from template'

6

Now, select 'Others'. Now you should see a file created, containing the following content.

                          {                                          //                                          See                                          https://become.microsoft.com/fwlink/?LinkId=              733558                                          //                                          for                                          the                                          documentation                                          about                                          the                                          tasks.json                                          format                                          "version"              :                                          "2.0.0"              ,                                          "tasks"              :                                          [                                          {                                          "label"              :                                          "echo"              ,                                          "type"              :                                          "shell"              ,                                          "command"              :                                          "echo Hi"                                          }                                          ]                                          }                                                  

Enter fullscreen mode Exit fullscreen style

tasks.json contains all the tasks that VSCode tin can run for the states. In this case, I have created a task that runs a crush control echo Hello by default. Let's change it to make besides modify the label of the task to better explain the chore.

                          {                                          //                                          Meet                                          https://become.microsoft.com/fwlink/?LinkId=              733558                                          //                                          for                                          the                                          documentation                                          most                                          the                                          tasks.json                                          format                                          "version"              :                                          "2.0.0"              ,                                          "tasks"              :                                          [                                          {                                          "label"              :                                          "build"              ,                                          "type"              :                                          "shell"              ,                                          "command"              :                                          "make"              ,                                          "group"              :                                          {                                          "kind"              :                                          "build"              ,                                          "isDefault"              :                                          true                                          }                                          }                                          ]                                          }                                                  

Enter fullscreen manner Leave fullscreen mode

I besides added a property called grouping where I define what kind of task it is (i.e. build) and if information technology'southward default or non. Setting this is important for the build shortcut to run this task. Now try to build the code with ctrl+shift+b.

Setting up Problem Matcher

Problem matchers scan the task output text for known warning or mistake strings and report these inline in the editor and in the Problems panel. Information technology supports GCC. Then in the tasks.json file add the following in the nether the "grouping" property.

                          "problemMatcher"              :                                          "$gcc"                                                  

Enter fullscreen mode Exit fullscreen mode

Now after running the build task, if whatsoever errors or warnings occur yous will see them in the problems console.

Setting up gdb as debugger

We have successfully built our lawmaking. At present nosotros are going to configure debugger. To add configuration press F5, A carte du jour will popular up asking you to choose which debugging solution you want to apply. Select 'C/C++' and it will generate launch.json in your project directory containing the following config.

                          {                                          //                                          Use                                          IntelliSense                                          to                                          acquire                                          most                                          possible                                          attributes.                                          //                                          Hover                                          to                                          view                                          descriptions                                          of                                          existing                                          attributes.                                          //                                          For                                          more                                          data              ,                                          visit:                                          https://go.microsoft.com/fwlink/?linkid=              830387                                          "version"              :                                          "0.2.0"              ,                                          "configurations"              :                                          [                                          {                                          "name"              :                                          "(gdb) Launch"              ,                                          "type"              :                                          "cppdbg"              ,                                          "request"              :                                          "launch"              ,                                          "plan"              :                                          "enter program name, for example ${workspaceFolder}/a.out"              ,                                          "args"              :                                          [],                                          "stopAtEntry"              :                                          false              ,                                          "cwd"              :                                          "${workspaceFolder}"              ,                                          "surround"              :                                          [],                                          "externalConsole"              :                                          false              ,                                          "MIMode"              :                                          "gdb"              ,                                          "setupCommands"              :                                          [                                          {                                          "clarification"              :                                          "Enable pretty-printing for gdb"              ,                                          "text"              :                                          "-enable-pretty-printing"              ,                                          "ignoreFailures"              :                                          true                                          }                                          ]                                          }                                          ]                                          }                                                  

Enter fullscreen mode Exit fullscreen fashion

We are almost done, here change the "program" property to ${workspaceFolder}/bin/principal. Notation that if you changed the executable proper name change main with the name you wrote in the Makefile. At present, press F5 subsequently running the build, make certain the executable was generated, and resides in the path you just wrote in the "program" property.

In that location is one problem though, you always accept to run build earlier pressing F5 for debugging. Just, we can do improve add the post-obit in the launch.json after "setupCommands" property.

                          "preLaunchTask"              :                                          "build"                                                  

Enter fullscreen style Exit fullscreen mode

This should run the task we defined before starting the debugger. Press F5 and voilĂ , Code builds and debugger starts.

I'yard hoping this tutorial helped you. If you find any mistake or misinformation please allow me know.

Happy coding!

colethavervist1955.blogspot.com

Source: https://dev.to/talhabalaj/setup-visual-studio-code-for-multi-file-c-projects-1jpi

0 Response to "Multi Process C Open and Read File"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel