-1

How can I add a header-only library outside the directory?

Previously I was using add_subdirectory(HEADERLIBRARY) only when the directory HEADERLIBRARY was inside the project directory. However, now I want to use the library for multiple projects, so I made a directory like this:

OUT
|----HEADERLIBRARY // of course contains CMakeLists.txt
|
|----project1
|----project2
|...

Is there a way to get the same effect as when I use add_subdirectory?

EDIT : HEADERLIBRARY = https://github.com/taocpp/PEGTL/blob/master/doc/Installing-and-Using.md#add_subdirectory

user5876164
  • 471
  • 3
  • 15
  • If the header-only library exists outside of your source tree, you probably want the CMake `find_package` command. Here are the [docs](https://cmake.org/cmake/help/latest/command/find_package.html). – Kevin May 11 '19 at 15:10
  • I added `set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR}/../HEADERLIBRARY)` `find_package(headerlibrary)` and still it doesn't work. What am I missing, or am I doing something wrong? @squareskittles – user5876164 May 11 '19 at 15:30
  • 1
    See this [answer](https://stackoverflow.com/a/20857070/3987854) for some notes about how to use `find_package`. Depending on the setup of this header-only library, you'll want to approach `find_package` in one of two ways. – Kevin May 11 '19 at 16:00
  • Sorry, but I really don't get it. I have never used CMake in such a way(I only used it with CLion) and it seems that I should spend plenty of time for this. I added some info about `HEADERLIBRARY`. Would that help for me to get the answer? @squareskittles – user5876164 May 11 '19 at 16:42

1 Answers1

1

Yes, you still just use add_subdirectory(source_dir [binary_dir] [EXCLUDE_FROM_ALL]). Because source_dir is not within the source directory tree of the top-level project you have to specify the [binary_dir] folder that is going to be used. By default binary_dir is the same as source_dir within the binary directory tree and it is handled automatically.

fdk1342
  • 3,274
  • 1
  • 16
  • 17