ubuntu如何编写C++程序

一、检查是否安装有g++ gcc 编译器

终端输入

g++ -v

如果提示没有安装,会有如下提示

Command ‘g++’ not found, but can be installed with:

sudo apt install g++

可以按照提示安装,输入

sudo apt install g++

g++安装结束后,gcc会自动安装上,输入g++ -v 和gcc -v检查,查看版本号。

二、重新安装vim

如果你的vim出现了上下左右箭头变成ABCD的清况,则需要重新安装vim, 具体方法参考这篇博客:https://mingshiqiang.blog.csdn.net/article/details/84557981

三、编写代码

vim hello.cpp

输入如下代码:

#include <iostream>

using namespace std;

int main()
{
    
    
    cout<<"Hello world!"<<endl;
    return 0;
}

四、编译代码:

gcc hello.cpp -lstdc++ -o hello

g++ hello.cpp -o hello

五、运行

编译之后会产生二进制文件hello, 输入以下命令运行:

扫描二维码关注公众号,回复: 14222711 查看本文章
./hello

注意:这里是输入"./hello"而不是hello

六、make编译

除了使用第四步的编译方法,还可以使用make命令进行编译,例如

make hello

此时会自动找到hello.cpp进行编译,如果没有安装make会有如下提示
Command ‘make’ not found, but can be installed with:

sudo apt install make # version 4.2.1-1.2, or
sudo apt install make-guile # version 4.2.1-1.2
按照提示安装make即可

sudo apt install make

再运行make hello即可产生二进制文件hello
注意:如果已经产生了hello, 需要先删掉,rm hello

猜你喜欢

转载自blog.csdn.net/yao_hou/article/details/123557810
今日推荐