Centos编译运行一个简单的C++程序

I、准备工作:

1) 一个类UNIX系统,这里选择CentOS 7

2) 安装gcc编译器,方法很多,一种简单的安装方式:yum install gcc-c++   注:yum install g++ 会失败,因为没有g++这个包

安装好后显示如下:

3)创建测试目录

[root@localhost /]# mkdir desk
[root@localhost /]# ls
desk  opt  公共  模板  视频  图片  文档  下载  音乐  桌面
[root@localhost /]# cd desk/

4)创建cpp文件

[root@localhost desk]# touch test.cpp
[root@localhost desk]# vim test.cpp

#include <iostream>
using namespace std;
int add(int num1, int num2);
int main()
{
    int x, y, z;
    x = 1;
    y = 2;
    z = add(x,y);
    cout << z << endl;
}

int add(int num1, int num2){
    return num1+num2;
}

5)编译运行文件 (注:书写格式)
[root@localhost desk]# g++ -o test.cpp   
g++: 致命错误:没有输入文件
编译中断。

[root@localhost desk]# g++ test.cpp -o test
[root@localhost desk]# ./test
3

猜你喜欢

转载自blog.csdn.net/qq_34625397/article/details/81234983