CentOS 环境下C/C++程序的开发

来源:互联网 发布:知乎创始人周源 编辑:程序博客网 时间:2024/06/05 14:16
CentOS 环境下C++程序的开发(root权限下执行)
====================================================
第一步:检查是否安装gcc和g++
分别执行命令:
[root@localhost~]# gcc -v

出现类似以下的提示:

...

gcc version 4.47 20120313 (Red Hat 4.47-3) (GCC)
[root@localhost~]# g++ -v

出现类似以下的提示:

...

gcc version 4.47 20120313 (Red Hat 4.47-3) (GCC)
版本一致,表明安装正确。

如果没有安装的话:
执行命令:
[root@localhost~]yum install gcc-c++

第二步:编译执行程序。
写好一个 hello.c 程序
//hello.c
#include <stdio.h>

int main()
{
    printf("Hello C\n");
    return 0;
}
编译命令:
[root@localhost~]gcc -o hello hello.cpp
执行命令:
[root@localhost~]./hello


C++程序的编译过程
//hello.cpp
#include <iostream>
using namespace std;

int main()
{
    cout<<"Hello C++"<<endl;
    return 0;
}
编译命令:
[root@localhost~]g++ -o myhello hello.cpp
执行命令:
[root@localhost~]./myhello