C 环境设置
C - 环境搭建
Section titled “C - 环境搭建”在线尝试 C,无需安装
Section titled “在线尝试 C,无需安装”你不一定需要在本地安装 C 开发工具(C development tools)来开始学习。许多网站提供在线 C 编译器(online C compilers)和执行环境(execution environments),你可以在浏览器中直接编写、编译和运行 C 代码。这是快速尝试示例的好方法。
流行选择包括:
- Compiler Explorer (godbolt.org)
- OnlineGDB (onlinegdb.com)
- Repl.it (replit.com)
- JDoodle (jdoodle.com/c-online-compiler)
本教程中的大多数示例都可以使用这些在线工具进行测试。试试这个基本的“Hello, World!”程序:
#include <stdio.h> // 包含标准输入/输出头文件
int main(void) { // 主函数,程序执行从此开始 // 打印消息到控制台 printf("Hello, Modern C World!\n");
return 0; // 表示程序成功执行}本地环境搭建
Section titled “本地环境搭建”如果你更喜欢在本地工作或处理大型项目,你需要在你的计算机上搭建一个 C 开发环境(C development environment)。这通常需要两个主要组件:
- 文本编辑器(Text Editor)或 IDE: 用于编写 C 源代码(source code)。
- C 编译器(C Compiler): 将你的源代码翻译成机器可读的可执行文件(machine-readable executable files)。
文本编辑器 / 集成开发环境 (IDE)
Section titled “文本编辑器 / 集成开发环境 (IDE)”你将在纯文本文件中编写 C 代码,通常文件扩展名为 .c(源文件,source files),有时还有 .h(头文件,header files)。
你可以使用任何纯文本编辑器,但专注于编程的编辑器或 IDE 提供了一些有用的功能,如语法高亮(syntax highlighting)、代码补全(code completion)、调试集成(debugging integration)和构建系统支持(build system support)。
流行选择包括:
- 编辑器(Editors): Visual Studio Code (VS Code)、Sublime Text、Atom、Notepad++、Vim、Emacs。
- IDE: CLion(商业)、Code::Blocks(免费)、Eclipse CDT(免费)、Visual Studio (Windows,提供免费的 Community edition)。
编译器(compiler)接受人类可读的源代码(.c 文件)并将其翻译成机器码(machine code)(可执行程序或目标文件,object files)。它通常与链接器(linker)协同工作,链接器将目标文件和库(libraries)组合起来创建最终的可执行文件(final executable)。
当前最广泛使用的 C 编译器有:
- GCC (GNU Compiler Collection): 一个免费、开源且非常流行的编译器,几乎可在所有平台(platforms)上使用(Linux、macOS、Windows)。通常使用
gcc命令调用。 - Clang: 一个较新的开源编译器(LLVM project 的一部分),以其速度和出色的诊断(diagnostics)功能而闻名。也广泛可用。通常使用
clang命令调用。 - MSVC (Microsoft Visual C++ Compiler): 集成在 Windows Visual Studio 中的编译器。可以从命令行或 Visual Studio IDE 中使用。
安装步骤因操作系统(operating system)而异:
在 Linux 上安装
Section titled “在 Linux 上安装”GCC 通常预装或可通过你的发行版的软件包管理器(package manager)轻松安装。打开终端(terminal)尝试:
gcc --version# orclang --version如果它们未安装,请使用你的软件包管理器:
// 在基于 Debian/Ubuntu 的系统上:sudo apt updatesudo apt install build-essential # 安装 GCC, make, 和其他必需工具# (可选)安装 Clang:sudo apt install clang
// 在基于 Fedora/CentOS/RHEL 的系统上:sudo dnf updatesudo dnf groupinstall "Development Tools" # 安装 GCC, make 等# (可选)安装 Clang:sudo dnf install clang在 macOS 上安装
Section titled “在 macOS 上安装”最简单的方法是安装 Xcode 的 Command Line Tools。打开终端(Terminal app)(在 Applications/Utilities 中)并运行:
xcode-select --install按照提示进行操作。这将安装 Clang(macOS 通常将其软链接(symlinks)为 gcc)、Git 和其他必需的开发工具(development tools)。或者,从 App Store 安装完整的 Xcode IDE 也包含了这些工具。
在 Windows 上安装
Section titled “在 Windows 上安装”在 Windows 上获取 GCC/Clang 有几种选择:
- MinGW-w64: 提供了 GCC 的原生 Windows 移植版。你可以下载安装程序(例如,来自 MSYS2 或直接来自 MinGW-w64 project sources)。通常推荐使用 MSYS2 (msys2.org),因为它提供了软件包管理器(pacman),可以轻松安装 MinGW-w64 工具链(toolchains)(如
mingw-w64-x86_64-gcc)以及在类 Unix shell 环境(Unix-like shell environment)中的其他开发工具。 - Windows Subsystem for Linux (WSL): 允许你在 Windows 上直接运行 Linux 环境(Linux environment)。从 Microsoft Store 安装 WSL 和一个 Linux 发行版(如 Ubuntu)。然后,在 WSL 终端(WSL terminal)中按照 Linux 安装说明进行操作。
- Visual Studio: 安装 Visual Studio Community Edition(免费)。安装时选择“使用 C++ 的桌面开发”工作负载(workload)。这提供了 MSVC 编译器工具链(MSVC compiler toolchain),可以从包含的 Developer Command Prompt 或与 VS Code 集成使用。
命令行使用的注意事项: 如果在使用 MinGW-w64 或 MSYS2 时没有 IDE 自动处理,请确保包含 gcc.exe 的目录(例如 C:\msys64\mingw64\bin)已添加到 Windows 的 PATH 环境变量中,以便你可以从任何命令提示符(command prompt)运行编译器。
基本编译示例(命令行)
Section titled “基本编译示例(命令行)”- 将上面“Hello, World!”代码保存为
hello.c。 - 打开一个可以访问编译器的终端(terminal)或命令提示符(command prompt)。
- 使用 GCC 编译代码:
gcc hello.c -o hello # 在 Linux/macOS/MinGW/WSL 上# 或者使用 Clang:clang hello.c -o hello(如果在 Developer Command Prompt 中使用 MSVC,命令通常是 cl hello.c)。
这将创建一个名为 hello(在 Windows 上是 hello.exe)的可执行文件(executable file)。
- 运行可执行文件(executable):
./hello # 在 Linux/macOS/WSL/Git Bash 上hello.exe # 在 Windows Command Prompt/PowerShell 上你应该会看到输出:Hello, Modern C World!