# Add include files for Github ExternalProject library

**URL:** <https://discourse.cmake.org/t/add-include-files-for-github-externalproject-library/529>\
**Category:** Code\
**Created:** [January 20, 2020, 7:31pm UTC](https://discourse.cmake.org/t/add-include-files-for-github-externalproject-library/529 "2020-01-20T19:31:10Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![prex](https://discourse.cmake.org/user_avatar/discourse.cmake.org/prex/32/306_2.png) [@prex](https://discourse.cmake.org/u/prex)\
**Post date:** [January 20, 2020, 7:31pm UTC](https://discourse.cmake.org/t/add-include-files-for-github-externalproject-library/529/1 "2020-01-20T19:31:10Z")

</div>

I’m running out of ideas how to fix this. I have a CMakeList used by the catkin\_tools (ROS) build system. Furthermore, I’m using catkin\_simple, which defines some macros. However, the problem I have is more related to the ExternalProjects\_Add function, which I use to get my files from Github to add them as a library.

Having local files in a source folder with an include folder, everything went as expected. When I started downloading them directly from Github, I had the problem that the headers can not be found, because they are not in the source include folder as before. I added `cs_export(INCLUDE_DIRS ${SOURCE_DIR}/AdsLib)` which now tries to find the folder before the folders are even cloned.

This is my file which does not work:

```
cmake_minimum_required(VERSION 2.8.3)
project(ads_catkin)

find_package(catkin_simple REQUIRED)
catkin_simple()

include(ExternalProject)

ExternalProject_Add(ads
    PREFIX ${CATKIN_DEVEL_PREFIX}/ads
    GIT_REPOSITORY https://github.com/Beckhoff/ADS.git
    CONFIGURE_COMMAND ""
    UPDATE_COMMAND ""
    BUILD_COMMAND ""
    INSTALL_COMMAND ""
    BUILD_BYPRODUCTS 
        <SOURCE_DIR>/AdsLib/AdsDef.cpp
        <SOURCE_DIR>/AdsLib/AdsLib.cpp
        ...
        <SOURCE_DIR>/AdsLib/AdsDef.h
        <SOURCE_DIR>/AdsLib/AdsLib.h
        ...
)

ExternalProject_Get_Property(ads SOURCE_DIR)

include_directories(
    ${SOURCE_DIR}/AdsLib
)

cs_add_library(AdsLib
    ${SOURCE_DIR}/AdsLib/AdsDef.cpp
    ${SOURCE_DIR}/AdsLib/AdsLib.cpp
    ...
)
add_dependencies(AdsLib ads)

cs_install()

install(DIRECTORY ${SOURCE_DIR}/AdsLib/
        DESTINATION ${CATKIN_GLOBAL_INCLUDE_DESTINATION}
        FILES_MATCHING PATTERN "*.h" PATTERN "*.hpp"  
)

cs_export(
    INCLUDE_DIRS ${SOURCE_DIR}/AdsLib
)

```

As a comparison, here is a file that works perfectly fine with local files:

```
cmake_minimum_required(VERSION 2.8.3)

project(ads_twincat)

find_package(catkin_simple REQUIRED)
catkin_simple()

add_definitions(-std=c++11 -Wall -Wextra)

cs_add_library(${PROJECT_NAME}
    src/AdsDef.cpp
    src/AdsLib.cpp
    ...
)

cs_install()
cs_export()
```

---

<div class="post-metadata">

**Author:** ![robert.maynard](https://discourse.cmake.org/user_avatar/discourse.cmake.org/robert.maynard/32/4_2.png) [@robert.maynard](https://discourse.cmake.org/u/robert.maynard)\
**Post date:** [January 21, 2020, 3:05pm UTC](https://discourse.cmake.org/t/add-include-files-for-github-externalproject-library/529/2 "2020-01-21T15:05:34Z")

</div>

`ExternalProject` wouldn’t be the tool I would use to solve this problem, since `ExternalProject` does all the operations such as `git clone` at build time.

Instead what you should use is [`FetchContent`](https://cmake.org/cmake/help/v3.11/module/FetchContent.html) as it is designed to fetch the repository at configure time, and therefore everything exists for subsequent calls such as `export`

---

<div class="post-metadata">

**Author:** ![prex](https://discourse.cmake.org/user_avatar/discourse.cmake.org/prex/32/306_2.png) [@prex](https://discourse.cmake.org/u/prex)\
**Post date:** [January 21, 2020, 3:24pm UTC](https://discourse.cmake.org/t/add-include-files-for-github-externalproject-library/529/3 "2020-01-21T15:24:12Z")

</div>

The problem with FetchContent is that it’s not available for Cmake 3.10 and I can’t install this on the target devices.

---

<div class="post-metadata">

**Author:** ![robert.maynard](https://discourse.cmake.org/user_avatar/discourse.cmake.org/robert.maynard/32/4_2.png) [@robert.maynard](https://discourse.cmake.org/u/robert.maynard)\
**Post date:** [January 21, 2020, 3:34pm UTC](https://discourse.cmake.org/t/add-include-files-for-github-externalproject-library/529/4 "2020-01-21T15:34:08Z")

</div>

You could try to backport the FetchContent to CMake 3.10, or use the predecessor to it that @craig.scott wrote: [https://github.com/Crascit/DownloadProject](https://github.com/Crascit/DownloadProject)

---

<div class="post-metadata">

**Author:** ![prex](https://discourse.cmake.org/user_avatar/discourse.cmake.org/prex/32/306_2.png) [@prex](https://discourse.cmake.org/u/prex)\
**Post date:** [January 21, 2020, 3:44pm UTC](https://discourse.cmake.org/t/add-include-files-for-github-externalproject-library/529/5 "2020-01-21T15:44:39Z")

</div>

It’s more that this catkin wrapper should run out of the box without any further installations. It’s intended for large ROS projects which require the ADS library, but I do not want the user to install anything additional.

---

<div class="post-metadata">

**Author:** ![robert.maynard](https://discourse.cmake.org/user_avatar/discourse.cmake.org/robert.maynard/32/4_2.png) [@robert.maynard](https://discourse.cmake.org/u/robert.maynard)\
**Post date:** [January 21, 2020, 3:54pm UTC](https://discourse.cmake.org/t/add-include-files-for-github-externalproject-library/529/6 "2020-01-21T15:54:30Z")

</div>

> [@prex](#):
>
> this catkin wrapper should run out of the box without any further installations.

Yes, place a copy of FetchContent, or DownloadProject inside the project which you use.

---

<div class="post-metadata">

**Author:** ![prex](https://discourse.cmake.org/user_avatar/discourse.cmake.org/prex/32/306_2.png) [@prex](https://discourse.cmake.org/u/prex)\
**Post date:** [January 22, 2020, 1:21pm UTC](https://discourse.cmake.org/t/add-include-files-for-github-externalproject-library/529/7 "2020-01-22T13:21:02Z")

</div>

Alright, works like a charm with just leaving out the add\_subdirectory building step.

One remaining question: All the .cpp and .hpp files are in the same source folder and, therefore, included by `include <AdsLib.h>` etc. From these files a library is built. Does this now mean that I also need to put them on top level in the include folder, or can I create a sub-folder include/AdsLib?

---

<div class="post-metadata">

**Author:** ![robert.maynard](https://discourse.cmake.org/user_avatar/discourse.cmake.org/robert.maynard/32/4_2.png) [@robert.maynard](https://discourse.cmake.org/u/robert.maynard)\
**Post date:** [January 22, 2020, 4:12pm UTC](https://discourse.cmake.org/t/add-include-files-for-github-externalproject-library/529/8 "2020-01-22T16:12:43Z")

</div>

You can create a subdirectory, but you will likely need to make sure you have a `target_include_directories` or `include_directories` statement for that new subdirectory.
