4

There is an GitHub repository that contains only hpp files with no CMakeList.txt file.

I need to include those header files in my project. I do not want to manually clone them then linking against them. I want that each time I call cmake command, cmake will clone them and put them in some inner folder.

Is it possible? if yes, how can I achive this?

2
  • 1
    I want that each time I call cmake command, cmake will clone them and put them in some inner folder. - As usual, ExternalProject_Add will handle the project's downloading. As you needn't configuration and futher steps, you may set appropriate COMMAND to empty strings. Commented Jun 2, 2017 at 7:43
  • Related: This question discusses how to do this for the Catch unit testing library, which is header-only. Commented Jun 2, 2017 at 11:19

3 Answers 3

2

You may want to consider the approach used in the DownloadProject github project which includes an example that does a download step for the GoogleTest repo. It invokes CMake's ExternalProject module during the configure stage rather than the build stage, so the external sources become available at configure time, which seems to be what you want here too. In your case, you don't need the subsequent step of using add_subdirectory() to pull anything from the external project into your main build, so just the download is enough. This approach is also referenced by this answer.

Using DownloadProject looks something like this (taken from the example provided as part of the github project):

include(DownloadProject.cmake)
download_project(PROJ           googletest
                 GIT_REPOSITORY https://github.com/google/googletest.git
                 GIT_TAG        master
)

The downloaded source would then be available under the directory stored in the variable ${googletest_SOURCE_DIR}. See the fully general implementation in the DownloadProject source for how it works. You can simply embed it in your own project's sources and re-use it to efficiently handle downloading any external dependencies you might need.

Sign up to request clarification or add additional context in comments.

Comments

1

I managed to do it like this:

file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/external_includes)
execute_process(
    COMMAND git clone "xxxxx.git" lib_name
    WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/external_includes)

include_directories(${CMAKE_CURRENT_BINARY_DIR}/external_includes)

This approach was tested on Windows with MSVS & Ubuntu with GCC.

Of course, using something like Hunter or any packages manger is preferable but sometimes fast and easy solution are needed.

1 Comment

This is going to try to do a git clone every time CMake is run, even if you've got it cloned already.
0

another option I've spotted at json cpp repo:

FetchContent_Declare(json
    GIT_REPOSITORY https://github.com/nlohmann/json.git
    GIT_TAG v3.7.3)

FetchContent_GetProperties(json)
if(NOT json_POPULATED)
    FetchContent_Populate(json)
    add_subdirectory(${json_SOURCE_DIR} ${json_BINARY_DIR} EXCLUDE_FROM_ALL)
endif()

IMHO looks even better

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.