Files
CMake_SingleRun/README.md
Steven Hobs d1f8114c3d init
2026-03-04 12:45:47 +08:00

60 lines
1.5 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# CMake 单文件练习项目
## 项目说明
这是一个基于CMake的练习项目用于演示如何使用CMake自动发现和编译src目录下的C/C++源文件。
## 项目结构
```
cmake_singlefile_run/
├── CMakeLists.txt # CMake配置文件
├── README.md # 项目说明(本文件)
├── include/ # 头文件目录
├── src/ # 源代码目录
│ ├── hello.c # C语言示例for循环打印0-10
│ └── hello_cpp.cc # C++语言示例for循环打印0-10
└── build/ # 编译输出目录(自动生成)
└── bin/ # 可执行文件输出目录
```
## 编译和运行
### 编译
```bash
mkdir -p build
cd build
cmake ..
cmake --build .
```
### 运行
```bash
# 运行C语言版本
./bin/hello_c
# 运行C++版本
./bin/hello_cpp_cc
```
## CMakeLists.txt 说明
项目使用CMake配置特点包括
- **自动源文件发现**:自动搜索 `src` 目录下所有 `.c .cc .cpp .cxx` 源文件
- **自动目标生成**:为每个源文件自动创建对应的可执行文件目标
- **标准设置**
- C语言C11 标准
- C++语言C++17 标准
- **输出目录**:所有可执行文件输出到 `build/bin` 目录
## 功能演示
两个示例程序都使用for循环打印i=0到10的数值
### C版本hello.c
使用 `printf()` 打印输出
### C++版本hello.cc
使用 `std::cout` 打印输出