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.
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的系统
C language example
For example, write the code for Hello World.
#include <stdio.h> int main() { printf("Hello World!"); return 0; }
Create a new file on your system as follows:
$ vim helloworld.c
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
Use C compiler to write the following commands.
$ g++ helloworld.c -o hello
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
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!