如何避免相对路径中包含文件夹(How to avoid relative paths in incl

2019-09-30 00:51发布

在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 /富”。

Answer 1:

内部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"任何地方,而不引用其相对路径。



Answer 2:

解决方法是地方include_directories(src)App/CMakeLists.txt



文章来源: How to avoid relative paths in include folder