 Hi, welcome to Visual Studio Toolbox. I'm your host Robert Green, and joining me today is Rong Lu. Hey, Rong. Hi, Robert. We're going to kick off the new year by talking about C++ and Visual Studio Code. Yeah. This is a combination that many people might not think about. We know about Visual Studio Code, but it seems a lot of people are using it for web stuff, for C-Sharp. People know a lot about C++. We've talked about that with Visual Studio. You've been on the show talking about Visual Studio. It's been a very long time. Welcome back. Yeah, thank you. Today, we're going to talk about C++ and Visual Studio Code. Yeah, absolutely. Visual Studio Code, as many of you already know, had built-in support for many web languages, JavaScript type script, and other languages are also supported via extensions, actually, and C++ is one of those languages. So we on the Visual C++ team, we actually ship an extension for C++ for Visual Studio Code providing another alternative tool for developing C++ code both on windows and non-windows platform. So it makes sense that in Visual Studio Code, you don't want a full-featured IDE, you just want something that's just you and your code, writing various languages, C++ is a language. So it kind of makes sense. Yeah. Visual Studio Code is meant to be a lightweight editor as opposed to a fully-featured IDE. There are cases where you just want to write some quick code or just read code, or your program is so simple that you don't really need the complexity of an IDE, but just want to simply compile your code and do some simple debugging. That's if that's all you want, and you're looking for lightweight editor, Visual Studio Code is definitely a good option. That could be faster than installing the C++ workload into Visual Studio. Just go to the AS code. Yeah, a much lightweight experience, say overall. However, even though Visual Studio Code is meant to be a code editor to start with, and a lot of experience is focusing on editing experience, it does provide built-in support for integrating external tasks. So you can not only do building, but also integrate your external tasks to do testing, packaging, and some of those things. If you want to bring the whole workflow into Visual Studio Code, it's all possible, but you don't have to. So that's the beauty of it. It comes with a very lightweight tool, but it is fully extensible and has the flexibility for bringing more functionalities if you want. Awesome. So as I mentioned, it runs on Windows, Mac, and Linux everywhere where Visual Studio Code runs or extension runs. So it's definitely an option there out there, whether you're building on Windows, or Mac, or Linux platform. So yeah, today I'm going to show a demo on a MacBook. All right. So here I am in Visual Studio Code. So to do C++ development, it is highly recommended that you go get the C++ extension that we ship. So if you go to the extension tab and search for C++ and find one that's shipped by Microsoft, and just install it right from here. It takes a few seconds to get everything installed, and I already have this one installed on my machine, and that's all I need for this demo today. So now let's switch back to my folders view. What I'm going to do is to open the folder that contains some C++ code. So this is a project I downloaded from GitHub. This is called Box2D, which is a physics engine for building games, and I'm just going to open that folder. So first thing you'll notice, on the box, you start getting some basic intelligence. Even without any configuration on your part. So right here, I have one of the C++ file open, which is this one. Oh, by the way, this is a pretty sizable project. So you'll actually see a bunch of CPP and header files in here, lots of folder structures in here. I'm just opening one of them. But if you started looking at the code, if you just want to read the code, you start getting quick info tool tip, you hover over and some basic auto-complete feature as well. So if I want to hover over some of these things here, you get a list of possible matches. So that is the basic intelligence experience on the box. But if you also notice something here on the screen, which says there's an info bar that says, config include path for better intelligence results, which means if you're willing to configure include path and to help our intelligence engine to have more knowledge of your include files, we will be able to give you a much better experience. Is there a reason you wouldn't want to do that? It's work. Yeah. So if it's something you have to go and spend some time, figure out and yeah, if there have been cases where you just don't have the time to do that, you don't have the information. But on the box, you get the basic things. But if you want to try out better intelligence, we definitely recommend you to go and config include path. So let me show you real quick. So this is my browser here. So on our GitHub repo for the CPP tools, there's a documentation there that tells you exactly what you need to do to config include path, which is going to be some of the stuff I'm going to demo here. Okay. So first of all, the intelligence include path are defined in this file. This is called ccpp.proptist.json file. So I already have the here. Let me just get this one. Move this away. Let me show you real quick. So it's going to be command p and look for ccpp edit configurations, which takes you to that file. Now in this file, there are multiple configurations. By default, there's one configuration for each OS. So look for the section where your host OS is on. So for example, here I'm looking at Mac configuration. This is where the include path is defined. So by default, we generate system defaults based on your host OS. So for example, on Mac, because I also have Xcode installed, then I get the default ones, which should already include a lot of the system headers. So we don't have to worry about those. But we would need to know if there are other libraries you're using, we wouldn't know where your header files are. We wouldn't know. So it'll be helpful if you can tell us, tell the IntelliSense engine where to find those files. So of course you can go through the manual steps and figure out everything and put everything here by yourself. But we figured users might want some help in figuring out where those path could be because it's a tedious process to figure out every single folder. So one thing we do here is we have libob suggestions. So if you look at all these green squiggles and click on one of those, and we show a libob, click on a libob, libob actually provides suggestions for where this header could be located. Basically, there's a background process that searches recursively in the root folder. It looks for every possibility in the folder and provides suggestions if it is able to find one. So for example, in here, I can just simply click on the menu and all the squiggles just went away. Because what happened was we automatically added this path to include path. So if you go through the libob and suggestions, you actually never have to hopefully, never have to manually edit this file. But this is the file if you need to do it manually. So now I have that path added and IntelliSense Engine is actually will be able to give us better results. No, just for comparison. Remember I was looking at this member here. Now we have the exact match because we know where to find it now. Of course, there's more IntelliSense features that we provide. So that was the quicking for, of course, we do member list as well. So now you get the full member list. If you do have errors in your code, we will now start to resquiggle your code. This shows real issues in the code. So that's really helpful. Some of the IntelliSense engines to help you write your code more efficiently. The other scenario we target is reading code. So a lot of times you open a large C++ project and you want to understand how the code works. And we do provide code browsing features. For example, if I wanted to see the definition of this member function, I can right click on it and we do provide go to definition and declaration. And one thing I like to use is to pick definition instead of go to. So we can bring in the definition into the context of the current file. Can you edit it from in there? Oh yeah, that's like a full window. Cool. So in addition to the go to functions, we also provide code formatting. If you select code and you can say format document and format selection, we actually use clan format tool to help you format your code. Okay, so that's a little bit of IntelliSense and code browsing how to help you to read code. So beyond that, you might ask, what if I want to compile my code or build my project? As like I mentioned earlier, Visual Studio Code made it possible to build your code from within the window without having to leave the whole window. So this particular project is, let me open this file first. So to do a building in Visual Studio Code, the one file you need to config is tasks.json file. And essentially, it's pretty straightforward. You just want to pass your command. If you're dealing with one single file, you can even passing the compiler name here and the file you want to compile. And that's all you need to config. And for a bit more complex projects, you might want to use something like another build system or something like that. For example, in this particular case, this is config to use Xcode build to build. So what I did here. So you're using MS build, using Xcode build. No, no, this is completely on Mac. I'm using Xcode and I'm calling the Xcode build command from within Visual Studio Code. So what I have here is a script file. So I basically go to this directory and call this command. That's what I have in the script file. And then I just have to pass in this script to the command. And that's all I can fit. And then I can start building my whole project from here. I can say run build task. And Visual Studio Code is going to call this command and you'll see here, it actually successfully built my project in here. So that's building. Now beyond building. So at this point, I already have the output of my project and I'm ready to run this project. And you can of course just simply run, launch this XE from within here. And that's the file you want to edit, which is launched or JSON. And the only thing, well two things I guess, primarily you want to edit here is tell VS Code what the program is that you want to launch and what is your current working directory? That's the two things I can fit. And launching is just one scenario, simply run program. What if you want to debug the program? If there's a bug, you want to quickly debug through the code and the C++ extension provides support for LDB and GDB debugger on a Windows platform and on Windows you can still use GDB and the Visual Studio debugger as well. So in here, in this case, because I'm on Mac, I'm gonna config my configuration to use the LLDB debugger. And here's the type of the debugger. That's all the configuration I need to do. Now I can go back to my code and I can set a breakpoint here. And this method is called B2 Clyde Circles. Essentially, when two circles collide with each other, this function is gonna be called. Now we are all ready to switch over to this debug menu. I already have this selected, since I only have one configuration, say, I want to launch this program and debug with the LDB debugger. And all I need to do at this point is just to hit F5 or this arrow here. And this is gonna launch our program, which is showing us some physics in here. And what I'm gonna do is to get another circle in here and let them collide. Now we're hitting the breakpoint in Visual Studio Code and here are some things you can look at as you would expect local variables. We can even do expression evaluation and you've got full cost stack here. It even support multi-threading as well. Now you can step over your code and look at all these variable changes here. Or you can do a run to cursor if you wanna run to another line and everything just works like you would expect it and with very simple configuration here. Yeah, so basically I run through all the way from how you open the folder, get IntelliSense, browsing features and building and debugging the full cycle. So we do support all those things in Visual Studio Code with our extension. Very nice. So this one more scenario that I can demo here regarding IntelliSense. That's what a lot of people ask because setting up in-crew path is hard. Like I mentioned, a lot of manual steps. Even though we do provide light bulb suggestions, it may not help you to get 100%. So the question is, hey, since I'm able to, sometimes the project's already conflict to build with another build system like CMake. The build system already has the knowledge of which headers you wanna include. Otherwise it wouldn't build. Can we leverage that information and pass that back to IntelliSense and let them figure out the right in-crew path to include? So that is the scenario we're talking here. Let me do a real quick demo here. So first of all, I'm gonna open another project by adding another folder to the workspace. So that's something brand new supported in Visual Studio Code in last month release, which is now you can open many different folders in the same instance of Visual Studio Code. You don't have to switch between windows anymore. And we do support multiple folders in the C++ extension as well. So now you can have multiple folders open. And our IntelliSense engine operates independently for different folders. So it wouldn't confuse IntelliSense from one to the other. So it knows what code you're looking at. So now in here, I have another project. That's just the build, that's just the debug. So they are all independent. So in here, in this project, let me close the other project. And here's again, the JSON file, if you wanna do the manual steps. But let me open a code. Code real quick. Again, we're seeing this message. And you get real quick goals. The individual file level, is there a way to do it at the folder level? You mean the entire project? It is per folder that you open. Per folder. So this file always lives in this dot VS code folder, which is per folder. Right, so is there a way, do you have to go into each individual CPP file and configure the include path for that file? Or can you configure the include path for the entire project? So this file in here, this is configuring for this whole folder, which is containing lots of CPP files. So to answer your question, this is not per file, this is per folder you open. Right, but if you go in, you're in this file, you get a light bulb saying that you can configure, right? If you go into another CPP file, would you also get a light bulb? You will. Might have different suggestions because these are different headers. So then is there a way, is there a need to, instead of having to go into every CPP, say I want the JSON file, I want this configured for everything in the entire project all at once. Yeah, that's a very good suggestion. But no, so right now the light bulb suggestions is per translation unit. Okay. If you can think of translation unit as almost equivalent to a file, but it would pooling files that it is dependent on, like this file is going to be compiled along with the header, for example. But yeah, but right now I'm going to show you a much quicker way to config in Tilesense once for all the files in this folder. As I mentioned, this project is already configed with CMake build system. So this is already set up to build with CMake, everything works. So now the question is, how do we bring that information back to in Tilesense? It is actually really straightforward. Go back here and copy this. This is a flag that CMake supports. Essentially this tells CMake when you build this project, also export a file that's named compile commands. Essentially it's an XML file that has an array of objects. Each object defines how each translation, how each source file, CBP file, can be compiled in the project, which means it's going to have information for which files to include. Okay. So now all I have to do at this point is to rerun this build process, and I am going to have the additional output, which is this compile commands.json file. This is probably going to take a few minutes, so I'm just going to show you one that's already generated earlier. This is the one. Essentially this is defining for each CPP file what compiler is being used and all these defines and passing into the compiler, and then it has dash i, specifies the includes path. So you don't have to read through this. Oh, I'm going to start debugging here. So close that. At this point, all I have to do is go back to the json file. Now instead of using the include path, I'm going to say I'm going to use a compile commands and I just need to tell the IntelliSense engine where to find that json file I just opened. Okay. So this is the path of my compile commands.json file. Mm-hmm. And save this file. Go back. Can you scroll? Those are gone. Just like that. Okay. Now IntelliSense works for this entire folder that you have here. And you never have to- Is that specific to CMake? Can you do that with Xcode build? This is specific to CMake. However, this could be generated by many other build systems like Ninja supports this too. So this is something, the specification is actually defined in the clan documentation. And CMake is definitely one of the popular build systems that people use today. So yeah, if your project is already conflict with one of those build systems, definitely take advantage of the knowledge there and get the json file generated and we will leverage that. Cool. Yeah, that's all the demos I want to show you today. Cool. So if you're on Windows, you've got complete full support for C++ and Visual Studio, full featured IDE, Visual Studio you know and love. If you just want to do, if you don't have the C++ workload installed and you just want to do some simple C++, this is a nice alternative. Yeah, absolutely. To installing the full C++ workload, which can be rather large and can take some time. Even in cases where you do have Visual Studio on your box and there are times, even for large projects. If you just want to read code, this is faster. If you want to do a real quick edit, yeah. I do that with C-sharp code. Definitely use that as a companion tool. I set all the CS files to open in code because it just loads faster. Yeah. I just want to look at the code in there. Yeah, something quick. Visual Studio code is great for that. Yeah. And then if you're on Mac, this is a great way to go. Visual Studio for Mac doesn't currently support C++. Right. And then if you're on Linux, then of course you'd be using this. Yeah. Great. Yes. Sounds great. Thanks so much for coming on. Yeah. Thank you for having me here. Yeah. I hope you enjoy the demos. Yeah. Great way to kick off the new year of Visual Studio Toolbox. And we will see you next time.