Cmake包含目录

Cmake include directories

本文关键字:包含目 Cmake      更新时间:2023-10-16

我有一个这样的文件夹结构:Project/LLibraries/Math,Project/LLibrarys/Math2。

在Project文件夹中,我有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文件夹中我有MyMatrix.h,我想将其包含在main.cpp文件中,其工作方式为:

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

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

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

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

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

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

这意味着什么?当你使用../时,你的文件MyVectors.hMath文件夹中,它会把你带到Math的父目录,即Libraries。从那里你可以简单地按照路径到你需要的目录。

关于另一个问题,我可以在这个答案中找到更多细节:https://stackoverflow.com/a/35910234/2555668