CMake Error "include could not find requested file" when nest a third-party cmake project into my own

Viewed 82

Following is my CMakeLists.txt structure

|-- MyProject
    |-- CMakeLists.txt
    |-- README.md
    |-- bin
    |-- include
    |-- lib
    |-- src
    |   |-- CMakeLists.txt
    |   |-- main.cc
    |   |-- parser
    |       |-- CMakeLists.txt
    |       |-- parser.cc
    |       |-- parser.h
    |-- third-party
        |-- CMakeLists.txt
        |-- cassandra-cpp-driver

Now I want to introduce a yugabyteDB-cpp-driver https://github.com/yugabyte/cassandra-cpp-driver/tree/2.9.0-yb to use its API, so I add those commands into my root CMakeLists.txt

set(THIRD_PARTY_DIR ${PROJECT_SOURCE_DIR}/third-party)

add_subdirectory(${THIRD_PARTY_DIR})

Then I add these to third-party/CMakeLists.txt

set(CQL_DRIVER_LIB_NAME "cassandra-cpp-driver")
set(CQL_DRIVER_INC_PATH ${CQL_DRIVER_LIB_NAME}/include)

add_subdirectory(${CQL_DRIVER_LIB_NAME})

After these, when I trying to do build the project, I got

CMake Error at third-party/cassandra-cpp-driver/CMakeLists.txt:10 (include):
  include could not find requested file:

    CppDriver


CMake Error at third-party/cassandra-cpp-driver/CMakeLists.txt:12 (CassInitProject):
  Unknown CMake command "CassInitProject".

Seems the cpp-driver it self has a include() command which calls the .cmake files the git contains, but I can't trigger it in my project.

1 Answers

Note: Below I describe a solution to the specific problem occuring, but this is in no way a complete solution to allow you to include the project the way you intend to. This is unlikely to be possible without modifying the third party cmake logic. The cmake logic just doesn't seem to be designed for your intended use case.


CMake Error at third-party/cassandra-cpp-driver/CMakeLists.txt:10 (include):
  include could not find requested file:

    CppDriver

The relevant lines in the CMakeLists.txt file in the repository linked are

set(CASS_ROOT_DIR ${CMAKE_SOURCE_DIR})
...
list(APPEND CMAKE_MODULE_PATH ${CASS_ROOT_DIR}/cmake/modules)

include(CppDriver)

The first line is supposed to make cmake able to the CppDriver.cmake module when using the include() command.

CMAKE_SOURCE_DIR refers to the directory containg the toplevel CMakeLists.txt though, which is your own directory. To be able to include the project via add_subdirectory, the relevant line should read

set(CASS_ROOT_DIR ${CMAKE_CURRENT_SOURCE_DIR})

(Note: In no way this is a complete analysis of the cmake logic. There may be different parts that would require similar adjustments.)

You could fix this specific error by simply adding the correct path before using add_subdirectory():

set(CQL_DRIVER_LIB_NAME "cassandra-cpp-driver")
set(CQL_DRIVER_INC_PATH ${CQL_DRIVER_LIB_NAME}/include)

list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/${CQL_DRIVER_LIB_NAME}/cmake/modules)

add_subdirectory(${CQL_DRIVER_LIB_NAME})

But you'd just run into a similar issue in a different place in the third party cmake logic.

Related