您的位置:首页 > 运维架构 > Linux

如何在linux终端下编写C++应用程序

2017-09-17 11:47 537 查看
我们知道在windows下编译并执行c++程序,只需要一个合适的集编辑,编译,连接执行为一体的多功能软件就可以完成整个过程。国内用的比较多的软件有visual studio(版本现已更新到2017)初学者也可以考虑visual c++6.0;本章我们主要讨论在如何linux系列操作系统中如何使用shell进行c++程序的编写以及执行。

类比于写一个运行在windows平台的c++应用程序,在linux终端下首先也要创建一个后缀名为.cpp的文件用来存放你的主要代码。

使用touch命令touch test.cpp 再ls一下会看到你的文件夹里多了一个名为test.cpp的文件。

vim test.cpp

进入文件内编写你的代码程序

#include<iostream>

  2 using namespace std;

  3 class Time{

  4 private:

  5     int hour;

  6     int minute;

  7     int second;

  8 public:

  9     void set(int h,int m,int s);

 10     void print();

 11 };

 12 void Time::set(int h,int m,int s)

 13     { hour=h;minute=m;second=s;}

 14 void Time::print()

 15     {cout<<hour<<"/"<<minute<<"/"<<second<<endl;}

 16 int main()

 17 {   Time a,b;

 18     a.set(10,12,50);

 19     a.print();

 20     b.set(12,10,40);

 21     b.print();

 22     return 0;

 23 }

保存退出

使用gcc编译你所写的程序gcc test.cpp 这个时候系统会默认生成一个名为a.out的文件在你的当前目录下。若想指定一个你所喜欢的文件名可以使用-o

gcc test.cpp -o 后面加你想生成的文件名。

如果编译通过的话证明你所写的程序是没有bug的

运行一下 ./a.out

ok.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  vim linux shell c++