0

I have two CMake projects on macOS - the main project and an external library. Both projects are cloned on my machine. I want to add the external library as a dependency to the main project. I also want to make the main project build the external library. Is that possible with CMake?

P.S. All examples that I have found are downloading the external library first.

EDIT:

ExternalProject_Add(<lib_name>
                    SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/<rel_path_to_src>
                    BINARY_DIR ${CMAKE_CURRENT_SOURCE_DIR}/<rel_path_to_build_dir>
                    BUILD_ALWAYS ON)

This is my ExternalProject_Add. But I am not sure how to properly link the library so it can be built before the main project.

I tried to link as follows:

find_library( LIB <lib_name> HINTS ${CMAKE_CURRENT_SOURCE_DIR}/<rel_path_to_lib_dir>)
target_link_libraries( ${PROJECT_NAME} PRIVATE ${LIB} )

I receive the following error:

ninja: error: '<path_to_lib>/lib.a', needed by '<executable>', missing and no known rule to make it
  • 1
    Command [ExternalProject_Add](https://cmake.org/cmake/help/latest/module/ExternalProject.html#command:externalproject_add) allows you to build external project alongside with your one. See e.g. that question: https://stackoverflow.com/questions/15175318/cmake-how-to-build-external-projects-and-include-their-targets – Tsyvarev Jun 02 '21 at 07:57
  • @Tsyvarev thank you for the answer. But I still can't see how it can work with an already downloaded project. – Ivelin Krastev Jun 02 '21 at 08:31
  • 1
    Oh, so your problem is actually "How to use `ExternalProject` with already downloaded project?". Just pass `SOURCE_DIR` parameter which points to your downloaded library and do NOT specify download steps like `URL`. – Tsyvarev Jun 02 '21 at 08:34
  • It will be amazing if you can provide me an example code. Because I cannot configure it for some reason. I am probably doing something wrong. – Ivelin Krastev Jun 02 '21 at 09:14
  • You could add the code with your **attempt** into the question post (also add the error message which is caused by it). So we could be able to help you in fixing a problem. – Tsyvarev Jun 02 '21 at 09:40

0 Answers0