Compiling multiple C++ files in Visual Studio Code (Ubuntu 22.04)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so I'm here inside Visual Studio code and I have a simple C plus program everything works fine and if I run my program I get the expected result which is Hello from Main CPP which is written here but let's now say that I want to compile several files so I want my program to not only contain this file main.cpp but I want it to also contain other files additional files so how can we do that so first it's worth mentioning that if you're talking about header files you don't have to do anything and it will work out the box so as an example let's add a new header file and let's say that this new header file is to hold the values of some constants so let's call it constants dot h and let's define here my favorite number and that's it that's all I want this header file to contain so let's save it and let's now get back to the main.cbp file and now I'm going to use the new header file now I will be able to use everything that was defined inside the header file so let's try it by using my favorite number and if we run the program we can see hello my favorite number is three and that's because it was defined here inside the header file but what if I want my program to contain several CPP files so let's as an example create a monster class and let's add a method to this class now I want to implement this method and I'm going to do that in a new CPP file let's now create an instance of the monster class in the main file but if we try to run this program it fails and we get an error and you can see here that the problem is undefined reference to this method so when we use this function over here it seemed to work and that's because this function is defined in the header file but during linkage the implementation of the function was needed and since it was in a different CPP file it couldn't be found so let's fix it first let's do it manually if I want to compile several source files using GCC or G plus plus I need to list all of these files so it would look like that so J plus plus and then the list of all of the CPP files that I want to include in my program in this case it's going to be main.cpp and monster.cpp and if we run this line it succeeds and we got this new file a DOT out which is the binary result and if we run it we get the expected result so how can we achieve the same behavior with Visual Studio code we need to make a small change here in this folder.vs code and here we need to edit tasks.json so as you can see the command that we're running is G plus plus just like we did here G plus plus we then specify the arguments to this command and here it is here are the arguments so first let's get rid of this line I'll talk about it later and what we're going to do is replace file which would be main.cpp in our case with something else so let's delete it and when I hit Ctrl space I get all kinds of suggestions we are looking for workspace folder so workspace folder here it is let's choose it and what this variable stands for is this folder the base folder that contains all of our project from this folder we want to include all of the CPP files so forward slash and Then star dot CPP and this will include each and every CPP file from this folder in this case this file main.cpp and also this file monster.cpp let's save the file and if we get back to main.cpp and try to run our program now please the build succeeds and we get the expected result but what about the dash G the flag we had here and removed this flag has to do with debugging so if now instead of running the program I try to debug it we can see that the program never stopped at the break point so even though the program runs successfully we cannot debug it and that's exactly the meaning of the dash G flat CPP files that appear after the dash G flag will have debug information so to allow debugging even though we use several CPP files all we need to do is use the dash G flag so Dash G and let's save this Json file and if we try to debug our program now it stops at the breakpoint and we can debug everything
Info
Channel: Abstract programmer
Views: 27,173
Rating: undefined out of 5
Keywords:
Id: Ar40VjBW3-M
Channel Id: undefined
Length: 5min 51sec (351 seconds)
Published: Fri Feb 10 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.