我应该如何管理 Xcode 工作区中跨项目的依赖项?

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

How should I manage dependencies across projects in an Xcode workspace?

xcodexcode4build-dependencies

提问by

I'm working on an iOS app project, and add the json-framework project to the workspace. The project navigator on the left shows both projects, and the build scheme selector shows the schemes from both projects too. Now I want to add the libjson.a target from the json-framework project as a dependency on the iOS app target in the other project. The expected result is that whenever the app target is built, it builds (if necessary) the library target and links the app target against it. Here are the ways I've tried to do this:

我正在处理一个 iOS 应用程序项目,并将 json-framework 项目添加到工作区。左侧的项目导航器显示两个项目,构建方案选择器也显示两个项目的方案。现在我想将 json-framework 项目中的 libjson.a 目标添加为另一个项目中 iOS 应用程序目标的依赖项。预期的结果是,无论何时构建 app 目标,它都会构建(如有必要)库目标并将 app 目标链接到它。以下是我尝试执行此操作的方法:

  • Build both as part of the same scheme. The way I try this is to edit the scheme for my app, adding 'libjson.a' to the 'Build' portion of the scheme, and by the way "Find Implicit Dependencies" is checked. Then I go to the target editor for my app target, and in "Build Phases"->"Link Binary With Libraries", I choose 'libjson.a' from the list of workspace libraries. When I subsequently try to build the scheme, I see it build the library target, but building the app target fails with linker error "Library not found for -ljson" - suggesting that it hasn't actually discovered that the library has been built. Indeed in the project navigator, the entry under the app project for the library is still red indicating that the file doesn't exist.

  • Add the json target as an explicit dependency. To try this, I don't modify the build scheme, but go to the target editor for my app target and click the add button under 'Target Dependencies'. No targets from other projects in the workspace show up, so this is a non-starter.

  • Drag the JSON project into the other project, then add the target as a dependency. This is what I would have done in Xcode 3. In the project navigator, I grab the library project and drag it over the app project. This brings up the usual 'add files' pane, which I just dismiss by clicking 'Finish'. There are now twoentries for the library project in the project navigator: one at the top level, and one under the app project. I can now add the library target as a dependency of the app target using the target editor, and can link against it without error in the link libraries phase. But it looks broken: there are multiple entries for the same project in the navigator. Is there a different way to do this?

  • 将两者构建为同一方案的一部分。我尝试这样做的方法是为我的应用程序编辑方案,将 'libjson.a' 添加到方案的 'Build' 部分,顺便检查“查找隐式依赖项”。然后我转到我的应用程序目标的目标编辑器,在“构建阶段”->“链接二进制库”中,我从工作区库列表中选择“libjson.a”。当我随后尝试构建该方案时,我看到它构建了库目标,但构建应用程序目标失败,并出现链接器错误“未找到 -ljson 的库”——这表明它实际上并未发现库已构建。确实在项目导航器中,库的应用项目下的条目仍然是红色的,表示该文件不存在。

  • 将 json 目标添加为显式依赖项。为了尝试这个,我不修改构建方案,而是转到我的应用程序目标的目标编辑器,然后单击“目标依赖项”下的添加按钮。工作区中没有来自其他项目的目标出现,所以这是一个非启动器。

  • 将 JSON 项目拖到另一个项目中,然后将目标添加为依赖项。这就是我在 Xcode 3 中所做的。在项目导航器中,我抓取库项目并将其拖到应用程序项目上。这会显示通常的“添加文件”窗格,我只需单击“完成”即可将其关闭。现在项目导航器中有两个库项目条目:一个在顶层,一个在应用程序项目下。我现在可以使用目标编辑器将库目标添加为应用程序目标的依赖项,并且可以在链接库阶段无误地链接它。但它看起来坏了:导航器中有同一个项目的多个条目。有没有不同的方法来做到这一点?

What should be considered the "Xcode 4-ish" way of connecting these targets in different projects in the same workspace? It would seem lacking if multiple projects in the same workspace can't actually interact with each other. Thanks, Graham.

