全班分成两组。你只做自己这一组,一共 6 关。前 4 关只用记事本和命令行,后 2 关才请 VS Code 来帮忙。每关最后有一个绿色或蓝色的「通关对照」。你的屏幕和它一致,再打勾。The class splits into two groups. Do only your own group. There are 6 levels. Levels 1 to 4 use a plain text editor and the command line. VS Code joins at levels 5 and 6. Each level ends with a green or blue Pass check. Tick the box when your screen matches it.
Python 和 C++ 都是高级语言,人读得懂。Python 用解释器:说一句,做一句。C++ 用编译器:整篇翻译完,再交给你一个能运行的程序。Python and C++ are both high-level languages: people can read them. Python uses an interpreter: it does one statement, then the next. C++ uses a compiler: it translates the whole program, then gives you a program you can run.
可以上网搜教程。搜完回到这一页,用通关对照判断自己做对没有。看到「一键安装包」或来路不明的下载,关掉。You may search for tutorials. Then come back and use the Pass check. Close any page that offers a mystery one-click pack.
输入命令前,把输入法换成英文。文件夹名不要加中文和空格。对照框里的字是结果,不要把它再打进命令行。Switch your keyboard to English before you type a command. Keep Chinese characters and spaces out of folder names. The Pass check is a result. Do not type it in.
把 Python 请到电脑上Invite Python onto the computer
Python 3.15.2,2026 年 10 月 25 日发布。页面上带 3.16 或 pre-release 的是测试版,这次不用。Use Python 3.15.2, released on 25 October 2026. A file marked 3.16 or pre-release is a test version. Leave it for today.
- 打开「命令提示符」:开始菜单输入 Open Command Prompt: in the Start menu, type
cmd。. - 建立文件夹。如果提示已经存在,继续下一关即可。Make the folders. If the computer says they already exist, go on to the next level.
打进命令行Type this in
mkdir C:\0478lab mkdir C:\0478lab\python cd /d C:\0478lab\python
- 安装文件用教师发的 Install the file from your teacher:
python-3.15.2-amd64.exe(约 35.9 MB)。自己下载时打开 (about 35.9 MB). To download it yourself, open the Python 3.15.2 发布页Python 3.15.2 release page,点 and choose Windows installer (64-bit)。页面上的 Python install manager 这次不用。. Leave the Python install manager for today. - 第一屏最下面勾选 On the first screen, tick Add python.exe to PATH。这句话的意思是:以后在任何文件夹都能叫到 Python。然后点 . This lets you call Python from any folder. Then click Install Now。.
- 关掉命令提示符,重新打开,输入:Close Command Prompt, open it again, and type:
打进命令行Type this in
py -3 --version py -3 -c "import sys; print(sys.executable)"
通关对照Pass check
下面是结果,不要输入。This is the result. Do not type it.
Python 3.15.2 C:\Users\YourName\AppData\Local\Programs\Python\Python315\python.exe
第一行要一字不差。第二行是解释器在硬盘上的位置,抄在纸上。文件夹名字可以和示例不同,只要以 python.exe 结尾。Line 1 must match exactly. Line 2 is where the interpreter lives. Copy it onto paper. The folders may differ from the example. The line should end with python.exe.
python 打开了应用商店: opens the Microsoft Store:到「设置 → 应用 → 高级应用设置 → 应用执行别名」,把 go to Settings → Apps → Advanced app settings → App execution aliases, and turn off python.exe 和 and python3.exe 关掉。后面继续用 . Keep using py -3。.- 打开「终端」。Open Terminal.
- 安装教师发的 Install the file from your teacher:
python-3.15.2-macos11.pkg(约 78.1 MB,给 macOS 10.15 及更新的系统。文件名里的 11 不是要求你的系统必须是 11)。自己下载时打开 (about 78.1 MB, for macOS 10.15 or newer. The 11 in the file name does not mean your Mac must be version 11). To download it yourself, open 同一个发布页the same release page,点 macOS installer。电脑原来自带的 Python 这次不用。 and choose the macOS installer. Leave the Python that came with the Mac. - 关掉终端再打开:Close Terminal and open it again:
打进终端Type this in
python3 --version python3 -c "import sys; print(sys.executable)"
通关对照Pass check
下面是结果,不要输入。This is the result. Do not type it.
Python 3.15.2 /Library/Frameworks/Python.framework/Versions/3.15/bin/python3
第一行要一字不差。第二行抄在纸上。用官网安装包时,通常就是这一条。位置不同也可以,只要第一行是 Python 3.15.2。Line 1 must match exactly. Copy line 2 onto paper. The official installer usually prints this path. Another path is fine when line 1 is Python 3.15.2.
用最普通的记事本写四行Write four lines in a plain editor
先不要打开 VS Code。我们要看见:没有集成环境,程序也写得出来。Leave VS Code closed. You are about to see that a program can be written without an IDE.
cd /d C:\0478lab\python notepad hello.py
问你要不要新建文件时,点「是」。把下面四行粘进去,按 If it asks to create a new file, choose Yes. Paste the four lines and press Ctrl+S。编码选 UTF-8。保存后回到命令行,输入 . Choose UTF-8. Then go back to Command Prompt and type type hello.py。.
mkdir -p ~/0478lab/python cd ~/0478lab/python touch hello.py open -e hello.py
如果窗口上有粗体、字号按钮,先选菜单「格式 → 制作纯文本」。引号要用直的 If you can see bold and font-size buttons, choose Format → Make Plain Text. Use straight quotes "。保存后回到终端,输入 . Save, return to Terminal, and type cat hello.py。.
first = 20 second = 22 total = first + second print(total)
这四行是在说:20 加 22,把结果记在 total 里,然后显示出来。These four lines say: add 20 and 22, remember the result as total, then show it.
通关对照Pass check
type hello.py 或 cat hello.py 应原样打出这四行。type hello.py or cat hello.py should print these four lines unchanged.
first = 20 second = 22 total = first + second print(total)
文件名正好是 hello.py。如果是 hello.py.txt,先打开文件扩展名,删掉多余的 .txt,再对照一次。The file name is exactly hello.py. If it is hello.py.txt, show file extensions, remove the extra .txt, and check again.
让命令行算出 42Let the command line work out 42
dir py -3 hello.py
dir 里的名字必须正好是 hello.py。如果是 hello.py.txt,在资源管理器里打开「查看 → 显示 → 文件扩展名」,把多余的 .txt 删掉。In dir, the name must be exactly hello.py. If it is hello.py.txt, open View → Show → File name extensions in File Explorer and remove the extra .txt.
ls python3 hello.py
通关对照Pass check
程序自己打出的最后一行:The last line the program prints:
42
只有数字 42。没有引号,没有小数点,也没有别的字。Just the digits 42. No quotes, no decimal point, and no other words.
故意写错,看它在哪里停Make one mistake and see where it stops
这一关就是要看到报错。报错出现,说明你看懂了解释器。This level is meant to show an error. The error means you can see how the interpreter works.
notepad bad.py
touch bad.py open -e bad.py
print("start")
print(first)
print("end")py -3 bad.py dir
python3 bad.py ls
通关对照Pass check
2026 年 11 月 19 日,Python 3.15.2 打出的是:On 19 November 2026, Python 3.15.2 printed:
start
Traceback (most recent call last):
File "bad.py", line 2, in <module>
print(first)
^^^^^
NameError: name 'first' is not defined. Did you mean: 'list'?
第一行是 start。NameError 那一行要有 first。end 不会出现。File 那一行可以带上文件夹路径。Did you mean 多一句也算过。dir 或 ls 里没有 exe,也没有可以单独拿走的程序。Line 1 is start. The NameError line includes first. end does not appear. The File line may include a folder path. An extra Did you mean line still passes. dir or ls shows no exe and no program you can take away on its own.
请 VS Code 来到同一个窗口Bring VS Code into the same window
用 VS Code 1.147.0,2026 年 11 月 18 日的正式版。Windows 选 User Installer 的 x64,并勾选「添加到 PATH」。Mac 选 Universal 版,拖进「应用程序」。需要 macOS 12 或更新的系统。Use VS Code 1.147.0, the release from 18 November 2026. On Windows, choose the User Installer for x64 and tick Add to PATH. On a Mac, choose Universal and drag it to Applications. It needs macOS 12 or newer.
- 打开 VS Code,选择「文件 → 打开文件夹」,打开刚才的 Open VS Code, choose File → Open Folder, and open the
python文件夹。问你信不信任时,选信任。 folder. If it asks whether you trust the folder, choose trust. - 左侧扩展里搜 Python,安装发布者是 In Extensions, search for Python and install the one published by Microsoft 的那个。别的「一键运行」扩展先不要装。. Leave other one-click run extensions for later.
- 按 Press Ctrl+Shift+P(Mac 用 (on a Mac, Command+Shift+P),输入 ), type
Python: Select Interpreter,选纸上那条 3.15。列表里没有,就选 Enter interpreter path,把路径粘进去。, and choose the 3.15 path from your paper. If it is missing, choose Enter interpreter path and paste the path. - 打开 Open
hello.py,在字上面右键,选 , right-click the text, and choose Run Python File in Terminal。中文界面可能写成「在终端中运行 Python 文件」。. A Chinese menu may say「在终端中运行 Python 文件」.
通关对照Pass check
终端最后一行:The last line in the terminal:
42
最后一行上面的命令里,能看见 python 和 hello.py。数字仍是单独一行的 42。The command just above it shows python and hello.py. The number is still 42 on a line by itself.
试四个小帮手Try the four helpers
- 补全。Autocomplete.新的一行打 On a new line, type
pr,列表里出现 . When the list showsprint就接受,然后把这行删掉。它只是帮你把单词补完。, accept it, then delete that line. It only finishes the word for you. - 波浪线。The wavy line.把 Change
print(total)临时改成 toprint(totl)并保存。 and save.totl下面会出现波浪线。看完改回去。 gets a wavy line. Then change it back. - 整理书写。Tidy the writing.把缩进弄乱,按 Mess up the indent, then press Shift+Alt+F。Mac 按 . On a Mac, press Shift+Option+F。问你用哪个工具时,选 Python。它只把字排整齐。. If it asks which tool, choose Python. It only lines the words up.
- 暂停看变量。Pause and look at the variables.在 Click in the margin of the
print(total)这一行的行号左边点一下,出现红点。按 line so a red dot appears. Press F5。问你时选 Python Debugger,再选 Python File。让你安装 debugpy 时,安装。停下后看左边的变量。按 . Choose Python Debugger, then Python File. If it offers to install debugpy, install it. When it pauses, look at the variables on the left. Press Shift+F5 结束。 to finish.
通关对照Pass check
这一关看四个现象,不是一行数字。This level has four things to see. It is not one number.
| 你做的事What you do | 标准结果What you should see |
|---|---|
pr | 列表里有 printThe list contains print |
print(totl) | totl 下面有波浪线A wavy line under totl |
| 整理书写Tidy the writing | 缩进恢复整齐The indent is tidy again |
| 红点暂停Pause on the red dot | first = 20,second = 22,total = 42 |
看完波浪线后,把 totl 改回 total。四个都看见,这一关过了。After you see the wavy line, change totl back to total. All four sights mean this level is done.
把编译器请到电脑上Invite the compiler onto the computer
编译器负责把整份程序翻译成一个可以运行的文件。The compiler translates the whole program into a file you can run.
主安装包是 MSYS2,版本 The main installer is MSYS2, version 20260927,2026 年 9 月 27 日发布。文件名 , released on 27 September 2026. The file is msys2-x86_64-20260927.exe,约 81 MB。官网写着 latest 的那个,在 2026 年 11 月 19 日也是这一份。, about 81 MB. On 19 November 2026, the file marked latest on the website was still this one.
- 运行安装包,安装位置保持 Run the installer and keep the location as
C:\msys64。. - 打开开始菜单里的 Open MSYS2 UCRT64。只叫 MSYS 的另一个图标这次不用。 from the Start menu. Leave the icon that only says MSYS.
- 先更新。窗口若自己关掉,重新打开 UCRT64,再执行一次,直到它不再要求关闭。Update first. If the window closes itself, open UCRT64 again and run the command once more, until it stops asking to close.
打进 UCRT64Type this in UCRT64
pacman -Syu
- 安装编译器和调试器。出现名单按 Enter,问你是否继续时输入 Install the compiler and the debugger. Press Enter for the list. When it asks to continue, type
Y。.打进 UCRT64Type this in UCRT64pacman -S --needed mingw-w64-ucrt-x86_64-gcc mingw-w64-ucrt-x86_64-gdb
- 还在这个窗口里确认。只要出版本号就行。官方示例里常见 GCC 16.2.0,数字更高也可以。Still in this window, check the versions. Any version number is enough. The official example often shows GCC 16.2.0. A higher number is fine.
打进 UCRT64Type this in UCRT64
g++ --version gdb --version
- 让普通命令行也找得到它。开始菜单搜「编辑账户的环境变量」,在用户变量的 Path 里新建一行 Let ordinary Command Prompt find it too. Search the Start menu for “Edit environment variables for your account”. Under the user variable Path, add
C:\msys64\ucrt64\bin。Path 就是电脑找程序时会查看的文件夹名单。. Path is the list of folders the computer searches when you ask for a program. - 关掉旧窗口,新开「命令提示符」:Close the old window and open a new Command Prompt:
打进命令行Type this in
g++ --version gdb --version where g++ where gdb
通关对照Pass check
形状像这样。括号里的字可以不同。The shape looks like this. The words in brackets may differ.
g++.exe (...) 16.2.0 GNU gdb (GDB) ... C:\msys64\ucrt64\bin\g++.exe C:\msys64\ucrt64\bin\gdb.exe
g++ 和 gdb 都要有版本号。16.2.0 或更高都算过。where 印出的两条路径都在 ucrt64\bin 里,把它们抄在纸上。Both g++ and gdb show a version number. 16.2.0 or higher passes. The two paths from where are inside ucrt64\bin. Copy them onto paper.
学校电脑不允许安装软件时If the school computer does not allow this install
用教师发的 w64devkit Use w64devkit 2.11.0,2026 年 10 月 29 日发布。文件名是 from your teacher, released on 29 October 2026. The file is w64devkit-x64-2.11.0.7z.exe,约 65.6 MB。文件名里带 x86 的那一个这次不用。, about 65.6 MB. Leave the file whose name contains x86.
- 解压后找到 After it unpacks, find
g++.exe。它所在的 . Thebin文件夹就是要加入 Path 的地方。 folder that contains it is the folder you add to Path. - 双击 Double-click
w64devkit.exe,输入 and typeg++ --version和 andgdb --version。. - 把这个 Add this
bin加进用户变量 Path。这一步通常不需要管理员。 to the user Path variable. This step usually needs no administrator. - 新开命令提示符,再执行 Open a new Command Prompt and run
where g++。后面的命令都在命令提示符里打。纸上的路径代替后文里的 . Type the later commands in Command Prompt. Use the path from your paper in place ofC:\msys64\ucrt64\bin。 later on.
这条备用路的通关对照Pass check for this spare route
g++.exe (...) ... GNU gdb (GDB) ... C:\path\on\your\paper\g++.exe C:\path\on\your\paper\gdb.exe
两条 where 路径都落在你找到 g++.exe 的那个 bin 里,并且两条 version 都有数字。Both where paths are in the bin folder that contains g++.exe, and both version commands show a number.
Mac 不用再下一个很大的安装包。系统会弹出「命令行工具」,里面就有编译器。A Mac does not need another large installer. The system offers Command Line Tools, and the compiler is inside them.
xcode-select --install
弹出窗口后选择安装,等它结束。如果它说已经装过,直接继续。装完新开一个终端:Choose Install in the window and wait. If it says the tools are already there, continue. Then open a new Terminal:
g++ --version clang++ --version lldb --version which clang++
通关对照Pass check
2026 年 11 月 19 日,教师机的前两行是:On 19 November 2026, the teacher’s computer began with:
Apple clang version 21.0.0 (clang-2100.4.1.1) Target: arm64-apple-darwin25.6.0 /usr/bin/clang++
g++、clang++、lldb 三条都要有版本号。which 印出 /usr/bin/clang++,把这一条抄下来。括号里的小版本、Target 里的系统号可以不同。Intel 芯片的 Target 里会出现 x86_64,也算过。这里的 g++ 会去调用 Apple 的编译器,做的事仍然是:整份翻译,生成一个能运行的程序。g++, clang++, and lldb each show a version. which prints /usr/bin/clang++. Copy that line. The small build number in brackets, and the system number in Target, may differ. An Intel Mac may show x86_64 in Target. That still passes. Here g++ calls Apple’s compiler. It still translates the whole program and makes a file you can run.
用记事本写下同一道加法Write the same addition in a plain editor
mkdir C:\0478lab mkdir C:\0478lab\cpp cd /d C:\0478lab\cpp notepad hello.cpp
新建文件后粘贴下面的程序。另存为时编码选 Create the file and paste the program. When you save, choose ANSI,按 , then press Ctrl+S。回到命令行,输入 . Back in Command Prompt, type type hello.cpp。.
mkdir -p ~/0478lab/cpp cd ~/0478lab/cpp touch hello.cpp open -e hello.cpp
有格式按钮时,先选「格式 → 制作纯文本」。引号用直的。保存后输入 If you can see formatting buttons, choose Format → Make Plain Text. Use straight quotes. After saving, type cat hello.cpp。.
#include <iostream>
int main() {
int first = 20;
int second = 22;
int total = first + second;
std::cout << total << std::endl;
return 0;
}意思和 Python 那四行一样:20 加 22,把 42 显示出来。写法更严,所以它仍然是高级语言。It means the same thing as the four Python lines: add 20 and 22, then show 42. The writing has stricter rules, and it is still a high-level language.
通关对照Pass check
type hello.cpp 或 cat hello.cpp 应打出和上面一样的程序。type hello.cpp or cat hello.cpp should print the same program as the box above.
#include <iostream>
int main() {
int first = 20;
int second = 22;
int total = first + second;
std::cout << total << std::endl;
return 0;
}
文件名正好是 hello.cpp。尖括号和两个小于号都要在。The file name is exactly hello.cpp. The angle brackets and the two less-than signs are all there.
翻译出成品,再运行Translate it, then run the result
dir g++ -o hello.exe hello.cpp dir hello.exe
如果报错里有 If the error contains stray 和 and \357,回到记事本,用 ANSI 编码另存为,覆盖原文件,再编译一次。, go back to Notepad, save again as ANSI over the old file, and compile once more.
通关对照Pass check
第二次名单里多出 hello.exe。运行后的最后一行:The second list has a new hello.exe. After you run it, the last line is:
42
dir 还会显示日期和大小,那些可以不同。新出现的文件名必须是 hello.exe,最后一行必须是 42。dir also shows dates and sizes. Those may differ. The new file name is hello.exe, and the last line is 42.
ls g++ -o hello hello.cpp ls -l ./hello
通关对照Pass check
第二次名单里多出 hello,没有 .exe。运行后的最后一行:The second list has a new hello, with no .exe. After you run it, the last line is:
42
ls -l 还会显示权限和大小。那些可以不同。新文件名必须是 hello,最后一行必须是 42。ls -l also shows permissions and size. Those may differ. The new file name is hello, and the last line is 42.
故意写错,看它交不交新程序Make one mistake and see whether a new program appears
notepad bad.cpp
touch bad.cpp open -e bad.cpp
#include <iostream>
int main() {
std::cout << "start" << std::endl;
std::cout << first << std::endl;
std::cout << "end" << std::endl;
return 0;
}g++ -o hello2.exe bad.cpp dir hello2.exe hello.exe
通关对照Pass check
报告里要有这一句。前后可能还有别的行。The report contains this line. Other lines may appear around it.
error: 'first' was not declared in this scope
然后原来的 hello.exe 再打出:Then the old hello.exe prints:
42
dir hello2.exe 找不到文件。屏幕上没有 start。报告里有 first。旧程序的最后一行仍是 42。这三件一起算过。dir hello2.exe cannot find the file. start does not appear. The report contains first. The old program still ends with 42. All three together are a pass.
g++ -o hello2 bad.cpp ls hello2 ./hello
通关对照Pass check
2026 年 11 月 19 日,教师机打出的报告是:On 19 November 2026, the teacher’s computer printed:
bad.cpp:4:18: error: use of undeclared identifier 'first'
4 | std::cout << first << std::endl;
| ^~~~~
1 error generated.
原来的 ./hello 再打出:Then the old ./hello prints:
42
ls hello2 会说没有这个文件。报告里有 first 和 undeclared identifier。屏幕上没有 start。旧程序的最后一行仍是 42。ls hello2 says there is no such file. The report contains first and undeclared identifier. start does not appear. The old program still ends with 42.
只带走成品Take only the finished program
mkdir ship copy hello.exe ship\ cd ship hello.exe dir
通关对照Pass check
运行打出 42。随后的名单里有 hello.exe。Running it prints 42. The list afterwards contains hello.exe.
42
ship 里没有 hello.cpp,也没有再调用 g++。名单里的日期和大小可以不同。ship has no hello.cpp, and you did not call g++ again. Dates and sizes in the list may differ.
mkdir -p ship cp hello ship/ cd ship ./hello ls
通关对照Pass check
运行打出 42。随后的名单里有 hello。Running it prints 42. The list afterwards contains hello.
42
ship 里没有 hello.cpp,也没有再调用 g++。ship has no hello.cpp, and you did not call g++ again.
让 VS Code 把这些事放在一起Let VS Code hold these jobs together
安装 VS Code Install VS Code 1.147.0(2026 年 11 月 18 日)。Windows 用 User Installer 的 x64,Mac 用 Universal。扩展只装 Microsoft 的 (18 November 2026). Windows uses the User Installer for x64. Mac uses Universal. Install only Microsoft’s C/C++。打开 extension. Open the cpp 文件夹并信任它。先回到上一级文件夹,再打开 cpp。如果现在还在 ship 里面,成品会生成到错误的地方。 folder and trust it. Move up one folder first, then open cpp. If you are still inside ship, the result will be created in the wrong place.
- 打开 Open
hello.cpp。按 . Press Ctrl+Shift+P(Mac 为 (on a Mac, Command+Shift+P),输入 ) and typeEdit Configurations (UI)。这是一张设置表,照着填。. This opens a settings form. Fill it in as shown.
| 填哪里Where to type | Windows,MSYS2Windows, MSYS2 | Windows,w64devkitWindows, w64devkit | Mac |
|---|---|---|---|
| Compiler path 编译器在哪where the compiler is | C:/msys64/ucrt64/bin/g++.exe | 纸上的 g++.exethe g++.exe from your paper | /usr/bin/clang++ |
| IntelliSense mode 哪一种编译器which compiler | windows-gcc-x64 | windows-gcc-x64 | 先在终端打 In Terminal, type uname -m。是 arm64 就选 macos-clang-arm64,是 x86_64 就选 macos-clang-x64. Choose macos-clang-arm64 for arm64, or macos-clang-x64 for x86_64 |
| C++ standard | c++17 | c++17 | c++17 |
- 菜单选「终端 → 配置默认生成任务」。Windows 选带 g++.exe 的那一项,Mac 选 clang++ 或 g++。名单是空的,就打开本关最底下的备用配置。Choose Terminal → Configure Default Build Task. On Windows, choose the item with g++.exe. On a Mac, choose clang++ or g++. If the list is empty, open the spare settings at the bottom of this level.
- 按 Press Ctrl+Shift+B(Mac 为 (on a Mac, Command+Shift+B)。最后看到退出代码 0,就是翻译成功。英文界面写成 exit code: 0。). Exit code 0 means the translation succeeded. A Chinese window may say 退出代码 0.
然后在 VS Code 的终端里运行。这里要加 Then run it in the VS Code terminal. Add .\,因为这个终端常常是 PowerShell。 here, because this terminal is often PowerShell.
.\hello.exe
./hello
- 新的一行打 On a new line, type
std::co,出现 . Whencout就接受,再删掉这行。这是自动完成。没有列表时,回到设置表看看编译器路径。 appears, accept it, then delete that line. This is autocomplete. If there is no list, check the compiler path on the settings form. - 把 Change
total临时改成 tototl,再按生成快捷键。这次必须失败,报告里有 , then press the build shortcut. This one must fail, and the report containstotl。改回去,再次生成成功。. Change it back, and build successfully again. - 左边「运行和调试」里新建 In Run and Debug on the left, create
launch.json,选 C++ (GDB/LLDB)。Windows 选 g++ 那一项,Mac 选 clang++ 那一项。对不上就用备用配置。 and choose C++ (GDB/LLDB). On Windows choose the g++ item. On a Mac choose the clang++ item. If nothing matches, use the spare settings. - 在显示 total 的那一行左边点出红点,按 Click a red dot in the margin of the line that shows total, then press F5。能看见 first 是 20,second 是 22,total 是 42。按 . You can see first is 20, second is 22, and total is 42. Press Shift+F5 结束。 to finish.
通关对照Pass check
| 你做的事What you do | 标准结果What you should see |
|---|---|
| 生成 hello.cppBuild hello.cpp | 退出代码 0,英文界面是 exit code: 0exit code: 0 |
| 运行成品Run the result | 42 |
std::co | 列表里有 coutThe list contains cout |
totl | 生成失败,报告里有 totlThe build fails and the report contains totl |
| 红点暂停Pause on the red dot | first = 20,second = 22,total = 42 |
把 totl 改回 total 之后,再生成一次,又是退出代码 0。After you change totl back to total, build once more and get exit code 0 again.
菜单里没有编译器时才看Look here only when the menu has no compiler
工作区里没有 If the workspace has no .vscode 文件夹就新建一个。两段都整份替换。里面的任务名字必须彼此一样。 folder, create one. Replace each file completely. The task names must match each other.
Windows 的 tasks.jsontasks.json for Windows
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++.exe build active file",
"command": "C:\\msys64\\ucrt64\\bin\\g++.exe",
"args": ["-fdiagnostics-color=always", "-g", "${file}", "-o", "${fileDirname}\\${fileBasenameNoExtension}.exe"],
"options": { "cwd": "${fileDirname}" },
"problemMatcher": ["$gcc"],
"group": { "kind": "build", "isDefault": true }
}
]
}Windows 的 launch.jsonlaunch.json for Windows
{
"version": "0.2.0",
"configurations": [
{
"name": "C/C++: g++.exe build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "C:\\msys64\\ucrt64\\bin\\gdb.exe",
"setupCommands": [
{ "description": "Enable pretty-printing for gdb", "text": "-enable-pretty-printing", "ignoreFailures": true }
],
"preLaunchTask": "C/C++: g++.exe build active file"
}
]
}用 w64devkit 的同学,把两处 If you use w64devkit, replace both copies of C:\\msys64\\ucrt64\\bin 换成纸上的 bin 路径,反斜杠写成 with the bin path from your paper. Write each backslash as \\。.
Mac 的 tasks.jsontasks.json for Mac
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: clang++ build active file",
"command": "/usr/bin/clang++",
"args": ["-std=c++17", "-fcolor-diagnostics", "-fansi-escape-codes", "-g", "${file}", "-o", "${fileDirname}/${fileBasenameNoExtension}"],
"options": { "cwd": "${fileDirname}" },
"problemMatcher": ["$gcc"],
"group": { "kind": "build", "isDefault": true }
}
]
}Mac 的 launch.jsonlaunch.json for Mac
{
"version": "0.2.0",
"configurations": [
{
"name": "C/C++: clang++ build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "lldb",
"preLaunchTask": "C/C++: clang++ build active file"
}
]
}两组碰头The two groups meet
用自己的屏幕,把下面三句说给另一组听。Use your own screen and say these three things to the other group.
通关对照Pass check
Python 组说:写错时,start 已经显示了,然后停在第 2 行的 NameError。文件夹里没有单独的程序。The Python group says: when the program was wrong, start had already appeared, then it stopped at the NameError on line 2. The folder had no separate program.
C++ 组说:写错时,start 没有显示,也没有新程序。ship 里只有成品,最后一行仍是 42。The C++ group says: when the program was wrong, start did not appear, and there was no new program. ship had only the finished program, and its last line was still 42.
一起说:两种都是高级语言。一个用解释器,一个用编译器。Together: both are high-level languages. One uses an interpreter. One uses a compiler.
隐藏关卡还锁着The hidden level is still locked
要等 Python 组和 C++ 组都通关。老师把口令写在黑板上之后,再输入。口令是四个汉字,两种语言都输入这一句。Wait until both the Python group and the C++ group have finished. Then type the password the teacher writes on the board. It is four Chinese characters. Type those characters in either language.
还不对。先把前面的关做完,等老师写在黑板上。Not yet. Finish the levels first, and wait for the teacher to write it on the board.
隐藏关卡 · 看见汇编Hidden level · See assembly
前面两组用的都是高级语言。这一关看一种更靠近 CPU 的写法。考试仍然用教材里的缩写,例如:Both groups used high-level languages. This level looks at a way of writing that sits closer to the CPU. In the exam, still use the textbook mnemonics, for example:
LDA First ADD Second STO Sum
真实电脑上的缩写和教材不一样,而且换一种芯片就会换一套。今天不要求你记这些英文。你要看见的是:缩写可以被汇编程序变成电脑能运行的文件。The mnemonics on a real computer differ from the textbook, and a different chip uses a different set. You do not memorise these English words today. You are here to see that an assembler can turn mnemonics into a file the computer can run.
这个小程序没有写「打印」。它把 20 加 22,把 42 交给系统。所以屏幕会先空着。紧接着看退出码,才会看到 42。空屏幕就是这一关该有的样子。This little program does not say “print”. It adds 20 and 22, then hands 42 back to the system. The screen stays blank. The next command shows the exit code, and that is where 42 appears. A blank screen is what this level should look like.
Windows
继续用第 1 关已经装好的工具,不用再下载。在「命令提示符」里:Keep using the tools from level 1. There is nothing new to download. In Command Prompt:
mkdir C:\0478lab\asm cd /d C:\0478lab\asm notepad add.s
.globl main
.text
main:
movl $20, %eax
addl $22, %eax
retmovl $20, %eax 是把 20 放进一个叫 eax 的小格子。 puts 20 into a small slot called eax. addl $22, %eax 是往里面加 22。 adds 22 to it. ret 是带着结果回去。 goes back with the result.
as -o add.o add.s gcc add.o -o add.exe add.exe echo %ERRORLEVEL%
通关对照Pass check
add.exe 之后屏幕是空的。紧接着只打一次 echo,它打出:After add.exe the screen is blank. Type echo once, immediately, and it prints:
42
这条 echo 用在命令提示符里。中间如果先打了别的命令,数字就不再是 42。再用记事本打开 add.exe,看到的是乱码。那已经接近机器码。这一段按 MSYS2 和 w64devkit 书写,教师会在课前用机房的 Windows 先跑一遍。This echo belongs in Command Prompt. If another command comes first, the number is no longer 42. Open add.exe in Notepad and you see a jumble. That is close to machine code. This listing is written for MSYS2 and w64devkit. The teacher runs it once on the lab Windows computers before the lesson.
Mac
先输入 First type uname -m。结果是 . If the result is arm64,用第一份。结果是 , use the first listing. If it is x86_64,用第二份。2026 年 11 月 19 日,Apple 芯片的教师机已经跑通第一份,退出码是 42。, use the second. On 19 November 2026, the teacher’s Apple-chip computer ran the first listing, and the exit code was 42.
mkdir -p ~/0478lab/asm cd ~/0478lab/asm uname -m
Apple 芯片,arm64Apple chip, arm64
.global _main
.align 2
_main:
mov w0, #20
add w0, w0, #22
retmov w0, #20 把 20 放进叫 w0 的格子。 puts 20 into a slot called w0. The add 那一行往里面加 22。 line adds 22. ret 带着结果回去。 goes back with the result.
Intel 芯片,x86_64Intel chip, x86_64
.global _main
.text
_main:
movl $20, %eax
addl $22, %eax
retas -o add.o add.s clang add.o -o add ./add echo $?
通关对照Pass check
./add 之后屏幕是空的。紧接着只打一次 echo $?,它打出:After ./add the screen is blank. Type echo $? once, immediately, and it prints:
42
再用文本编辑打开 add,看到乱码。Intel 这份由老师在那种 Mac 上先试一次。Apple 芯片这一份,教师机已经得到 42。Open add in TextEdit and you see a jumble. The teacher tries the Intel listing first on a Mac of that kind. The Apple-chip listing already returned 42 on the teacher’s computer.
高阶思考A further question
编译、解释、汇编到底是如何进行的?先用自己的三份程序说一遍,再打开下面的参考。How do compiling, interpreting, and assembling actually happen? Say it once from your own three programs, then open the reference.
先自己说,再看参考回答Say it yourself, then read the reference
可以写进考试的回答An answer you can write in the exam
解释器读高级语言的一条语句,翻译并马上执行,再处理下一条。遇到错误就停在这一条,并指出位置。我们的 An interpreter reads one statement of a high-level language, translates it, runs it at once, then moves to the next statement. At an error, it stops on that statement and shows where it is. Our bad.py 先输出了 start,然后停在第 2 行,end 没有执行。这样的程序每次运行通常都要解释器,文件夹里没有单独的成品。 printed start, then stopped on line 2, so end did not run. A program like this usually needs the interpreter every time it runs. The folder has no separate finished program.
编译器把整份高级语言源程序翻译成可执行文件,同时给出错误报告。我们的 A compiler translates a whole high-level source program into an executable file and also produces an error report. Our bad.cpp 有错,所以没有生成 hello2,屏幕上也没有 start。翻译成功的 hello 可以离开源文件单独运行。 had an error, so hello2 was not created and start did not appear. A hello that translated successfully can run on its own, away from the source file.
汇编程序把汇编语言的助记符翻译成机器码。一条助记符通常对应一条机器指令。今天的 An assembler translates assembly-language mnemonics into machine code. One mnemonic usually matches one machine instruction. Today as 读了 read add.s,生成 and made add.o。之后那个能运行的文件离开汇编程序也能被 CPU 执行。教材里的 LDA、ADD、STO 也是助记符,只是写法跟这台电脑的芯片不同。. After that, the CPU can run the finished file without the assembler. LDA, ADD and STO in the textbook are mnemonics too. Their spelling belongs to a different chip from this computer.
同一个 20 加 22The same 20 plus 22
Python:Python: total = first + second,解释器逐行做,. The interpreter does it line by line, and print 把 42 显示出来。 shows 42.
C++:同样的加法写在 C++: the same addition is in hello.cpp 里,. g++ 整份翻译成 hello,运行 hello 显示 42。 translates the whole file into hello, and running hello shows 42.
汇编:把 20 放进一个格子,加上 22,再返回。汇编程序把它变成机器码。这次没有写打印,所以 42 出现在退出码里。Assembly: put 20 into a slot, add 22, then return. The assembler turns that into machine code. This program does not print, so 42 appears in the exit code.
如果还想往下问If you still want to ask further
这三种翻译程序本身都是程序。编译器要先把整份源程序读完,认出每个词和每条句子,发现没介绍过的名字就列出错误、不交新文件;没有错误,再把一句高级语言换成这条 CPU 能做的许多条机器指令,打包成可执行文件。解释器不先交完整文件,而是读一条、做一条,所以错误出现前,前面的句子可能已经做完。汇编程序做的跨度更小,大多是把一条缩写换成一条机器指令。All three translators are programs themselves. A compiler first reads the whole source program, recognises the words and sentences, and lists an error when a name was never introduced. It then hands over no new file. With no error, it turns one high-level statement into many machine instructions this CPU can do, and packs them into an executable file. An interpreter does not hand over a whole file first. It reads one statement and does it, so statements before an error may already have run. An assembler takes a shorter step: usually one mnemonic becomes one machine instruction.
考试写到「谁来翻译、一次译完还是逐条做、最后留下什么」就够了。编译器内部还有更细的步骤,那是以后的内容,今天不用背。For the exam, it is enough to say who translates, whether it is the whole program or one statement at a time, and what is left afterwards. A compiler has smaller steps inside it. That is for later. You do not memorise those steps today.