Jump to content

How to compile a C++ program on Linux


aum

Recommended Posts

In this TechRepublic How to Make Tech Work video, Jack Wallen shows you the step-by-step process of compiling a C++ program on Linux, using Ubuntu Desktop 23.04 for his demo.

 

 

I am going to show you how to compile a C++ program on Linux. I’ll demonstrate it on Ubuntu Desktop 23.04, I’ll use the tried and true Hello, World! app as an example.

 

The first thing you must do is install the necessary software to allow you to build C++ programs. To do that, open a terminal window and issue the command sudo apt-get install build-essential -y. If you’re on a Fedora-based distribution, that command would be sudo dnf install gcc-c++ -y.

 

Now that you have the necessary tools installed, let’s create the Hello, World! file with the command nano hello.cpp. In that file, paste the content shown in the video. Once you’ve pasted the content into the file, save it with the Ctrl-X shortcut.

 

Next, we have to compile the program with the command g++ hello.cpp -o hello. Because this is such a small program, it should compile almost immediately. One of the nice things about this process is that, upon compilation, your program will automatically have executable permissions, so there’s nothing to change in this regard.

 

Then, you can run the program with the command ./hello, which will print out Hello World! in your terminal. And that’s all there is to compiling C++ programs in Linux.

 

Here is the code for Hello World:

 

#include <iostream>

 

int main() {

    std::cout << "Hello World!";

    return 0;

}

 

Source

Link to comment
Share on other sites


  • Views 831
  • Created
  • Last Reply

Top Posters In This Topic

  • aum

    1

Popular Days

Top Posters In This Topic

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...