init
This commit is contained in:
102
.gitignore
vendored
Normal file
102
.gitignore
vendored
Normal file
@@ -0,0 +1,102 @@
|
|||||||
|
# ========== CMake Build Files ==========
|
||||||
|
# CMake build directory
|
||||||
|
build/
|
||||||
|
cmake_build/
|
||||||
|
out/
|
||||||
|
.cache/
|
||||||
|
|
||||||
|
# ========== IDE and Editor Files ==========
|
||||||
|
# Visual Studio
|
||||||
|
*.vs/
|
||||||
|
.vs/
|
||||||
|
*.sln
|
||||||
|
*.vcxproj
|
||||||
|
*.vcxproj.filters
|
||||||
|
*.vcxproj.user
|
||||||
|
|
||||||
|
# VS Code
|
||||||
|
.vscode/
|
||||||
|
*.code-workspace
|
||||||
|
|
||||||
|
# JetBrains IDEs
|
||||||
|
.idea/
|
||||||
|
*.iml
|
||||||
|
*.iws
|
||||||
|
*.ipr
|
||||||
|
|
||||||
|
# Eclipse
|
||||||
|
.eclipse/
|
||||||
|
.settings/
|
||||||
|
.project
|
||||||
|
.classpath
|
||||||
|
|
||||||
|
# Vim/Neovim
|
||||||
|
*.swp
|
||||||
|
*.swo
|
||||||
|
*~
|
||||||
|
.*.swp
|
||||||
|
|
||||||
|
# Sublime Text
|
||||||
|
*.sublime-project
|
||||||
|
*.sublime-workspace
|
||||||
|
|
||||||
|
# ========== Compiler and Build Artifacts ==========
|
||||||
|
# Object files
|
||||||
|
*.o
|
||||||
|
*.obj
|
||||||
|
*.a
|
||||||
|
*.lib
|
||||||
|
*.so
|
||||||
|
*.dylib
|
||||||
|
*.dll
|
||||||
|
|
||||||
|
# Executables
|
||||||
|
*.exe
|
||||||
|
*.out
|
||||||
|
*.app
|
||||||
|
|
||||||
|
# ========== OS-specific Files ==========
|
||||||
|
# macOS
|
||||||
|
.DS_Store
|
||||||
|
.AppleDouble
|
||||||
|
.LSOverride
|
||||||
|
*.swp
|
||||||
|
|
||||||
|
# Windows
|
||||||
|
Thumbs.db
|
||||||
|
ehthumbs.db
|
||||||
|
Desktop.ini
|
||||||
|
$RECYCLE.BIN/
|
||||||
|
|
||||||
|
# Linux
|
||||||
|
.directory
|
||||||
|
|
||||||
|
# ========== Dependency and Package Files ==========
|
||||||
|
# Python
|
||||||
|
__pycache__/
|
||||||
|
*.py[cod]
|
||||||
|
*$py.class
|
||||||
|
*.egg-info/
|
||||||
|
dist/
|
||||||
|
build/
|
||||||
|
|
||||||
|
# Node.js
|
||||||
|
node_modules/
|
||||||
|
npm-debug.log
|
||||||
|
yarn-error.log
|
||||||
|
|
||||||
|
# ========== Temporary and Log Files ==========
|
||||||
|
# CMake
|
||||||
|
CMake.log
|
||||||
|
CMakeUserPresets.json
|
||||||
|
|
||||||
|
# Compilation database
|
||||||
|
compile_commands.json
|
||||||
|
|
||||||
|
# Log files
|
||||||
|
*.log
|
||||||
|
|
||||||
|
# Temporary files
|
||||||
|
*.tmp
|
||||||
|
*.temp
|
||||||
|
.temp/
|
||||||
70
CMakeLists.txt
Normal file
70
CMakeLists.txt
Normal file
@@ -0,0 +1,70 @@
|
|||||||
|
# 指定CMake最低版本为3.16
|
||||||
|
cmake_minimum_required(VERSION 3.16)
|
||||||
|
|
||||||
|
# 项目名称:singlefile_practice,支持C和C++两种语言
|
||||||
|
project(singlefile_practice LANGUAGES C CXX)
|
||||||
|
|
||||||
|
# ========== C语言标准设置 ==========
|
||||||
|
# 使用C11标准
|
||||||
|
set(CMAKE_C_STANDARD 11)
|
||||||
|
# 要求严格遵循C标准,不允许编译器扩展
|
||||||
|
set(CMAKE_C_STANDARD_REQUIRED ON)
|
||||||
|
# 禁用编译器特定的扩展
|
||||||
|
set(CMAKE_C_EXTENSIONS OFF)
|
||||||
|
|
||||||
|
# ========== C++语言标准设置 ==========
|
||||||
|
# 使用C++17标准
|
||||||
|
set(CMAKE_CXX_STANDARD 17)
|
||||||
|
# 要求严格遵循C++标准,不允许编译器扩展
|
||||||
|
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||||
|
# 禁用编译器特定的扩展
|
||||||
|
# 禁用编译器特定的扩展
|
||||||
|
set(CMAKE_CXX_EXTENSIONS OFF)
|
||||||
|
|
||||||
|
# ========== 包含目录设置 ==========
|
||||||
|
# 添加include目录到编译器的头文件搜索路径
|
||||||
|
include_directories("./include")
|
||||||
|
|
||||||
|
# ========== 源文件自动发现 ==========
|
||||||
|
# 定义源代码根目录
|
||||||
|
# 定义源代码根目录
|
||||||
|
set(SOURCE_ROOT "${CMAKE_CURRENT_SOURCE_DIR}/src")
|
||||||
|
|
||||||
|
# 递归搜索src目录下所有C/C++源文件(.c .cc .cpp .cxx)
|
||||||
|
# CONFIGURE_DEPENDS: 如果文件列表变化,自动重新运行CMake配置
|
||||||
|
file(GLOB_RECURSE PRACTICE_SOURCES CONFIGURE_DEPENDS
|
||||||
|
"${SOURCE_ROOT}/*.c"
|
||||||
|
"${SOURCE_ROOT}/*.cc"
|
||||||
|
"${SOURCE_ROOT}/*.cpp"
|
||||||
|
"${SOURCE_ROOT}/*.cxx"
|
||||||
|
)
|
||||||
|
|
||||||
|
# 如果没有找到源文件,输出警告信息
|
||||||
|
if(NOT PRACTICE_SOURCES)
|
||||||
|
message(WARNING "No C/C++ source files found under ${SOURCE_ROOT}")
|
||||||
|
endif()
|
||||||
|
|
||||||
|
# ========== 自动为每个源文件创建可执行文件目标 ==========
|
||||||
|
# 遍历所有找到的源文件
|
||||||
|
foreach(SOURCE_FILE IN LISTS PRACTICE_SOURCES)
|
||||||
|
# 计算源文件相对于src目录的相对路径
|
||||||
|
file(RELATIVE_PATH REL_PATH "${SOURCE_ROOT}" "${SOURCE_FILE}")
|
||||||
|
|
||||||
|
# 将路径中的反斜杠和正斜杠替换为下划线
|
||||||
|
string(REPLACE "\\" "_" TARGET_NAME "${REL_PATH}")
|
||||||
|
string(REPLACE "/" "_" TARGET_NAME "${TARGET_NAME}")
|
||||||
|
|
||||||
|
# 将文件名中的点替换为下划线(例如:hello.c -> hello_c)
|
||||||
|
string(REPLACE "." "_" TARGET_NAME "${TARGET_NAME}")
|
||||||
|
|
||||||
|
# 为该源文件创建可执行文件目标
|
||||||
|
add_executable(${TARGET_NAME} "${SOURCE_FILE}")
|
||||||
|
|
||||||
|
# 设置可执行文件的输出目录为 build/bin
|
||||||
|
set_target_properties(${TARGET_NAME} PROPERTIES
|
||||||
|
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin"
|
||||||
|
)
|
||||||
|
|
||||||
|
# 输出编译日志信息
|
||||||
|
message(STATUS "Add target: ${TARGET_NAME} <- ${REL_PATH}")
|
||||||
|
endforeach()
|
||||||
59
README.md
Normal file
59
README.md
Normal file
@@ -0,0 +1,59 @@
|
|||||||
|
# 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` 打印输出
|
||||||
8
src/hello.c
Normal file
8
src/hello.c
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
for (int i = 0; i <= 10; i++) {
|
||||||
|
printf("i=%d\n", i);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
8
src/hello.cc
Normal file
8
src/hello.cc
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
for (int i = 0; i <= 10; i++) {
|
||||||
|
std::cout << "i=" << i << std::endl;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user