如何避免包含文件夹中的相对路径

时间:2018-07-05 00:44:34

标签: android cmake android-ndk

在Android Studio中,我的目录结构如下:

App
├── CMakeLists.txt
└── src
    ├── foo
    │   ├── CMakeLists.txt
    │   ├── foo.cpp
    │   └── foo.h
    ├── main
    │   └── cpp
    │       ├── CMakeLists.txt
    │       └── main.cpp
    └── test
        ├── CMakeLists.txt
        └── testDriver.cpp

在main.cpp中,我想#include "foo.h"甚至是#include "fooLib/foo.h",但是除非我#include "../../fooLib/foo.h",否则它不会编译。我试图在android studio中配置CMake,以允许我使用前者。我尝试导出,target_include_dirs,但有些东西我只是没有得到。

我希望能够从任何地方引用“ fooLib / foo”。

2 个答案:

答案 0 :(得分:0)

App/CMakeLists.txt

# set the root directory as ${CMAKE_CURRENT_SOURCE_DIR} which is a
# CMAKE build-in function to return the current dir where your CMakeLists.txt is. 
# Specifically, it is "<your-path>/App/"
set(APP_ROOT_DIR ${CMAKE_CURRENT_SOURCE_DIR})

# set your 3 other root dirs, i.e. foo, main and test under app/src.
set(APP_ROOT_SRC_DIR ${APP_ROOT_DIR}/src)
set(APP_ROOT_FOO_DIR ${APP_ROOT_SRC_DIR}/foo)
set(APP_ROOT_MAIN_DIR ${APP_ROOT_SRC_DIR}/main)
set(APP_ROOT_TEST_DIR ${APP_ROOT_SRC_DIR}/test)

# set your include paths into "SHARED_INCLUDES" variable.
set(SHARED_INCLUDES
                ${APP_ROOT_FOO_DIR}
                # ${APP_ROOT_FOO_DIR}/<your-other-child-dirs>

                ${APP_ROOT_MAIN_DIR}
                ${APP_ROOT_MAIN_DIR}/cpp
                # ${APP_ROOT_MAIN_DIR}/<your-other-child-dirs>

                ${APP_ROOT_TEST_DIR}
                # ${APP_ROOT_TEST_DIR}/<your-other-child-dirs>
                )

# This function will have effect to all the downstream cmakelist files. 
include_directories(${SHARED_INCLUDES})


# remember to include downstream cmakelist files for foo, main and test.
add_subdirectory(${APP_ROOT_FOO_DIR} bin-dir)
add_subdirectory(${APP_ROOT_MAIN_DIR} bin-dir)
add_subdirectory(${APP_ROOT_TEST_DIR} bin-dir)

现在,您可以在任何地方使用#include "foo.h"而不用引用其相对路径。

答案 1 :(得分:-1)

解决方法是将include_directories(src)放在App/CMakeLists.txt