正在加载...

编译

formatting 页在 box.cpp 中留下 left、right 与 total 上的整齐大括号以及 left=10、right=2、12。本页仍在 Quality and Wrap-up。车间文件仍是 box.cpp。新工作命名 compiling:把源文件变成可运行程序的构建步骤。保留相同名称与相同 +。运行 g++ -std=c++17 box.cpp -o box,再运行 ./box。屏幕仍显示 left=10,然后 right=2,然后 12。random 留给下一页。
这里的 compiling 表示读取源文件并写出可执行文件。保留 #include <iostream>。不要在这里发明 random。构建仍熟悉:box.cpp 到 box。

保留带整齐大括号的 left、right 与 total。保存后用 g++ -std=c++17 box.cpp -o box 构建,再运行 ./box。
三行:left=10,然后 right=2,然后 12。

若改了源文件却忘记重新编译,旧的 box 可能仍打印昨天的结果。若构建失败,先修好源文件。常见错误:把 ./box 当成编译命令、过早发明 random、省略 -o box、等待 cin、练习构建行时改数字。
本页停在 random 之前。

打开 box.cpp。保留 left、right 与 total。保存。用 g++ 构建。运行 ./box。

box.cpp compiling g++ build left right total

cpp
#include <iostream>

int main()
{
    int left = 10;
    int right = 2;
    std::cout << "left=" << left << std::endl;
    std::cout << "right=" << right << std::endl;
    int total = left + right;
    std::cout << total << std::endl;
    return 0;
}

用 g++ -std=c++17 box.cpp -o box 重新构建并运行 ./box。屏幕显示 left=10,然后 right=2,然后 12。

📊 compiling 构建与屏幕显示

片段 屏幕显示
First left=10,因为 ./box 之后第一次带标签打印会运行
Last 12,因为 total 仍使用加号
Rule g++ 把 box.cpp 变成 box;./box 运行程序
Screen left=10 然后 right=2 然后 12

每次只改一件事。临时打印 ready,重新构建并运行;恢复 total。本页结束 compiling,停在 random 之前。三行:left=10 然后 right=2 然后 12。

🧠 测试您的知识

准备开始

测试您的知识

通过这个互动测验挑战自己,看看你对这个主题的理解程度如何

❓
4
问题
🎯
70%
及格要求
♾️
∞
时间
🔄
∞
尝试次数

📝 说明

  • 仔细阅读每个问题
  • 为每个问题选择最佳答案
  • 您可以随时重新参加测验
  • 您的进度将显示在顶部