# first_c **Repository Path**: lixiangyang1024/first_c ## Basic Information - **Project Name**: first_c - **Description**: 第一个用mac环境下编写的C项目,是为了完成通过mac电脑开发,然后通过git管理,同步到windows电脑的vscode的工程中进行编译,调试,最终发行,需要编写dll和测试的exe,要实现这个整个流程的起点 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2024-12-25 - **Last Updated**: 2024-12-25 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README 这是我的第一个用mac环境下编写的C项目,是为了完成通过mac电脑开发,然后通过git管理,同步到windows电脑的vscode的工程中进行编译,调试,最终发行,需要编写dll和测试的exe,要实现这个整个流程 的起点,我的项目目录已经创建好了,mkdir -p /Users/xyl/Documents/c_pro_plus/first_c 这是我初步的项目结构: 以下是基于您的需求和背景,设计的完整开发、同步、编译、调试和发布流程,遵循项目最佳实践,重点放在 Mac 开发 和 Windows 编译调试发布 环节,涵盖 Git 管理、多架构支持(32/64 位 DLL 和 EXE)、VS Code 配置以及最终的发布准备。 1. 项目结构设计 推荐标准化项目结构,便于跨平台开发和版本管理。 first_c/ ├── .vscode/ # VS Code 配置(仅在 Windows 环境下使用) │ ├── c_cpp_properties.json │ ├── launch.json │ ├── tasks.json ├── include/ # 头文件目录 │ └── mylib.h ├── src/ # 源文件目录 │ ├── mylib.cpp # DLL 源文件 │ ├── test_main.cpp # 测试用主函数 ├── build/ # 编译输出目录(仅在 Windows 中生成) ├── CMakeLists.txt # CMake 配置(跨平台构建) ├── README.md # 项目说明文件 └── .gitignore # Git 忽略文件 2. 在 Mac 上开发代码 (1) 安装开发工具 1. 安装 Homebrew 和开发工具链: brew install cmake gcc 2. 选择编辑器: 使用 VS Code 或 CLion 作为主要开发工具。 3. 安装 VS Code 插件: • C/C++(由 Microsoft 提供) • CMake Tools(用于构建管理) • GitLens(版本控制) (2) 编写代码 • mylib.cpp(DLL 的实现): #include "mylib.h" #include void hello() { std::cout << "Hello from DLL!" << std::endl; } • mylib.h(DLL 的头文件): #ifndef MYLIB_H #define MYLIB_H #ifdef _WIN32 #define DLL_EXPORT __declspec(dllexport) #else #define DLL_EXPORT #endif extern "C" DLL_EXPORT void hello(); #endif // MYLIB_H • test_main.cpp(测试程序): #include "mylib.h" int main() { hello(); // 调用 DLL 中的函数 return 0; } (3) 配置 CMake 在根目录创建 CMakeLists.txt: cmake_minimum_required(VERSION 3.10) project(MyProject) # 编译选项 set(CMAKE_CXX_STANDARD 17) # 包含目录 include_directories(${PROJECT_SOURCE_DIR}/include) # 添加目标:动态库 add_library(mylib SHARED src/mylib.cpp) # 添加目标:测试程序 add_executable(test_main src/test_main.cpp) target_link_libraries(test_main mylib) (4) 本地测试 1. 创建构建目录: mkdir build && cd build 2. 运行 CMake: cmake .. 3. 构建并运行: cmake --build . && ./test_main 确认 DLL 功能正确后,通过 Git 提交代码。 3. 使用 Git 管理版本 (1) 初始化 Git 仓库 1. 在项目根目录初始化 Git: git init git remote add origin 2. 创建 .gitignore,忽略不必要的文件: /build/ *.exe *.dll 3. 提交代码: git add . git commit -m "Initial commit" git push -u origin main (2) 在 Windows 上克隆项目 在 Windows 中,使用 Git 克隆代码: git clone cd project 4. 在 Windows 上编译和调试 (1) 安装开发环境 1. 安装 MinGW: • 下载并安装 MinGW-w64。 • 确保 bin 目录(如 C:/mingw64/bin)添加到系统环境变量 PATH。 2. 安装 VS Code: • 下载并安装 VS Code。 • 安装以下插件: • C/C++(由 Microsoft 提供) • CMake Tools • Code Runner(可选,用于快速运行) 3. 验证工具链: 打开终端,确认 GCC 和 GDB 安装成功: gcc --version g++ --version gdb --version (2) 配置 VS Code 在项目根目录的 .vscode 文件夹中创建以下配置文件: 1. c_cpp_properties.json { "configurations": [ { "name": "Win64", "includePath": [ "${workspaceFolder}/include" ], "defines": [ "_DEBUG", "UNICODE", "_UNICODE" ], "compilerPath": "C:/mingw64/bin/g++.exe", "cStandard": "c17", "cppStandard": "c++17", "intelliSenseMode": "gcc-x64" } ], "version": 4 } 2. tasks.json 定义编译任务: { "version": "2.0.0", "tasks": [ { "label": "build", "command": "g++", "args": [ "-shared", "-o", "build/mylib.dll", "src/mylib.cpp", "-Iinclude" ], "group": { "kind": "build", "isDefault": true } }, { "label": "build-test", "command": "g++", "args": [ "-g", "-o", "build/test_main.exe", "src/test_main.cpp", "-Iinclude", "-Lbuild", "-lmylib" ], "group": "build" } ] } 3. launch.json 定义调试任务: { "version": "0.2.0", "configurations": [ { "name": "Debug Test Program", "type": "cppdbg", "request": "launch", "program": "${workspaceFolder}/build/test_main.exe", "args": [], "stopAtEntry": false, "cwd": "${workspaceFolder}", "environment": [], "externalConsole": true, "MIMode": "gdb", "miDebuggerPath": "C:/mingw64/bin/gdb.exe", "setupCommands": [ { "description": "Enable pretty-printing for gdb", "text": "-enable-pretty-printing", "ignoreFailures": true } ], "preLaunchTask": "build-test" } ] } (3) 执行编译和调试 1. 编译 DLL 和测试程序: • 打开 VS Code,按下 Ctrl+Shift+B,执行默认的 build 任务。 • 运行 build-test 任务生成测试程序。 2. 调试测试程序: • 按下 F5,启动调试。 3. 测试和验证: • 运行测试程序,确保 DLL 和 EXE 工作正常。 5. 发布准备 (1) 生成多架构 DLL 在 MinGW 中分别生成 32 位和 64 位版本: • 32 位: g++ -shared -o build/mylib32.dll -m32 src/mylib.cpp -Iinclude • 64 位: g++ -shared -o build/mylib64.dll -m64 src/mylib.cpp -Iinclude (2) 测试目标系统 在虚拟机或实际设备上测试: • Windows 7 和 Windows 10 • 32 位和 64 位系统 总结 1. 开发: • 在 Mac 上使用 CMake 开发和初步测试代码。 • 确保代码跨平台。 2. 版本管理: • 使用 Git 管理代码,通过仓库同步到 Windows。 3. 编译调试: • 在 Windows 上通过 VS Code + MinGW 编译和调试 DLL 和 EXE。 4. 发布: • 确保多架构支持和多系统兼容性。 如果有更复杂的需求或具体问题,随时沟通!