Last Updated:

How to write in C++ in VisualStudio

You can use a text editor and compiler separately, compile and run the program manually in a terminal or console, but it is much more convenient to use the IDE or different development environments. They usually include a built-in text editor that is linked to the compiler, allowing you to compile and run the program with a single click. Also, the user is provided with a huge number of other additional features.

In our example, as a development environment (for programming under Windows), we will use the full-featured and free VisualStudio 2019 Community environment, which we found at the link https://www.visualstudio.com/vs/.

After downloading and running VisualStudio, you need to note the item "Developing desktop applications with C++" in it:

Now select all the necessary items and run the installation by clicking OK. When the installation is complete, we will create the first project. Open VisualStudio. On the first screen, select the EmptyProject type for C++:

create new project in visual studio

On the second screen, in the box for the project name. Let's call it HelloApp. You can also specify its location. After that, click Create.

configure new project

If you already have a project open in VisualStudio, you can create a new project for C through the File ->New ->Project menu... (Project) and then repeat the same steps.

VS then creates an empty project. Now let's add a text file to it, in which we will type the code. To do this, in the SolutionExplorer window, right-click on the SourceFiles node and select From the context menu: Add ->NewItem...:

add new item in visual studio project

Now a window will open to add a new item:

add new item in visual studio project 2

Now we need to select the C++ File(.cpp) option, and at the bottom of the window let's add a name for the Hello.c file. Usually, C source files have the extension .c. It means that this file contains the source code in the C language, and it will be processed by the appropriate compiler.

At this point, we will change the project options. To do this, go to the Project ->Properties menu item

properties menu item

First, in the project properties, let's set what it will be a console application: Linker->System and then set the SubSystem field to Console(/SUBSYSTEM:CONSOLE) by selecting the necessary item in the list:

subsystem-console

After setting this value, click on the "Apply" button to use the new configuration settings.

Even in the project properties window on the left side, let's go to the C/C++ section and then to the Advanced item:

configuration settings

On the right side of the window, for the CompileAs field, specify the value compileas C Code (/TC). Thus, we say that the source code by default is compiled as C code, and not C ++. When the installation of this option is complete, click "Apply".

After adding the file, the project will have the following structure:

search solution - app

Let's look at this structure. The SolutionExplorer window contains the solution. In our example, it's called HelloApp. A solution can contain multiple projects. We have one project with the same name HelloApp. The project contains a number of nodes:

  • ExternalDependencies: Displays files that are used in source code files but are not specific to the project
  • HeaderFiles: Used to store header files with the .h extension
  • ResourceFiles: Used to store resource files, such as images
  • SourceFiles: Stores source code files

At this point, let's define the simplest code in the Hello.c file that will be responsible for displaying the line to the console:

#include<stdio.h> // connect the header file stdio.h
int main(void) // define the function main
{ // the beginning of the printf function
(Hello world! \n»); Output the string to the return0 console
. exit the } // end function

Let's run the program. To do this, in VS, select the Debug ->StartWithoutDebugging menu item or press Ctrl+F5:

debugging visual studio project

As a result, VS will pass the source code to the compiler, which will compile the exe executable file from the code, and it will then be launched for execution. And we will see on the running console our message:

 

Further in the Debug folder in the project, you will see a compiled exe file that can be run independently of VS:

 

In our example, the HelloApp.exe file represents the compiled executable. In addition to this file, two auxiliary files are automatically generated in the same folder:

  • HelloApp.ilk: "incrementallinker" file that is used by the linker to speed up the layout
  • HelloApp.pdb: A file that contains debug information