在同一工作区的不同项目中连接这些目标的“Xcode 4-ish”方式应该是什么?如果同一工作区中的多个项目实际上不能相互交互,这似乎是缺乏的。谢谢,格雷厄姆。

采纳答案by creednmd

I've just set a test project up, pretty much as you describe in version 3, by creating a new workspace and dragging the two Xcode project into it, nested as shown.

我刚刚设置了一个测试项目,就像您在版本 3 中描述的那样,通过创建一个新工作区并将两个 Xcode 项目拖入其中,如图所示嵌套。

You can delete the sibling project if you have it already.

如果您已经拥有同级项目,则可以删除它。

Hitting build on this and it just works, as far as I can see.

在此基础上进行构建,就我所见,它确实有效。

Scheme SettingsProject Layout

方案设置项目布局

I imagine there is internal path-confusion if you have two projects, and I'd be inclined to fiddle with location settings in "View"->"Utilities"->"File Inspector" and see what effect that has.

我想如果你有两个项目,内部路径混乱,我倾向于在“视图”->“实用工具”->“文件检查器”中摆弄位置设置,看看有什么效果。

enter image description hereenter image description here

在此处输入图片说明在此处输入图片说明

Another thing to try is to set your paths up in Xcode "Preferences…"->"Source Trees" and refer to them that way, as described here: Easy, Modular Code Sharing Across iPhone Apps: Static Libraries and Cross-Project References

要尝试的另一件事是在 Xcode“首选项...”->“源树”中设置您的路径并以这种方式引用它们,如下所述:跨 iPhone 应用程序的简单、模块化代码共享:静态库和跨项目引用

HTH. Andy W.

哈。安迪·W。

回答by Jonah

I managed to get dependencies between projects in a workspace to work as I described here: http://blog.carbonfive.com/2011/04/04/using-open-source-static-libraries-in-xcode-4/.

我设法使工作区中的项目之间的依赖关系按照我在此处描述的那样工作:http: //blog.carbonfive.com/2011/04/04/using-open-source-static-libraries-in-xcode-4/

Unfortunately I can't find a way to get Xcode to discover implicit dependencies or index everything in the build as advertised. I found workaround to both but I'm hoping that less manual configuration will be needed as Xcode 4 matures.

不幸的是,我无法找到一种方法让 Xcode 发现隐式依赖项或按照宣传的那样索引构建中的所有内容。我找到了两种解决方法,但我希望随着 Xcode 4 的成熟,需要的手动配置更少。

回答by Tyler Daniel

I was going to ask the same question, thinking that my own solution couldn't be right. But I don't see it mentioned here, and it does seem to work. Clearly XCode 4 is a work in progress. :)

我正要问同样的问题,认为我自己的解决方案不可能是正确的。但我没有看到这里提到它,它似乎确实有效。显然,XCode 4 正在进行中。:)

I have a workspace with two projects: a static library and an app which uses the library. The projects are siblings. Each project has its own scheme, and each scheme is set to only build one target. In other words, I added two projects to the workspace and that's it.

我有一个包含两个项目的工作区:一个静态库和一个使用该库的应用程序。这些项目是兄弟姐妹。每个项目都有自己的scheme,每个scheme都设置为只构建一个target。换句话说,我向工作区添加了两个项目,仅此而已。

To add the static library as a dependency of the app, I just drag the libsomething.a product from the library project (Project Navigator) into the "Link Binary with Libraries" list for the app target. That's it. Now when I build the app the library project is built first and then linked. Interestingly, when I modify the app's scheme to use a different configuration (eg, Release instead of Debug), the library is built using the same configuration.

要将静态库添加为应用程序的依赖项,我只需将库项目(项目导航器)中的 libsomething.a 产品拖到应用程序目标的“Link Binary with Libraries”列表中。就是这样。现在,当我构建应用程序时,首先构建库项目,然后链接。有趣的是,当我修改应用程序的方案以使用不同的配置(例如,发布而不是调试)时,库是使用相同的配置构建的。

So it works, and there is clearly some automatic dependency checking going on here. But it feels wrong. Then again, so does the modal scheme editor/manager and lack of a workspace object in the project navigator... I never thought I'd say it, but the Visual Studio UI (bleh) is a lot clearer.

