How to compile and run C and C++ programs on Linux

不言
Release: 2019-03-07 11:01:59
Original
11584 people have browsed it

C is a powerful programming language used for developing system software. This article will introduce you to running C and C programs in a Linux system through the command line. In this article, we use the 'gcc' and 'g' commands of GCC (GNU Compiler Collection) to compile C/C programs.

How to compile and run C and C++ programs on Linux

gcc is the GNU C compiler in GCC.

g is the GNU C compiler in GCC.

Installing development tools

To run C programs, the development tool package must be installed on the Linux system. Run one of the following commands to install the development kit according to your operating system.

$ sudo yum groupinstall "Development Tools"   ## 基于Redhat的系统 
$ sudo apt-get install build-essential        ## 基于Debian的系统
Copy after login

C language example

For example, write the code for Hello World.

#include <stdio.h>
int main()
{
   printf("Hello World!");
   return 0;
}
Copy after login

Create a new file on your system as follows:

$ vim helloworld.c
Copy after login

Compile and run C/C programs in Linux

Using GNU The C compiler compiles the above hello world program as follows:

$ gcc helloworld.c -o hello
Copy after login

Use C compiler to write the following commands.

$ g++ helloworld.c -o hello
Copy after login

The above command will create an executable file named hello in the current directory. You can directly run the same operation as other commands

$ ./hello
Copy after login

You can also copy the files under the bin directory (/usr/local/bin) to make them accessible system-wide.

This article has ended here. For more exciting content, you can pay attention to other related column tutorials on the PHP Chinese website! ! !

The above is the detailed content of How to compile and run C and C++ programs on Linux. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template