Getting started with C++ in VS Code

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
we're doing c plus uh pointer to a pointer to a pointer i did c plus plus in college um that was the first programming language i learned and i haven't done it since then but i understand people are still doing c plus and so for that uh i'd like to welcome julia reed julia welcome to the vs code live stream thanks burke happy to be here so what is it that you do with c plus plus and microsoft good question so i'm on the visual c plus team at microsoft which covers a whole bunch of c plus plus developer tools not just vs code we also work on visual studio and the microsoft c plus compiler but i focus specifically on c plus plus development experiences in vs code and my team actually maintains three extensions just for c plus development in vs code we have the c plus extension which gives you intellisense and code navigation and debugging for c plus plus files we have the cmake tools extension which is for building and debugging cmake projects and the make file tools extension which is for building and debugging make file projects so i'm going to talk about all three of those today well that is exciting i'm going to be honest i only knew what one of those things was uh but we do have you we always get chat is always like hey can you all do more c plus and chad you may recognize julia if you watch our youtube shorts um you've made a couple of those for us tic toc so uh take it away julia cool all right let me share my screen there we go let me add it here okay and let's change there we go look at that man stream yard such a nice tool everybody use streamyard it's great we love it nice okay um can you see my terminal we can perfect um so yeah before actually diving into the first demo um one really important concept to understand about the c plus extension in vs code is that it doesn't come with the c plus plus compiler it doesn't install one for you it uses whichever c plus plus compiler you have installed on your machine to set up intellisense and debug configurations for your project um so the first step for c plus development in vs code is making sure that you have a c plus compiler already installed on your machine um so for the demos today i'll be using a macbook and so i'm on mac os but the experiences are the same across mac linux and windows and i'll be sure to call out any platform specific instructions um if we run into any so julia do both windows and mac come with the c plus plus compiler already installed uh mac does most likely because xcode comes with the clang compiler um and you can verify that just by running clang dash dash version um and if it doesn't come installed then you can use the xcode select command crash dodge install and then that'll install it for you so this is if you're on mac for windows you'll have to install well if you have visual studio installed then you'll have the microsoft c plus plus compiler if you don't have visual studio installed then you'll need to install it um and you can check out our docs um to do that so for windows we have a document for installing the microsoft c plus plus compiler and how to use that with vs code quick note that you are allowed to use the msbc compiler with vs code as long as you have a valid visual studio license either community pro or enterprise but if you want to install gcc on windows instead then we also have tutorials for that you can do gcc with wsl or uh with mingw uh through nsys2 and we also have tutorial for installing gcc on linux and so for those of us who don't know what like gcc is or mensis or whatever the these are all compilers i assume yeah yeah yeah so um the most popular compilers for c plus development i'd say are gcc if you're on linux clang if you're on mac and then the microsoft c plus plus compiler if you're on windows and then uh mingw is a way to use the gcc compiler and linux tools on windows but wsl is another way to do that and if you're using vs code i'd recommend just using the wsl extension it's a pretty seamless experience nice okay thank you no problem okay um so now that we've covered installing compilers um if you have a c plus plus compiler installed then you're all ready to stop you're all ready to start cfos development in vs code um i think the easiest way to open your project in vs code is just to navigate to the project directory in the terminal and then type code space period enter and this opens um your project in vs code so for this for the first scenario that i'm going to talk about today it's just a very basic run and debug a single c plus file and vs code so we have um one source file main.cpp and this is just a simple fibonacci program it prints out 15 terms of a fibonacci sequence um i don't have any like third-party libraries or anything um so all i need to get intellisense and to run and debug this file all i need is a c plus plus extension um oops which you can install in the extensions marketplace directly from vs code all right so julia real quick you mentioned that there's intellisense for c plus plus yeah but ethan is saying um he'd like to know why there's no robust code completion for c and c plus like theirs for java and other high level languages does that mean that there's not the same kind of intellisense i'm not sure what he's referring to there we have code completion with intellisense um which i can just give you a quick example like if i start typing n then we do have auto complete lists what ethan might be referring to is intellicode versus intellisense so intellicode is an ai based intellisense um and that's what we have in visual studio i believe and there are some languages in vs code that use intellicode instead of intellisense the c plus plus extension uses intellisense um and there are discussions around maybe switching to intellicode but for right now it's intellisense but we do have autocomplete okay ethan hopefully that answered your question all right thank you julia no problem um okay so here in my main.cpp file intellisense appears to be working right off the bat um we don't have any like red error squiggles under our include paths and we have autocomplete as i just showed you a quick info so if i hover over a variable i can see the type information or documentation signature help if i hover over a function name and the intellisense should work out of the box but if you need to customize your intelligence configuration maybe you're including third-party libraries in your project and you want to make sure the c plus extension can find those libraries um you can do that by clicking on this button in the bottom right of the status bar so if you hover over it it says c c plus plus configuration and since i'm on my macbook it's telling me that i'm using a mac intellisense configuration let's click on that and here we can edit our configuration either with a ui or by editing a json file directly um but let's let's use the ui okay let me zoom out a bit so we can see all these are the configurations different between mac and windows like i always thought that was strange that there's like it says the name of your computer in the status bar right so the reason that so the c plus extension will come up with a default intellisense configuration based on the information it detects about your system such as your operating system your architecture the compilers that are found on your machine um but a lot of c plus plus developers target multiple platforms or architectures in their development workflows so the point of having like different intellisense configurations for a project is so that you can create platform-specific intellisense configurations so okay or so that you can have one configuration that uses clang and another configuration that uses gcc if you want to test your code with both of those compilers so here this is just a default configuration it created for me edit called it mac but i could create a new one and call it linux or custom configuration or whatever um and then within that configuration you can choose which compiler you want to use so these are this is a list of all the compilers found on my machine um by the c plus plus exactly it looks like you've installed them all i've installed them all and then compiler args so when you are compiling your program from the command line if there are any uh special args that you're passing to the compiler you can define those args in this compiler args property and then when we query the compiler to set up intellisense for your project we'll query it with those args so if you are using any special compiler flags you'd uh define them here intellisense mode tells us which platform and architecture variant of the compiler to use so this is really helpful if you are setting up cross compilation intellisense configurations so let's say you are on your mac laptop but you're working on a project that is targeting linux arm with gcc as long as you have that gcc compiler installed on your system that's able to compile for that architecture then you can point your intellisense configuration to that compiler and then change your intellisense mode to linux gcc arm if you want to learn more about setting up cross compilation intellisense configurations we do have a tutorial um with the rest of our docs it's called intellisense for cross compiling this is a lot of configuration julia like do i have to do these things to get started with c plus plus and vs code or that's important you shouldn't have to um and like when i opened this file in vs code for the first time on mac like the default configuration that the cfos plus extension created for me works um but i like to show how the intelligence configurations work under the hood because as you progress in c plus plus development you're likely going to want to customize your intellisense configuration um i think like one interesting thing about c plus plus is that there are a lot of different tool chains out there that people can be using you know it's like a lot of people use clay a lot of people use gcc a lot of people use msvc there's not like one overruling compiler and so being able to customize those configurations is great so that if you and your teammate are using different tool chains you can have configurations for both those tool chains in your ccp properties json file which is what that ui i just showed it writes to this file um and that way you can just toggle between those intelligence because this low-level stuff is so interesting because javascript and python developers is like not just not something we have to think about right like javascript and it runs in all places we got a couple well we have actually a bunch of questions coming on in the chat here um i think this is so be i hope i said that right if i listen if i butcher your name i'm doing the best i can i'm sorry i do apologize genuinely uh can i use language servers like ccls in vs code so we have the when you when you're using the c plus plus extension you're using um the microsoft c plus plus language server if there are other c plus language servers that you want to use you can check out to see if there are other extensions that support those as long as those language servers are using the language server protocol um but for the c plus plus extension it's it's just our language server interesting and uh and emerge here that's an interest that sounds like a gaming tag maybe i'll steal that from my uh for my overwatch account so sometimes i hope there's an extension that can do magic with my cmake files and the uh properties.json which just synced the external dependencies i'm using again i have no idea what do you know what they're referring to here yes and it i'll i'll be showing that today okay all right extension is magical all right i'll get out of your way um okay so before diving into c make tools i just want to quickly show you how you would run and debug a single uh file project like if you're not using a build system so you would set a breakpoint and then just select run start debugging or you can hit f5 and then you're going to want to choose your environment and the compiler that you want to use to build your program so this again is a list of compilers found on my machine and i'll select clang and then let me zoom in a little bit okay so in the terminal i can see the build finished successfully and it launched the debugger which is what we're looking at right now um and within this debug ui you can look at your local variables you can change the values of your variables by doing control click set value you can add variables to watch step through the program and you can even look at the assembly language in disassembly view by opening up disassembly view during a debug session um and then if you continue running the program then your oops your output will show up in the debug console if you're on mac os so you might have noticed that a json file popped up right before the debugger launched and that's because when you hit f5 or select run start debugging then the vs code will create these two files tasks.json and launch.json in the dot vs code folder for your project and tasks.json stores your build tasks so it tells the c plus plus extension how to build your program so remember how i selected clang plus plus in that little drop down um so the build task that was created is using the clang plus plus compiler and then the args property tells us which arguments to pass to clang to compile your program such as the file name the output file um and you can define as many build tasks as you'd like for a project and then whichever one has this is default true property is the one that'll run if you just do command shift b so you can think of like if you're normally compiling from the terminal and you write out clang plus plus and then a bunch of args you can turn that command line into a build task and tasks.json so that you don't have to keep opening a terminal and typing out that command line every time you want to build your program you just run command shift b so now that this build task is created if i go to my source file and hit command shift b then it'll it'll run the build and you can see exactly what the command line looks like that it's constructing from that build task launch.json is the same idea except it's for your debug configuration so you have the name of your program if you need to pass any args to your program on the command line you can put those in this args array and you can also define uh args to pass to gdb um specifically or to the debugger so would it be would it correct summary be that the tasks is where you define how to build it and the launch is where you say okay run the build and then execute the the built program and attach the debugger yeah exactly so the task is where you how you build the launch configuration is how you debug and then you can define a pre-launch task which is the build task so that if you run this debug configuration it'll verify like oh i'm going to build and the build has to be successful and then i'll launch the debugger and we set all this up for people right like we don't make them do this themselves no no no so as you just saw like i didn't have these two files before um i just set a break point hit run and start debugging for the first time and then the c plus plus extension created this build task and this debug configuration for me and if it like hopefully it works right out of the box and you don't actually have to look at or touch task.json or launch.json but if you have you know arguments you need to pass to your program or there are other compiler flags that you want to specify you could do that in those json files so interesting question here from ryan what's up ryan is c plus plus with gui programming possible with vs code can you build yeah guys with c you can totally um i would say it gets more complicated if you are doing remote development you'll need to configure some additional settings in order to be able to see that gui pop-up um but it's totally possible i've i have done some demos where we build super tux which is a video game um using vs code you built it you did you say you made a video game well supertux is an open source video game um so i didn't write it but in some of our demos we on our my team when we're speaking at conferences and stuff that's a project that we commonly use as an example and that totally launches a gui it launches a video game so one more question here uh and i think this is ancient chat but uh pro doge um which interesting avatar it looks like you spent some time on that or you downloaded it off the internet i don't know what are the other but asking i couldn't get include bit standard c plus plus dot h header to work on my mac can you show how to use it that seems like a relatively specific question about a specific include it is i'll just say that if you are using a system header or a standard library or a header within your project directory you shouldn't have to configure any additional settings like those should just work if you are using a third-party library then in your intellisense configuration in the include path property you can put the path to that directory so for example like on this laptop i use vc package to install third-party libraries so i have a path to all the libraries i've installed with vc package in my include path so that if i were to include those files or those header files intellisense would be able to find them gotcha makes sense hopefully that answers your question uh i don't know that we can guarantee the working of every header file but if you think there's a bug definitely open an issue on the cpp tools um i want to quickly show before we run out of time i want to show the c make tools extension yeah go ahead okay cool so um most people with cp who are developing with c plus are probably using a build system and cmake is a really popular cross-platform build system you can install the cmic tools extension in the extension marketplace in vs code directly and this program i have here is also a fibonacci program but it's using some third-party libraries which i've installed using a vc package manifest file and it's using cmake to build my project um because i have this cmakelist.txt file in the root of my project directory the cmctools extension is activated which i can tell based on the buttons that it's providing in the status bar um you'll also notice that this file or this project is using cmake presets um for those of you who are using cmake you may be familiar with cmake presets it cmic preset.json is a file that kitware started supporting earlier this year and it enables you to share common configure build and test options with teammates because you can check it into source so any options that you normally pass to cmake on the command line when you're configuring building or testing your project you can just define those in this json file and then use this file to invoke those same cmake builds from the command line in a ci pipeline or in vs code or visual studio um anyway so you have your cmake presets file and the cma tools extension will detect that file and read from it and then surface those presets in the ui um you can do that you can choose presets by clicking these buttons in the status bar so this one is my configure preset and it's showing me mac os because i'm on on mac os i have a linux configure preset also defined but that's irrelevant so it's not giving me that option and then once you select your preset then you can configure your project okay and the configure succeeded and then we can select our build preset um so i'll do the verbose build and build target we'll do all and then hit this build button in the status bar and the build succeeded so now to debug our program let's set a breakpoint and select a launch target and this debug button and this launches the debugger so i'll just continue uh running the program and exit the debug session so if you're using cmake presets which is not a vs code specific thing it's supported by kitware itself and any other editor or ide that enables support for cmic presets then you can configure build and debug and test your program in vs code without configuring any additional like vs code specific settings um all just within a few minutes it's super easy and i mentioned that you can also test your program using c-test so in this project i do have some tests to find and i'm using g test and i have a test preset so i can select that test preset and then hit this button in the bottom right to run the tests and it tells me that they've succeeded and let me zoom out so you can really see all those uh buttons if you hate the status bar buttons if it's too crowded then you can disable the cmaktools extensions buttons from the status bar and instead use the cmake pane here on the left and you can just do the same things from here you control click and you can build debug um your targets um so that yeah that's the cmake tools extension and like a three minute uh demo um but if you want to learn more about c make tools you can check out our docs or if you want to learn more about cmake presets erica one of our co-workers gave a really great talk at accu 2021 called cross-platform pitfalls and how to avoid them and she talks about cmake presets and scenic tools so that's i've got the aka for cmec presets demo up perfect check that out and then make file tools is that the other one yes um so i know i'm kind of over time here but i'll just quickly show you without diving into all of the settings um but i have a make file project open in vs code and the project i have opened is vim which i'm sure many of you are familiar with um once again install make volatiles extension never heard of it and um here you can go once you have the makefile tools extension installed you'll see the makefile tools icon which is the c plus file with a little gear and on the left click on that and then here you can choose how to configure your project so i'm just using the default configuration but if you need to pass any special flags to make to configure your project you can define additional configurations in the extension settings and then let's configure the project so you can just type make file configure from the command palette and then let's select a build target so this is a list of targets found in your projects make file so make file tools extension we'll read from the make file and service all those targets in the ui let's select all and then hit this build button and it says target all built successfully and then the last thing is to select a launch target and then um when i selected that launch target the makefile tools extension actually created some like a launch configuration object in my settings.json so this is where if you need to pass any command line args to your program when running and debugging it you can specify those in binary args you can also define other debugger settings here like stop at entry to be true if you want the debugger to stop as soon as you hit the main method and then if we go back to our make file tools pane um we can let's run the program you can see then in the terminal so that worked um and then if you were to do wow that's the fastest exiting of them in history the land speed record and then yeah you can launch the debugger also using that debug button and in the icon all right interesting question here from uh sci-fi not vs it just says vs code c plus is not supposed for m1 max um i think there's some words missing from that comment but is it just doesn't work on m1 is there something to know intellisense or we do have some support for um m1 max i believe we have intellisense support but not um like default debug support but there is a way to configure debugging on m1 max though i believe it requires using an emulator um we having native debug support on m1 max is definitely on our radar and um something that we'll hopefully get to implementing soon in the meantime there is a way to configure your launch.json so that it works i will take some time to find that and then i can post that on twitter um or like on a blog post or something so that people are aware of that and well and luis here said i did compile c plus plus last night in my m1 mac with intellisense confirmed so maybe thank you this is working it's working um all right let's see here um just looking at the chat uh there's so many questions julia there's so much going on over here oh uh this was a interesting not really to see post plus but is this the first stream uh no this is like the 40 something extreme but we stream do and then i think this the same person asked if we have a stream schedule we do it's every thursday at what time is it 8 a.m pacific time are you on the west coast julia i am actually in new york so oh lunch time for me it's a very convenient time for you yeah i always think it's funny when our west coast people have to hop on at 8 a.m i know i know i actually had time to drink my coffee that's what you guys living on the west coast nobody feels sorry for you uh all right chat we're one minute over uh if you got any questions for julia we'll hang on for one more minute here uh oh here's i mean i guess we can take this question i don't have the this this person asked this is hamadreza how do i compile vs code from source on windows if you go to the vs code github repository they might have some instructions there okay that's that's all i have so yeah i think it's probably in the readme that's a great question um so yeah that's that's it lots more in the comments uh hey chad everybody say thank you to alessandro who's been answering your questions from the vs code channel there thank you alessandra so much for your help thank you julia for being with us at 11 a.m eastern time it is lunch time with that we will see you next week next week i believe is our release party for our last release producer alessandro am i correct on that can you drop that in the chat and let me know because if it is we have something special to show y'all not related to vs code like an actual physical thing we want to show you so we'd love for you to be here all right thanks everybody we will see you back here next week thursday for the vs go live stream bye julia thank you bye see ya
Info
Channel: Visual Studio Code
Views: 97,149
Rating: undefined out of 5
Keywords: vs code, vscode, visual studio code, cpp
Id: dSGW-DLMnUc
Channel Id: undefined
Length: 32min 3sec (1923 seconds)
Published: Thu Dec 02 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.