【问题标题】:Is it possible to use some sort of path for #include?是否可以为#include 使用某种路径?
【发布时间】:2016-07-10 10:54:26
【问题描述】:

我在文件中有一堆#include 语句:

#include "/Users/cooper/Desktop/MyLib/graph_api.h"
#include "/Users/cooper/Desktop/MyLib/mst.h"
#include "/Users/cooper/Desktop/MyLib/dfs.h"
#include "/Users/cooper/Desktop/MyLib/bfs.h"
#include "/Users/cooper/Desktop/MyLib/topo_sort.h"
#include "/Users/cooper/Desktop/MyLib/scc.h"
#include "/Users/cooper/Desktop/MyLib/bipartite.h"
#include "/Users/cooper/Desktop/MyLib/dijkstra.h"
#include "/Users/cooper/Desktop/MyLib/union_find.h"
#include "/Users/cooper/Desktop/MyLib/my_string.h"
#include "/Users/cooper/Desktop/MyLib/2d_array.h"

不过,它可能会在未来发生变化,我将不得不更新一些行。有没有可能有类似的东西

PATH = "/Users/cooper/Desktop/MyLib/
#include PATH + "2d_array.h"

?

【问题讨论】:

  • 它本身不是 makefile 的一部分。它是一个命令行选项。 Makefile 只是管理编译的一种机制
  • 看起来您使用的是 OSX 系统(根据路径判断),这意味着您可能正在使用 Xcode。在项目设置中必须有一个可以输入预处理器设置的地方,例如添加包含路径。如果你这样做,并添加/Users/cooper/Desktop/MyLib,那么你可以简单地做#include <2d_array.h>
  • this answer 应该对你有用。 (我的意思是写#define PATH "/Users/cooper/Desktop/MyLib/#include PATH "2d_array.h"
  • 我正在使用 XCode,对。

标签: c++


【解决方案1】:

通常,您的编译器会提供一个选项,您可以在其中添加从#include 语句中查找头文件的路径。

例如为 GCC 添加

-I/Users/cooper/Desktop/MyLib

到你的编译器命令行。


此选项也可用于在您的 IDE 项目设置中设置,或作为构建系统中的变量。

【讨论】:

    猜你喜欢
    • 2019-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-30
    • 1970-01-01
    • 2023-01-24
    • 1970-01-01
    相关资源
    最近更新 更多