温馨提示×

ubuntu如何编译cpp

小新
607
2021-02-19 14:58:55
栏目: 智能运维

ubuntu如何编译cpp

ubuntu编译cpp的示例:

1.编写C++文件,保存为“test1.cpp”。

//-------------ubuntu上.cpp文件编译与运行

#include

int main()

{

using namespace std;

const int ArSize = 20;

char name[ArSize];

char dessert[ArSize];

cout << "Enter your name:\n";

cin.getline(name, ArSize);

cout << "Enter your favorite dessert:\n";

cin.getline(dessert, ArSize);

cout << "I have some delicious " << dessert;

cout << " for you, " << name << ".\n";

return 0;

}

2.在终端输入以下命令进行编译。

xxxx@xxxx-xxxxxx-xxxxxxx-xxxxx:~/文档/cppfile$ g++ test1.cpp

会在同目录下生成一个a.out文件。

3.再输入以下命令运行可执行文件。

xxxx@xxxx-xxxxxx-xxxxxxx-xxxxx:~/文档/cppfile$ ./a.out

Enter your name:

Rawa

Enter your favorite dessert:

Cake

I have some delicious Cake for you, Rawa. //编译运行成功

0