依赖项中公共头文件的 Xcode 搜索路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5059506/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 20:16:39  来源:igfitidea点击:

Xcode search paths for public headers in dependencies

objective-cxcodedependency-management

提问by Krumelur

I am trying to clean up some of my projects, and one of the things that are puzzling me is how to deal with header files in static libraries that I have added as "project dependencies" (by adding the project file itself). The basic structure is like this:

我正在尝试清理我的一些项目,让我感到困惑的一件事是如何处理我作为“项目依赖项”添加的静态库中的头文件(通过添加项目文件本身)。基本结构是这样的:


MyProject.xcodeproj
  Contrib
    thirdPartyLibrary.xcodeproj
  Classes
    MyClass1.h
    MyClass1.m
    ...

Now, the dependencies are all set up and built correctly, but how can I specify the public headers for "thirdPartyLibrary.xcodeproj" so that they are on the search path when building MyProject.xcodeproj. Right now, I have hard-coded the include directory in the thirdPartyLibrary.xcodeproj, but obviously this is clumsy and non-portable. I assume that, since the headers are public and already built to some temporary location in ~/Library (where the .a file goes as well), there is a neat way to reference this directory. Only.. how? An hour of Googling turned up blank, so any help is greatly appreciated!

现在,所有依赖项都已正确设置和构建,但是如何为“thirdPartyLibrary.xcodeproj”指定公共标头,以便它们在构建 MyProject.xcodeproj 时位于搜索路径上。现在,我已经硬编码了thirdPartyLibrary.xcodeproj 中的include 目录,但显然这是笨拙且不可移植的。我认为,由于标头是公开的并且已经构建到 ~/Library 中的某个临时位置(.a 文件所在的位置),因此有一种引用此目录的简洁方法。只有..如何?一个小时的谷歌搜索变成空白,所以非常感谢任何帮助!

采纳答案by Damian Carrillo

I think that your solution is sufficient and a generally accepted one. One alternative would be to have all header files located under an umbrella directory that can describe the interface to using the depended-on libraries and put that in your include path. I see this as being similar to /usr/include. Another alternative that I have never personally tried, but I think would work would be to create references to all the headers of thirdPartyLibraryfrom MyProjectso that they appear to be members of the MyProject. You would do this by dragging them from some location into MyProject, and then deselecting the checkbox that says to copy them into the project's top level directory. From one perspective this seems feasible to me because it is as if you are explicitly declaring that your project depends on those specific classes, but it is not directly responsible for compiling them.

我认为您的解决方案已经足够并且是普遍接受的解决方案。一种替代方法是将所有头文件都放在一个伞形目录下,该目录可以描述使用依赖库的接口并将其放入包含路径中。我认为这类似于 /usr/include。另一种我从未亲自尝试过但我认为可行的替代方法是从MyProject创建对thirdPartyLibrary 的所有标题的引用,以便它们看起来是MyProject 的成员。您可以通过将它们从某个位置拖到MyProject 中来做到这一点,然后取消选择将它们复制到项目顶级目录的复选框。从一个角度来看,这对我来说似乎是可行的,因为就好像您明确声明您的项目依赖于那些特定的类,但它并不直接负责编译它们。

One of the things to be wary of when addressing this issue is depending on implementation-specific details of Xcode for locating libraries automatically. Doing so may seem innocuous in the meantime but the workflows that it uses to build projects are subject to change with updates and could potentially break your project in subtle and confusing ways. If they are not well-defined in some documentation, I would take any effect as being coincidental and not worth leveraging in your project when you can enforce the desired behavior by some other means. In the end, you may have to define a convention that you follow or find one that you adopt from someone else. By doing so, you can rest assured that if your solution is documented and reproducible, any developer (including yourself in the future) can pick it up and proceed without tripping over it, and that it will stand the testament of time.

解决此问题时要注意的一件事是根据 Xcode 的特定于实现的细节来自动定位库。这样做在此期间可能看起来无害,但它用于构建项目的工作流程会随着更新而变化,并且可能会以微妙和混乱的方式破坏您的项目。如果它们在某些文档中没有明确定义,那么当您可以通过其他方式强制执行所需的行为时,我会将任何效果视为巧合并且不值得在您的项目中利用。最后,您可能必须定义一个您遵循的约定,或者从其他人那里找到一个您采用的约定。通过这样做,您可以放心,如果您的解决方案被记录在案并且可以重现,那么任何开发人员(包括未来的您自己)都可以拿起它并继续进行,而不会被它绊倒,

回答by Casey Fleser

If I understand correctly, I believe you want to add a path containing $(BUILT_PRODUCTS_DIR) to the HEADER_SEARCH_PATHS in your projects build settings.

如果我理解正确,我相信您想在项目构建设置中将包含 $(BUILT_PRODUCTS_DIR) 的路径添加到 HEADER_SEARCH_PATHS。

As an example, I took an existing iOS project which contains a static library, which is included just in the way you describe, and set the libraries header files to public. I also noted that the PUBLIC_HEADERS_FOLDER_PATH for this project was set to "/usr/local/include" and these files are copied to $(BUILT_PRODUCTS_DIR)/usr/local/include when the parent project builds the dependent project. So, the solution was to add $(BUILT_PRODUCTS_DIR)/usr/local/include to HEADER_SEARCH_PATHS in my project's build settings.

举个例子,我拿了一个现有的 iOS 项目,其中包含一个静态库,它只是按照您描述的方式包含在内,并将库头文件设置为 public。我还注意到,这个项目的 PUBLIC_HEADERS_FOLDER_PATH 被设置为“/usr/local/include”,当父项目构建依赖项目时,这些文件被复制到 $(BUILT_PRODUCTS_DIR)/usr/local/include。因此,解决方案是在我的项目的构建设置中将 $(BUILT_PRODUCTS_DIR)/usr/local/include 添加到 HEADER_SEARCH_PATHS 。

HEADER_SEARCH_PATHS = $(BUILT_PRODUCTS_DIR)/usr/local/include

Your situation may be slightly different but the exact path your looking for can probably be found in Xcode's build settings. Also you may find it helpful to add a Run Script build phase to your target and note the values of various settings at build time with something like:

您的情况可能略有不同,但您寻找的确切路径可能可以在Xcode 的构建设置中找到。此外,您可能会发现将运行脚本构建阶段添加到您的目标并在构建时注意各种设置的值很有帮助,例如:

echo "BUILT_PRODUCTS_DIR " $BUILT_PRODUCTS_DIR
echo "HEADER_SEARCH_PATHS " $HEADER_SEARCH_PATHS
echo "PUBLIC_HEADERS_FOLDER_PATH " $PUBLIC_HEADERS_FOLDER_PATH
.
.
.
etc.

回答by Ciryon

The way we do it is to go into build target settings for the main project and add:

我们这样做的方法是进入主项目的构建目标设置并添加:

User Header Search Path = "Contrib"

and check that it searches recursively. We don't see performance problems with searching recursively even with many (10-15 in some projects) dependencies.

并检查它是否递归搜索。即使有很多(在某些项目中为 10-15)依赖项,我们也没有看到递归搜索的性能问题。