在 Linux 下用 C++ 管理依赖,常见做法和工具可以分为 系统级包管理、现代构建系统、包管理器(跨平台/项目级)、容器化 几类。下面按“从传统到现代”给你一个清晰地图。
sudo apt install libboost-dev libssl-dev
sudo yum install boost-devel openssl-devel
# 或
sudo dnf install boost-devel openssl-devel
✅ 优点
find_package(Boost REQUIRED COMPONENTS filesystem)
target_link_libraries(app Boost::filesystem)
include(FetchContent)
FetchContent_Declare(
fmt
GIT_REPOSITORY https://github.com/fmtlib/fmt.git
GIT_TAG 10.1.1
)
FetchContent_MakeAvailable(fmt)
✅ 优点
git clone https://github.com/microsoft/vcpkg
./vcpkg/bootstrap-vcpkg.sh
./vcpkg/vcpkg install fmt boost
CMake 集成:
set(CMAKE_TOOLCHAIN_FILE ~/vcpkg/scripts/buildsystems/vcpkg.cmake)
✅ 优点
pip install conan
conan install . --build=missing
conanfile.txt:
[requires]
fmt/10.1.1
boost/1.82.0
[generators]
CMakeDeps
CMakeToolchain
✅ 优点
FROM ubuntu:22.04
RUN apt update && apt install -y \
g++ cmake libboost-dev
COPY . /app
WORKDIR /app
RUN cmake . && make
✅ 适合
| 场景 | 推荐方案 |
|---|---|
| 小工具 | apt + CMake |
| 中型项目 | CMake + vcpkg |
| 大型 / 多团队 | Conan |
| 需要可复现 | Docker + Conan/vcpkg |
如果你愿意,可以告诉我:
我可以直接给你一套可复制的目录结构和配置模板。