【问题标题】:Cmake include directoriesCmake 包含目录
【发布时间】:2016-03-31 07:42:08
【问题描述】:

我有一个这样的文件夹结构:Project/Libraries/Math、Project/Libraries/Math2。

在项目文件夹中,我有 main.cpp,以及具有以下内容的 CMakeLists.txt:

cmake_minimum_required (VERSION 2.6)
project (CppMain)
add_executable(CppMain main.cpp)

include_directories(${CMAKE_CURRENT_SOURCE_DIR))

在 Math 文件夹中我有标题 MyVectors.h,在 Math2 文件夹中我有 MyMatrices.h,我想将其包含在 main.cpp 文件中,其工作原理如下:

#include "Libraries/Math/MyVectors.h"
#include "Libraries/Math2/MyMatrices.h"

问题是标题 MyVectors.h 也以相同的方式包含标题 MyMatrices.h,但链接器没有找到它。我可以在 CMakeLists 中修改什么来解决这个问题?

【问题讨论】:

  • MyVector.h 和 MyMatrices.h 是否放在同一个文件夹中?
  • 抱歉,我编辑了这个问题。它们位于两个不同的文件夹中。

标签: c++ cmake makefile


【解决方案1】:

这似乎是相对路径的经典案例。你这样做是为了将文件包含在main.cpp

#include "Libraries/Math/MyVectors.h"
#include "Libraries/Math2/MyMatrices.h"

为了让文件访问父目录,您可以使用../ 应将以下几行引入MyVectors.h

#include "../Math2/MyMatrices.h"

这意味着什么?那么您的文件MyVectors.h 位于Math 文件夹中,当您使用../ 时,它会将您带到Math 的父目录Libraries。从那里您可以简单地按照所需目录的路径。

我可以在另一个问题上找到有关此答案的更多详细信息:https://stackoverflow.com/a/35910234/2555668

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-28
    • 1970-01-01
    相关资源
    最近更新 更多