如何避免包含文件夹中的相对路径 [英] How to avoid relative paths in include folder

查看:228
本文介绍了如何避免包含文件夹中的相对路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Within Android Studio, I have a directory structure like so:

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,但是有些东西我只是没有得到.

In main.cpp, I would like to #include "foo.h" or even #include "fooLib/foo.h" but It won't compile unless I #include "../../fooLib/foo.h". I am trying to configure CMake within android studio to allow me to use the former. I tried export, target_include_dirs, but there is something i am just not getting.

我希望能够从任何地方引用"fooLib/foo".

I would like to be able to refer to "fooLib/foo" from anywhere.

推荐答案

内部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"而不引用其相对路径.

Now, you can use the #include "foo.h" anywhere without quoting its relative path.

这篇关于如何避免包含文件夹中的相对路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