Home > Software engineering >  How to set correct directory path in add_subdirectory function?
How to set correct directory path in add_subdirectory function?

Time:04-03

Using Android Studio and have two libraries, Lib(A) and Lib(B), there are two CMakeList.txt for each project.

I want to know how can I set add_subdirectory in Lib(A) because in run mode can not detect function in Lib(B), I guess the compiler/Gradle ignores CMakelist of Lib(B).

libraries structure:

 A
 |--src
    |--main
       |--JAVA
       |--CPP
          |-CMakelist.txt
          |-...CPP

and

 B
 |--src
    |--main
       |--JAVA
       |--CPP
          |-CMakelist.txt
          |-...CPP

Lib(A) CMakelist:

cmake_minimum_required(VERSION 3.18.1)
project("A")
add_subdirectory("src/main/cpp/")
add_library(A
        SHARED
        a.cpp)

find_library(log-lib
             log )
target_link_libraries(Services
                   ${log-lib} )

Lib(B) CMakelist:

cmake_minimum_required(VERSION 3.18.1)
project("B")
add_library(B
        SHARED
        b.cpp)

find_library(log-lib
             log )

I tested:

  add_subdirectory("src/main/cpp/")
  and
  add_subdirectory("com.myapp.B/src/main/cpp/")

but doesn't work, the error is:

  CMake Error at CMakeLists.txt:3 (add_subdirectory):
  add_subdirectory given source "src/main/cpp/" which is not an existing
  directory.

CodePudding user response:

The first parameter of add_subdirectory takes the path to the directory containing the CMakeLists.txt file to include. If this path is relative, it's resolved relative to the directory containing the CMakeLists.txt currently being parsed.

If you specify an absolute path or a relative path navigating to a parent directory, you need to specify the build directory to use as second argument.

add_subdirectory(../../../../B/src/main/CPP B_build)
  • Related