所以它有效,而且这里显然有一些自动依赖项检查。但是感觉不对。再说一次,模态方案编辑器/管理器也是如此,项目导航器中缺少工作区对象......我从没想过我会说,但 Visual Studio UI (bleh) 更清晰。

回答by Ben G

My bullet-proof solution to do this :

我的防弹解决方案可以做到这一点:

Create "Per Debug-Release / Per Architecture" settings in Build Settings in the Main project (not the lib), to include either ../MyLibProject/build/Debug-iphoneos or ../MyLibProject/build/Release-iphonesimulator or etc..
depending on the configuration (you can create those kind of configuration by clicking on the + next to Debug or Release and choose either "Any iOS Simulator SDK" or "any iOS SDK". You need to do that for both "Header Search Path" (in case your library copy some headers files, which is more than likely) AND for "Library Search Paths". Which means that for each setting, you'll probably end-up with 4 different paths (debug sim , debug ios, release sim, release ios). That would make sure the configuration of both projects match.

在主项目(不是库)的构建设置中创建“每个调试发布/每个架构”设置,以包括 ../MyLibProject/build/Debug-iphoneos 或 ../MyLibProject/build/Release-iphonesimulator 等..
取决于配置(您可以通过单击 Debug 或 Release 旁边的 + 并选择“Any iOS Simulator SDK”或“any iOS SDK”来创建此类配置。您需要为“Header Search Path”(如果您的库复制了一些头文件,这很有可能)和“库搜索路径”。这意味着对于每个设置,您可能最终会得到 4 个不同的路径(debug sim ,debug ios , release sim, release ios). 这将确保两个项目的配置匹配。

Now, to auto-compile the lib, that is to create the dependency, you can use the "Build Phase -> Link to Binary With Libraries -> + -> select the .a file" advice given above.

现在,要自动编译 lib,即创建依赖项,您可以使用上面给出的“构建阶段 -> 使用库链接到二进制文件 -> + -> 选择 .a 文件”的建议。

That's the only way I managed to have something that builds and link correctly for every environment on xcode 4.5

这是我设法为 xcode 4.5 上的每个环境正确构建和链接的唯一方法

Note : I even added the -lmyLib flag in "other linker flags", but i'm not sure that's really necessary

注意:我什至在“其他链接器标志”中添加了 -lmyLib 标志,但我不确定这是否真的有必要

回答by yoAlex5

I see the next variants:

我看到下一个变体:

  1. Explicit dependencyin a project[About]
  2. Implicit dependencyin a workspace[About]
  1. Explicit dependency在一个项目中[关于]
  2. Implicit dependency在工作区[关于]

回答by amrox

I've had some success with creating framework-like static libraries, though it's not a perfect solution.

我在创建类似框架的静态库方面取得了一些成功,尽管它不是一个完美的解决方案。

回答by Joris Kluivers

See the Xcode user guide: Xcode Concepts -> Xcode workspace under 'Projects in a Workspace Share a Build Directory'.

请参阅 Xcode 用户指南:Xcode 概念 ->“工作区中的项目共享构建目录”下的 Xcode 工作区。

All projects in a single workspace share a build directory. Dependencies are discovered automatically and build if needed:

单个工作区中的所有项目共享一个构建目录。依赖项会自动发现并在需要时构建:

"Xcode examines the files in the build directory to discover implicit dependencies. For example, if one project included in a workspace builds a library that is linked against by another project in the same workspace, Xcode automatically builds the library before building the other project, even if the build configuration does not make this dependency explicit. You can override such implicit dependencies with explicit build settings if necessary. For explicit dependencies, you must create project references."

“Xcode 检查构建目录中的文件以发现隐式依赖关系。例如,如果工作区中包含的一个项目构建的库与同一工作区中的另一个项目链接,则 Xcode 会在构建另一个项目之前自动构建库,即使构建配置没有使此依赖项显式。如有必要,您可以使用显式构建设置覆盖此类隐式依赖项。对于显式依赖项,您必须创建项目引用。”