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. //編譯運行成功