depend_pkg (git curl wget)
depend_pkg (git curl wget)
直接保留依赖库的文件夹/压缩包/使用git安装依赖
Demo
这里以 CppCmakeDemo 的pkg方式为例
里面有的依赖需要 git,有的需要 wget/curl
有的需要安装后再编译,而有的不需要编译直接可用
cpp-httplib
https://github.com/LincZero/cpp-httplib fork https://github.com/yhirose/cpp-httplib
(1) 纯头文件版本
这里无需git clone,该库为纯头文件库,只要用到一个文件,下载就好。以下命令四选一:
(Linux和Windows都能使用wget或curl,但windows默认没有wget只有curl,所以一般是优先使用curl。)
- Linux
mkdir libs && wget -O ./libs/httplib.h https://raw.githubusercontent.com/LincZero/cpp-httplib/master/benchmark/cpp-httplib-base/httplib.h
mkdir libs && curl -o ./libs/httplib.h https://raw.githubusercontent.com/LincZero/cpp-httplib/master/benchmark/cpp-httplib-base/httplib.h
- Windows
mkdir libs; wget -O ./libs/httplib.h https://raw.githubusercontent.com/LincZero/cpp-httplib/master/benchmark/cpp-httplib-base/httplib.h
mkdir libs; curl -o ./libs/httplib.h https://raw.githubusercontent.com/LincZero/cpp-httplib/master/benchmark/cpp-httplib-base/httplib.h
CMakeList.txt
include_directories(${CMAKE_SOURCE_DIR}/libs)
(2) clone版本 (废弃)
这个就需要clone整个库了,他那个 FindBrotli.cmake
和 httplibConfig.cmake.in
文件在repo/cmake文件夹里
Linux
cd libs
git clone --depth 1 https://github.com/yhirose/cpp-httplib.git
CMakeList.txt
find_package(httplib CONFIG REQUIRED PATHS "${PROJECT_SOURCE_DIR}/libs/cpp-httplib/")
target_link_libraries(${PROJECT_NAME} PRIVATE httplib::httplib)
spdlog
https://github.com/gabime/spdlog
(1) Linux
cd libs
git clone --depth 1 https://github.com/gabime/spdlog.git
cd spdlog && mkdir build && cd build
cmake .. && cmake --build .
(2) Windows
cd libs
git clone --depth 1 https://github.com/gabime/spdlog.git
cd spdlog; mkdir build; cd build
cmake ..; cmake --build .
(3) 然后搜包路径改一下
# 这个是原来用vcpkg的写法
# find_package(spdlog CONFIG REQUIRED)
# 修改成直接指定搜包路径
find_package(spdlog CONFIG REQUIRED PATHS "${PROJECT_SOURCE_DIR}/libs/spdlog/build/")
# 或者添加该语句 (整体添加搜包路径)
list(APPEND CMAKE_PREFIX_PATH "${PROJECT_SOURCE_DIR}/libs/spdlog/build/")
# 或者添加该语句 (整体设置搜包路径)
set(CMAKE_PREFIX_PATH "${PROJECT_SOURCE_DIR}/libs/spdlog/build/")
spdlog - head
或者直接用头文件版本,无需编译
cd libs
git clone --depth 1 https://github.com/gabime/spdlog.git
然后直接包含就好
add_subdirectory(libs/spdlog) # 添加头文件库的 CMake 集成
include_directories(${CMAKE_SOURCE_DIR}/libs/spdlog/include/)
nlohmann/json
cd libs
git clone --depth 1 https://github.com/nlohmann/json.git
cd json && mkdir build && cd build
cmake .. && cmake --build .
可以发现include文件夹里的是 .hpp
,所以可以用头文件方式使用
# 注意,用纯头文件版本时,要用后者。前者是要编译再使用的
include_directories(${CMAKE_SOURCE_DIR}/libs/json/include/)
nlohmann/json - head
或者直接用纯头文件版本,无需编译
cd libs
git clone --depth 1 https://github.com/nlohmann/json.git
然后直接包含就好
add_subdirectory(libs/json) # 添加头文件库的 CMake 集成
include_directories(${CMAKE_SOURCE_DIR}/libs/json/single_include/)
补充
头文件包含时,可选指定目标:
include_directories(${CMAKE_SOURCE_DIR}/libs/)
target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_SOURCE_DIR}/libs/)
链接到当前文件 0
没有文件链接到当前文件