21xrx.com
2024-11-22 04:07:31 Friday
登录
文章检索 我的文章 写文章
How to Run C++ Programs on Linux?
2023-07-10 04:59:39 深夜i     --     --
C++ Linux Programs Run How-to

As an aspiring programmer, if you're using Linux for the first time, you may wonder how to run C++ programs on Linux. This opens the door to a whole new world of endless possibilities in programming, and with the right knowledge, it becomes an effortless task. Here's a step-by-step guide on how to run C++ programs on Linux.

1. Install a C++ Compiler

Linux distributions don't usually come with a C++ compiler, so you need to install one. GCC(GNU Compiler Collection) is a popular choice, and it can be installed using a package manager like apt or yum.

For Debian-based distributions like Ubuntu, you can run the following command to install GCC:

sudo apt-get install g++

For Fedora, CentOS, and Red Hat, run:

sudo yum install gcc-c++

2. Writing the C++ Program

Open a text editor and create a new file for your C++ program and save it with the ".cpp" extension. The filename doesn't matter; instead, make sure the ".cpp" extension is there, indicating a C++ source file.

Here's an example program:

#include

using namespace std;

int main()

cout << "Hello

Save the file as "hello.cpp."

3. Compiling the C++ Program

Once you've written and saved the program, compile it by running the following command on the terminal:

g++ hello.cpp -o hello

The "g++" is the command to compile the file, and the "-o" flag is used to specify the output file name. The "hello" argument following "-o" specifies the program's name that will be generated after the completion of the compilation process.

4. Running the Program

After the C++ program is compiled successfully, run the following command in the terminal to execute it.

./hello

This command will run the program and output the text "Hello, World!" to the console. Congratulations, you've successfully run a C++ program in Linux!

Conclusion

Running C++ programs in Linux is an essential skill every programmer needs to master. The process is relatively easy and straightforward, and with the help of a C++ compiler like GCC and following the above steps, you can get started in no time. Best of luck!

  
  

评论区

{{item['qq_nickname']}}
()
回复
回复