Visual Studio Code (VSCode) is a software development tool that
is used to program in multiple programming languages. It is also a cross-platform
integrated development environment tool (IDE) which runs on Linux, Windows and MacOS.
To use VSCode for a particular
programming language, we need to install the corresponding extensions, which enables
VSCode to load all the tools to support the selected language. When programming
in C++ , we need to install the VSCode extension as well as a compiler that can
enable to compile the source code into machine code.
Install the Extension
VSCode works with extensions, which are libraries to support
languages and features. To be able to code in C++, we need to install the C++
extension. This can be done by searching for C++ from the Extensions view. From
the search result, select the C/C++ extension with intellisense, debugging and
code browsing. Click on the install button.
When reading the details of this extension, we learn that it
is a cross-platform extension. This means that it can run on multiple operating
systems (OS). It uses the MSVC and GCC compilers on Windows. The GCC compiler
on Linux, and Clang on macOS. C++ is a compiled language, which means that the
source code must be compiled into machine code to run on our machines.
Verify the Compiler
The extension does not install the compiler, so we need to
make sure that a compiler is installed. To verify this, we can open a terminal from
VSCode and type the command to check the compiler version.
// for Linux
and Windows g++ --version // macOS clang –version |
The output of that command should show the compiler version. If instead, the
message is command not found, this means that there is no compiler install, and you can move forward with installing the correct one for your target OS. Use GCC
for Linux and Windows (or MinGW-x64), and clang for macOS.
Write a Hello World Sample
Once the compiler is ready on your workstation, we can move
forward and start writing some code. Let’s start by creating a simple Hello World
app using C++. To do that, follow these
steps:
- Create a new folder. This is the project folder.
- Open the folder with VSCode
- Add a new file, name it helloworld.cpp
We should notice the CPP file extension. This is the
extension use for C++ files. The moment we create the file, the extension that
we previously installed should identify it and provide the programming
language support.
Now, we can add the following code to the file. This code shows some basics of a C++ application.
- Use include to import library support to the app.
- Use using to bring of the operations into the global scope.
- Declare the main() application entry point
- Use the standard console output to display our message
- Exit and stop the code execution
We should now have our simple Hello World app code written.
The next step is to compile and run the application. We can do that by following these steps from
the terminal window:
Note: Run these commands from the folder location
// compiles the code and creates the output file which is a standalone executable
// runs the application ./appHelloWorld |
The first command compiles the source code into machine code. It links the libraries, include declarations, to the output file or assembly. By looking at the project folder, we should see that a new file was created.
After the code is compiled, we can run the application from the terminal. The app should run successful and display the hello message. We should notice that this is a standalone application. It does not require any runtime environment like JavaScript, Python and other programming languages require.
Conclusion
VSCode is an integrated development environment tool that can enable us to work with different programming languages. It is also a cross-platform IDE, which enables programmers with different operating systems to use this technology. To work with a specific programming language, we need to install the corresponding extension. In the case of C++, we also need to install the compiler for the specific operating system. Let me know if you are using C++ with VSCode already and if you like or dislike the experience.