unusual linking time time while using cmake ExternalProject

  Kiến thức lập trình

I’m trying to build a simple CPP project based on Cmake.
My project has a dependency library(Spdlog).
I like to have my dependency library beside my source files locally so that every time I build my Application I can compile my dependency too.
In this way, I could upgrade my library version whenever I want and am not dependent on its repository when I have no internet access.Also, another positive point of this approach is I don’t have to waste my time by compiling the library every time I clean the project.Using this way dependency library will be compiled just once unless I remove the build directory.

this is my simple project structure:

    ├── CMakeLists.txt
├── inc/
│   ├── main.h
├── lib/
│   ├── spdlog/
│   │   ├── CMakeLists.txt
│   │   ├── include/
│   │   │   └── spdlog/
│   │   │       ├── spdlog.h
│   │   │       ├── ...
│   │   ├── src/
│   │   │   ├── ...
│   │   ├── tests/
│   │   │   ├── ...
│   │   ├── CMakeLists.txt
│   │   ├── ...
├── src/
│   ├── main.cpp
└── build/

my main.cpp

#include <iostream>
#include <spdlog/spdlog.h>

int main()
{
    spdlog::info("Hello , World!");

    return 0;
}

and this is my Cmake build script:

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()

Spdlog will compile successfully in normal time and the Cmake copies its headers to the appropriate directory but it took too much time for the linker to link my application to the library and it repeated every time I built ( about 60 seconds on a normal laptop even when I make a simple change in main.ppIs there anything wrong with my Cmake script?

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT