# cppLearning **Repository Path**: judeli/cpp-learning ## Basic Information - **Project Name**: cppLearning - **Description**: 现代c++学习以及流行开源库测试项目 - **Primary Language**: C++ - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2026-02-03 - **Last Updated**: 2026-04-13 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # cppLearning - Multi-Module C++ Project This project demonstrates a well-structured, multi-module C++ application with vcpkg as the unified dependency management system. ## Project Structure ``` cppLearning/ ├── apps/ # Application modules │ ├── tcp-demo/ # TCP demo application │ ├── web-demo/ # Web server demo application │ └── websocket-demo/ # WebSocket demo application ├── libs/ # Library modules │ ├── common/ # Common utilities library │ └── common-web/ # Web-related utilities library ├── cmake/ # CMake modules and utilities │ ├── Dependencies.cmake # vcpkg-based dependency management │ └── Warnings.cmake # Compiler warning configuration ├── CMakeLists.txt # Main project configuration └── CMakePresets.json # Build presets configuration ``` ## Prerequisites This project uses [vcpkg](https://github.com/microsoft/vcpkg) as the unified dependency manager. Before building, ensure you have vcpkg installed and the required packages are available. ### Installing vcpkg ```bash git clone https://github.com/Microsoft/vcpkg.git cd vcpkg ./bootstrap-vcpkg.sh # On Linux/macOS # .\bootstrap-vcpkg.bat # On Windows ./vcpkg integrate install ``` ### Required Dependencies Install the following packages using vcpkg: ```bash # Install all required packages vcpkg install fmt asio jsoncpp libev # Optional package (if needed) vcpkg install log4cpp ``` ## Adding New Modules ### Adding a New Application Module To add a new application module to the `apps/` directory: 1. **Create the application directory structure**: ```bash mkdir apps/my-new-app cd apps/my-new-app mkdir src include ``` 2. **Create source files**: ```bash touch src/main.cpp touch apps/my-new-app/CMakeLists.txt ``` 3. **Create the CMakeLists.txt for your application**: ```cmake # apps/my-new-app/CMakeLists.txt cmake_minimum_required(VERSION 3.15) # Define executable add_executable(my-new-app src/main.cpp ) # Link against required libraries target_link_libraries(my-new-app common # Link against common library ${PROJECT_FMT_TARGET} ${PROJECT_ASIO_TARGET} # Add other required libraries from vcpkg ) # Set include directories target_include_directories(my-new-app PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include ) # Apply project warnings set_project_warnings(my-new-app) ``` 4. **Register the new application**: Add the new application to the `apps/CMakeLists.txt` file: ```cmake add_subdirectory(my-new-app) ``` ### Adding a New Library Module To add a new library module to the `libs/` directory: 1. **Create the library directory structure**: ```bash mkdir libs/my-new-lib cd libs/my-new-lib mkdir include/my-new-lib touch libs/my-new-lib/CMakeLists.txt ``` 2. **Create header files**: ```bash touch include/my-new-lib/MyClass.h touch src/MyClass.cpp # Optional, if you have implementation files ``` 3. **Create the CMakeLists.txt for your library**: ```cmake # libs/my-new-lib/CMakeLists.txt cmake_minimum_required(VERSION 3.15) # Define library add_library(my-new-lib include/my-new-lib/MyClass.h # Add source files here if any # src/MyClass.cpp ) # Set public include directories target_include_directories(my-new-lib PUBLIC $ $ ) # Link against required dependencies from vcpkg target_link_libraries(my-new-lib ${PROJECT_FMT_TARGET} # Add other required libraries from vcpkg ) # Apply project warnings set_project_warnings(my-new-lib) ``` 4. **Register the new library**: Add the new library to the `libs/CMakeLists.txt` file: ```cmake add_subdirectory(my-new-lib) ``` ## Available Dependencies (via vcpkg) The project provides the following dependencies via vcpkg through the `Dependencies.cmake` module: - **fmt**: Modern formatting library (`${PROJECT_FMT_TARGET}`) - **asio**: Cross-platform C++ networking library (`${PROJECT_ASIO_TARGET}`) - **jsoncpp**: JSON manipulation library (`${PROJECT_JSONCPP_TARGET}`) - **libev**: Event loop library (`${PROJECT_LIBEV_TARGET}`) - **log4cpp**: Logging library (optional, `${PROJECT_LOG4CPP_TARGET}`) ## Building the Project ### Using CMake Presets (Recommended) ```bash # Configure for debug build cmake --preset=debug # Build the project cmake --build --preset=debug # Or configure for release build cmake --preset=release cmake --build --preset=release ``` ### Manual Configuration ```bash mkdir build cd build cmake .. -DCMAKE_BUILD_TYPE=Debug make ``` ### With vcpkg Integration If you've installed vcpkg globally, you can also specify the toolchain: ```bash cmake .. -DCMAKE_TOOLCHAIN_FILE=[vcpkg_root_directory]/scripts/buildsystems/vcpkg.cmake ``` ## Build Options - `ENABLE_WARNINGS_AS_ERRORS`: Treat all warnings as errors (default: OFF) - `BUILD_SHARED_LIBS`: Build shared libraries instead of static (default: OFF) ## CMake Modules This project provides the following custom CMake modules: - `set_project_warnings(target)`: Applies appropriate compiler warnings to the specified target - Dependency management through `Dependencies.cmake` (vcpkg-centric) - Automatic handling of cross-platform differences ## Troubleshooting ### Missing Dependencies If CMake fails to find dependencies, ensure all required packages are installed via vcpkg: ```bash # Check installed packages vcpkg list # Install missing packages vcpkg install fmt asio jsoncpp libev ``` ### vcpkg Path Issues If CMake cannot locate vcpkg, you can set the VCPKG_ROOT environment variable: ```bash export VCPKG_ROOT="/path/to/your/vcpkg" # On Linux/macOS # set VCPKG_ROOT=C:\path\to\your\vcpkg # On Windows ``` ## Contributing When adding new modules, please follow the established patterns and ensure all external dependencies are managed through vcpkg. Update this README if significant changes are made to the project structure or dependency management.