Compiling your First Program – First C++ Program

Compiling your First ProgramCompiling your First Program

We still have a few things to learn before we write our first C++ program.

Theory

Despite the fact that you are using .cpp files for your programs, these files are added to the project. Projects contain the files you need for your program and also store the settings you set up for your IDE. You start a project from the point you left off last time each time you open it. When a program is compiled, a project instructs the compiler which files to compile and which to link. Note that project files from one IDE cannot be imported into another IDE. In order to proceed, create a new project (with an alternative IDE).

A second aspect to consider is the types of projects. Choosing the project’s type will be part of the creation process. This lesson will be devoted to creating console-based projects. In other words, they are launched within the console (like with a command line).

GUI (. Abbreviation for Graphical User Interface) means a user interface designed for the graphical user. Console programs are exported as standalone executables by default. C++ beginners will find it ideal because of its simplicity.

A third benefit is that most IDEs create new projects automatically when you do. The workspace is a type of container which can hold multiple projects that are related. The recommendation is still to have separate workspaces for each program, even though multiple projects can be added to one workspace. Beginners can do the job much more easily with this method.

New programming languages usually start with the well-known program “Hello, world!”. Tradition will not be broken.

Visual Studio users

The first step in creating a new project in Visual Studio 2019 is to launch this IDE, then choose

“File> New> Project”:

You will then see a dialog box where you have to select from the tab

“Windows console application” then Click

“Visual C++” and click “ОК”:

In the corresponding fields, you can also enter the name of the project (any) and the location (I recommend not changing anything).

Text editors show you code and other text when they open. Remove this, then type the code below:

The following items are highly recommended:

The program will run in Visual Studio when you press Ctrl+F5. The following is what you should see if everything is going well:

As a result, the compilation was completed successfully and you will see the following output:

Hello, world!

The line should be removed “… exits with code 0 …”, you need to go to

Debug> Options”:

Then “Debug> General” check the box next to “Automatically close console when debugging stops” and press “ОК”:

That’s it! The Visual Studio program we have learned to compile has been implemented.

For Code: Blocks users

During the installation of Code :: Blocks, click “File” > “New” > “Project”:

You will then see a dialog box where you’ll have to select “Console application” and click “Go”:

When you press “Next,” you will be prompted to select C++ as your language:

In the next screen, you will need to enter the project name and its location (you can create a separate Projects folder), and click “Next”:

To close the dialog box, you will have to click “Finish”.

You will be left with an empty workspace once all the manipulations are done. Rather than opening the Sources folder on the left, you will need to double-click on main.cpp:

The first line of “Hello, world!” has already been written!

Compiled projects in Code: Blocks can be compiled with Ctrl+F9 or by selecting Build from the “Build” menu. The tab “Build log” should show you the following information if everything goes well:

The compilation has been successful!

You can run the compiled program by pressing Ctrl+F10 or going to the menu “Build” and select “Run”. There is a window that looks like this:

Your program has produced the following result.

Command-line users

Create a text file called HelloWorld.cpp and paste the following code into it:

Write the following command on the command line:

g++ -o HelloWorld HelloWorld.cpp

The HelloWorld .cpp file will be compiled and linked with this command.

For the program to run, enter the following:

HelloWorld or ./HelloWorld

Your program will be executed and your results will be displayed.

For web compiler users

Copy and paste the following code into your workspace:

Next, click the “Run” button. During the progress window, you should see the result.

For users of other IDEs

You need:

Step # 1: Make a console application.

Step # 2: Include the .cpp file in the project (if needed).

Step # 3: Select the code that follows and paste it into the .cpp file:

Step 4: Prepare the project for compiling.

Step # 5:  Get the project going.

If the compilation fails (aka “Oh my God, something went wrong!”)

You don’t need to panic, everything is fine. It is likely that this is merely a trifle.

  • Please ensure that the code is written correctly: no typos or errors. Often, compiler error messages will provide information on where and what caused the error.
  • Additionally, you can find solutions to most problems in Solutions to the Most Common C++ Programming Problems
  • You may also try an internet search on Google if all of these suggestions are not successful. In 90% of cases, someone has already encountered and solved this issue.

Conclusion

Congratulations on writing, compiling, and running your first C ++ program! Feel free to ignore any code you don’t understand. The next lesson will go into greater detail on all of this.

LEAVE A REPLY

Please enter your comment!
Please enter your name here