0

I have the following structure:

${root}
    project_a
        main.c
        CMakeLists.txt
    project_b
        lib.c
        target.cmake

CMakeList.txt

include(${ROOT}/project_b/target.cmake)

add_executable(${PROJECT_NAME} main.c)
add_dependencies (${PROJECT_NAME} app_cmake_b)
target_link_libraries (${PROJECT_NAME} app_cmake_b)

target.cmake

add_library(project_b STATIC ${ROOT}/lib.c)

Is using include the only way to import other cmake projects?

I do not like that I need absolute paths in my included target.cmake What is the best pratice to solve my usecase with CMake?

Tarion
  • 16,283
  • 13
  • 71
  • 107
  • 1
    Possible duplicate of [CMake: How to setup Source, Library and CMakeLists.txt dependencies?](https://stackoverflow.com/questions/31512485/cmake-how-to-setup-source-library-and-cmakelists-txt-dependencies) – Florian Jun 14 '17 at 08:15

2 Answers2

1

You should use the add_subdirectory function.

https://cmake.org/cmake/help/v3.0/command/add_subdirectory.html

You can add directories that are not subdirectories by using a relative path.

For example, if you want to add a sibling directory,

 add_subdirectory(../dir)

Simply make sure that the directory you place in the function has a CMakeLists.txt, and that will import that project into the current one.

snoopy
  • 328
  • 1
  • 12
  • I would but it's no subdirectory ... It does not work. – Tarion Jun 14 '17 at 19:15
  • @Tarion I edited to explain that you can add a directory that is not a subdirectory. – snoopy Jun 15 '17 at 01:08
  • Thanks for the hin. I like to have absolute paths for some reason. But found that this is working well: `add_subdirectory(${ROOT}/project_b ${ROOT}/pkg/project_b)`. – Tarion Jun 15 '17 at 09:57
1

Also, you can use ExternalProject module https://cmake.org/cmake/help/v3.7/module/ExternalProject.html

ExternalProject_Add(project_b SOURCE_DIR "path" BINARY_DIR "path" ...)
vatosarmat
  • 1,090
  • 10
  • 22