В моем проекте есть библиотека зависимостей (Spdlog).
Мне нравится, чтобы моя библиотека зависимостей располагалась рядом с моими исходными файлами локально, чтобы каждый Когда я создаю свое приложение, я также могу скомпилировать свою зависимость.
Таким образом, я могу обновить версию своей библиотеки, когда захочу, и не зависеть от ее репозитория, когда у меня нет доступа в Интернет. Кроме того, еще один положительный момент в этом Подход заключается в том, что мне не нужно тратить время на компиляцию библиотеки каждый раз, когда я очищаю проект. При таком подходе библиотека зависимостей будет скомпилирована только один раз, если я не удалю каталог сборки.
Другими словами, мне нравится иметь все зависимости локально в моем проекте, хотя я не люблю компилировать все зависимости после каждой очистки.
это моя простая структура проекта:
Код: Выделить всё
├── CMakeLists.txt
├── inc/
│ ├── main.h
├── lib/
│ ├── spdlog/
│ │ ├── CMakeLists.txt
│ │ ├── include/
│ │ │ └── spdlog/
│ │ │ ├── spdlog.h
│ │ │ ├── ...
│ │ ├── src/
│ │ │ ├── ...
│ │ ├── tests/
│ │ │ ├── ...
│ │ ├── CMakeLists.txt
│ │ ├── ...
├── src/
│ ├── main.cpp
└── build/
Код: Выделить всё
#include
#include
int main()
{
spdlog::info("Hello , World!");
return 0;
}
Код: Выделить всё
cmake_minimum_required(VERSION 3.10)
project(MyProjec)
set(CMAKE_VERBOSE_MAKEFILE ON)
# Add ExternalProject_Add for spdlog
include(ExternalProject)
ExternalProject_Add(spdlog_external
PREFIX ${CMAKE_BINARY_DIR}/spdlog
SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/lib/spdlog
BINARY_DIR ${CMAKE_BINARY_DIR}/spdlog_build
INSTALL_COMMAND ""
CMAKE_ARGS -DSPDLOG_BUILD_SHARED=ON # Adjust options as needed
-DSPDLOG_BUILD_STATIC=OFF
-DCMAKE_INSTALL_PREFIX=${CMAKE_BINARY_DIR}/spdlog_install
BUILD_COMMAND ${CMAKE_COMMAND} --build . --target install && ${CMAKE_COMMAND} -E copy_directory ${CMAKE_CURRENT_SOURCE_DIR}/lib/spdlog/include ${CMAKE_BINARY_DIR}/spdlog_build/include
)
# Set the include directory for the main project
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/inc)
include_directories(${CMAKE_BINARY_DIR}/spdlog_build/include)
# Set the source files for the main project
set(SOURCES
src/main.cpp
)
# Set the header files for the main project
set(HEADERS
inc/main.h
)
# Create the executable for the main project
add_executable(MyProjec ${SOURCES} ${HEADERS})
# Specify the include directories for the executable
target_include_directories(MyProjec PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/inc ${CMAKE_BINARY_DIR}/spdlog_build/include)
# Ensure spdlog is built before building the main project
add_dependencies(MyProjec spdlog_external)
# Get the spdlog library path
ExternalProject_Get_Property(spdlog_external BINARY_DIR)
set(SPDLOG_LIB_DIR ${BINARY_DIR})
set(SPDLOG_INCLUDE_DIR ${BINARY_DIR}/include)
# Link the spdlog library to the main project
if (UNIX)
target_link_libraries(MyProjec PRIVATE ${SPDLOG_LIB_DIR}/libspdlog.so)
else()
target_link_libraries(MyProjec PRIVATE ${SPDLOG_LIB_DIR}/spdlog.dll)
endif()
Подробнее здесь: https://stackoverflow.com/questions/787 ... nalproject
Мобильная версия