Linux环境下用vim编写编译运行C/C++程序

368 阅读1分钟

在Linux环境下编写编译运行C程序

首先在终端下输入命令进入编写

vim hello.c

#include <stdio.h>
int main()
{
        printf("hello C\n");
        return 0;
}

输入命令进行编译,该编译方式会默认生成一个a.out文件

gcc hello.c

输入命令进行运行

./a.out

在这里需要注意,编译的时候默认是生成a.out文件,这里也可以自定义编译的文件名,这里以编译出hello为例,命令如下

gcc hello.c -o hello

 运行该文件时命令如下

./hello

 

在Linux环境下编写编译运行C++程序 

首先编写一个C++源程序,命令如下

vim hello.cpp

#include <iostream>
using namespace std;
int main()
{
        cout<<"hello C++"<<endl;
        return 0;
}

 

输入命令进行编译,该编译方式会默认生成一个a.out文件

g++ hello.cpp

 输入命令进行运行

./a.out

 

同样的,如果想自定义编译出来的文件名,这里以编译出hello为例,需要如下命令

g++ hello.cpp -o hello

 运行该文件时命令如下

./hello