Xcode 4.2:如何从子项目导入 .h 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9501799/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Xcode 4.2: How to import .h file from subproject
提问by Shameem
I am new to Xcode subprojects. In my iPhone app project MyProject
, I am trying to refactor some common code to a static library project called MyLibrary
. After I create MyLibrary
and move the code, MyProject
is no-longer compiling. The error is that the MyProject
cannot see the .h files in MyLibrary
.
我是 Xcode 子项目的新手。在我的 iPhone 应用程序项目中MyProject
,我试图将一些通用代码重构为一个名为MyLibrary
. 在我创建MyLibrary
并移动代码后,MyProject
不再编译。错误是MyProject
无法在 .h 文件中看到 .h 文件MyLibrary
。
The error MyLibraryConfig.h: No such file or directory
is coming in the line:
错误MyLibraryConfig.h: No such file or directory
即将出现:
#import "MyLibraryConfig.h"
- How to import the
MyLibrary
.h files inMyProject
? - What is the best practice here? Assuming I have multiple such libraries, it is tedious to add these to header search paths to the parent project.
- 如何导入
MyLibrary
.h 文件MyProject
? - 这里的最佳做法是什么?假设我有多个这样的库,将它们添加到父项目的头搜索路径是很乏味的。
采纳答案by v1Axvw
Maybe your header file is also in a subdirectory.
也许你的头文件也在一个子目录中。
Imagine the following directory setup:
想象以下目录设置:
- Desktop
- MyProject
- MyProject.xcodeproj
- main.m
- MyLibrary
- MyLibrary.xcodeproj
- MyHeaderFile.h <-- wanted header file
If main.mhas these contents:
如果main.m有这些内容:
#include "MyHeaderFile.h"
int main() {
return 0;
}
The compiler (gcc) will think that MyHeaderFile.his located in the same directory as main.m, from which it is included. To tell the compiler you mean the header file in a subfolder, you have can do two things.
编译器 (gcc) 会认为MyHeaderFile.h与包含它的main.m位于同一目录中。要告诉编译器您的意思是子文件夹中的头文件,您可以做两件事。
- You can add a directory to the gcc compiler that says: "hey, also look in that folder". You can do this by using the
-iquote myFolder
flag. - You can include the directory in the include-statement:
#include "MyLibrary/MyHeaderFile.h"
- 您可以向 gcc 编译器添加一个目录,上面写着:“嘿,也请查看该文件夹”。您可以通过使用
-iquote myFolder
标志来做到这一点。 - 您可以在包含语句中包含目录:
#include "MyLibrary/MyHeaderFile.h"
There could of course be another problem, but this seams like the most straightforward one.
当然可能还有另一个问题,但这似乎是最直接的问题。
回答by Saran
In build settings --> Header Search Path --> Add below entry
在构建设置--> 标题搜索路径--> 添加以下条目
$(SRCROOT) and mark it as recursive.
$(SRCROOT) 并将其标记为递归。