简体   繁体   English

我说#include时,如何告诉预处理器在头文件的特定文件夹中搜索<xyz.h>

[英]How to tell the preprocessor to search for a particular folder for header files, when I say #include <xyz.h>

I have around 120 header files (.h files) , and in all of them each one includes many other header files using #include <abcd/xyz.h> , but as I kept .h files in a specific folder, preprocessor is generating filenotfound error. 我大约有120个头文件(.h文件),并且每个文件都使用#include <abcd/xyz.h>包含许多其他头文件,但是由于我将.h文件保存在特定的文件夹中,因此预处理程序正在生成filenotfound错误。

I moved all the .h files to the single .C file that is calling the first headerfile. 我将所有.h文件移动到了调用第一个头文件的单个.C文件中。

One way to do is make #include <abcd/xyz.h> as #include "abcd/xyz" , but I need to do this in all the header files wherever there is an include statement, and there are hundreds of them. 一种方法是将#include <abcd/xyz.h>#include "abcd/xyz" ,但是无论是否包含include语句,我都需要在所有头文件中执行此操作,并且其中有数百个。

I can't include many of them in the headerfiles section in Visualstudio because, some of the headerfiles have the same name, but they reside in different directories. 我不能在Visualstudio的头文件部分中包含许多头文件,因为某些头文件具有相同的名称,但它们位于不同的目录中。 ( <abcd/xyz.h> , <efgh/xyz.h> ). <abcd/xyz.h><efgh/xyz.h> )。

Any way to do this? 有什么办法吗?

You should add a path into "Additional include directories" in the "C++" section of the project options (the "General" tab). 您应该在项目选项(“常规”选项卡)的“ C ++”部分的“其他包含目录”中添加路径。 You can use environment variables as well as "this folder" (.) shortcut and "up one folder" (..) shortcut for this setting to not be bound to a certain directory structure. 您可以使用环境变量以及“此文件夹”(。)快捷方式和“上一个文件夹”(..)快捷方式来使此设置不绑定到特定目录结构。

and I can't include many of them in the headerfiles section in Visualstudio because , some of the headerfiles have the same name, but they reside in different directories.(,) 而且我不能在Visualstudio的头文件部分中包含许多头文件,因为,一些头文件具有相同的名称,但它们位于不同的目录中。(,)

That's a pretty big problem unless the files that are including those non-uniquely named headers are in the same directory as the header files themselves. 除非包含那些非唯一命名的头文件的文件与头文件本身位于同一目录中,否则这是一个很大的问题。

You have no way to guarantee that the compiler will locate one header before another without modifying the #include directive itself (and adding a relative path as one example). 您无法保证编译器在不修改#include指令本身的情况下(并添加一个相对路径作为示例)将一个标头定位在另一个标头之前。

EDIT : It looks like Visual Studio will allow you to specify different Additional Include Directories for each source file in a project (rt-click on the source file in Solution Explorer and modify C/C++ properties). 编辑 :看起来Visual Studio将允许您为项目中的每个源文件指定不同的“其他包含目录”(在解决方案资源管理器中,在源文件上单击鼠标右键,然后修改C / C ++属性)。 But I think this would be more work than modifying the #include directives themselves - depends on how many non-unique header filenames you have. 但是我认为,这比修改#include指令本身要耗费更多精力-取决于您拥有多少个非唯一标头文件名。

In the project settings (under C/C++ in VS2005/2008) there's an option for "additional include directories". 在项目设置中(在VS2005 / 2008中为C / C ++),有一个“其他包含目录”选项。 You can add the folders containing your header files here, using relative paths. 您可以使用相对路径在此处添加包含头文件的文件夹。

You can also do this at the IDE level in Tools -> Options -> Projects and Solutions -> VC++ Directories -> Include Files. 您也可以在IDE级别的工具->选项->项目和解决方案-> VC ++目录->包含文件中执行此操作。 Typically this method is reserved for headers included as part of a formal library. 通常,此方法为形式库中包含的标头保留。 The first option is typically preferred as it's portable (you can ship your project file to another developer and, provided you use relative/macro'd paths, they can build the project as-is). 通常首选第一个选项,因为它具有可移植性(您可以将项目文件运送给另一位开发人员,并且,如果您使用相对/宏路径,则他们可以按原样构建项目)。

您正在寻找的是-I标志,然后提供目录...如果您有一个Makefile,则应将其添加到CPP_FLAGS中。

您还可以在环境变量中添加INCLUDE变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM