How to compile dependencies using cmake?

Given that I have the following file structure,

my_project
    CMakeLists.txt
    /include
        ...headers
    /src
        ...source files
    /vendor
        /third-party-project1
            CMakeLists.txt
        /third-party-project2
            CMakeLists.txt

How do I use the third party projects by compiling them according to their CMakeLists.txt files from my own CMake file? I specifically need their include dirs, and potentially any libs they have.

Furthermore, how could I use FIND_PACKAGE to test if the third party projects are installed in the system, and if they don't compile and link against the included ones?

Upvotes: 3

Views: 6949

Answers (1)

zaufi
zaufi

Reputation: 7119

To find a project you may use find_package() command. Hope your third party projects provide a corresponding finder module, otherwise you ought to write it. Then you may analyze a result of find_package() in your CMakeLists.txt and conditionally use add_subdirectory() to dive into third-party-projectN/ -- this will build that libs as a part of your package. To use their headers just add (conditionally) include_directories(${PROJECT_SOURCE_DIR}/third-party-projectN/include). Their libraries will be accessible from your CMakeLists.txt as "ordinal" targets.

If that packages already installed in your system, a finder module should provide you some variables to use for include_directories() and/or target_link_libraries().

Upvotes: 1

Related Questions