1. 程式人生 > >Centos編譯執行一個簡單的C++程式

Centos編譯執行一個簡單的C++程式

I、準備工作:

1) 一個類UNIX系統,這裡選擇CentOS 7

2) 安裝gcc編譯器,方法很多,一種簡單的安裝方式:yum install gcc-c++   注:yum install g++ 會失敗,因為沒有g++這個包

安裝好後顯示如下:

3)建立測試目錄

[[email protected] /]# mkdir desk
[[email protected] /]# ls
desk  opt  公共  模板  視訊  圖片  文件  下載  音樂  桌面
[[email protected] /]# cd desk/

4)建立cpp檔案

[[email protected]

desk]# touch test.cpp
[[email protected] 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)編譯執行檔案 (注:書寫格式)
[[email protected]ocalhost desk]# g++ -o test.cpp   
g++: 致命錯誤:沒有輸入檔案
編譯中斷。

[[email protected] desk]# g++ test.cpp -o test
[[email protected] desk]# ./test
3