在需要时配置 Xcode 4 工作区以构建依赖项的正确方法是什么?

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

What's the correct way to configure Xcode 4 workspaces to build dependencies when needed?

xcodebuildxcode4

提问by Cris

My case is simple, a workspace with two sibling projects: one main (iOS) app and a project that builds several static library targets used by the app.

我的案例很简单,一个有两个兄弟项目的工作区:一个主 (iOS) 应用程序和一个构建应用程序使用的多个静态库目标的项目。

Here's how I have configured the build:

这是我配置构建的方式:

  • pointed a 'user header search path' in the main app's build settings to the library project location (via a source tree)
  • in my app's main target's editor -> build phases -> "Link Binary With Libraries" section, added the library products I want to use.
  • in the scheme, ticked 'Find Implicit Dependencies'
  • 将主应用程序构建设置中的“用户标题搜索路径”指向库项目位置(通过源树)
  • 在我的应用程序的主要目标的编辑器 -> 构建阶段 -> “链接二进制库”部分中,添加了我想要使用的库产品。
  • 在方案中,勾选“查找隐式依赖关系”

After a clean (and deletion of the derived data), a build nets me this error during the build of the main project:

在清理(并删除派生数据)之后,在主项目的构建过程中,构建会导致我出现此错误:

ld: library not found for -lChipmunk
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Command /Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin/clang failed with exit code 1

Sure enough, if I look in the newly-created derived data, the only object files to be found are for the main app, not the libraries. A widespread 'find' for *.o files doesn't reveal anything relevant, so the libraries aren't getting lost, they're definitely not being built.

果然,如果我查看新创建的派生数据,可以找到的唯一目标文件是主应用程序,而不是库。对 *.o 文件的广泛“发现”并未揭示任何相关内容,因此库不会丢失,它们肯定不会被构建。

Some supplementary points:

一些补充点:

  • when I've asked about this on the Apple dev forums, it's been suggested that I should add explicit deps in the main target's Build Phases->Target Dependencies editor. But you can only add deps here to targets in the sameproject or subprojects; in my case I have the app and library projects as workplace siblings.
  • if I build each library manually before the main build, all is fine.
  • 当我在 Apple 开发论坛上询问这个问题时,有人建议我应该在主要目标的 Build Phases->Target Dependencies 编辑器中添加显式 deps。但是这里只能在同一个项目或子项目中的targets中添加deps ;就我而言,我将应用程序和图书馆项目作为工作场所的兄弟姐妹。
  • 如果我在主构建之前手动构建每个库,一切都很好。

Update:

更新:

I've just figured out a workaround, which is to add all the deps' targets into the 'build' part of the main app's scheme. I had tried this before without success, but hadn't realised that I could drag the targets around in the list to get the right build order. Builds now happen in the correct order, both after a clean, and after changes in either library or main app source.

我刚刚想出了一个解决方法,即将所有 deps 的目标添加到主应用程序方案的“构建”部分。我之前尝试过但没有成功,但没有意识到我可以在列表中拖动目标以获得正确的构建顺序。构建现在以正确的顺序发生,无论是在清理之后还是在库或主应用程序源中的更改之后。

I'm leaving the question here, because manually sorting out a build order surely shouldn't be necessary. There has to be something wrong with how I have things set up.

我将问题留在这里,因为手动整理构建顺序肯定没有必要。我的设置方式一定有问题。

回答by christoph

Editing the scheme (swapping around build targets, un-/check "Parallelize Build" and/or "Find Implicit Dependencies") didn't work for me. I still had to clean build the project, after any code change in the static lib. Searching the dev forums, I finally found this answer, which worked wonders.

编辑方案(交换构建目标,取消/选中“并行构建”和/或“查找隐式依赖项”)对我不起作用。在静态库中的任何代码更改之后,我仍然必须清理构建项目。搜索开发论坛,我终于找到了这个答案,这很神奇。

Make sure the Identity and Type inspector is showing and select the libWhatever.a file in your application's project (not the library). If you see Location: Relative to Project [or Relative to Group], this is your problem.

  1. Click Relative to Project and change it to Relative to Build Products.
  2. This will change the type of the link, but it will still be broken.
  3. Click the locate button and find the output file.

确保标识和类型检查器正在显示并选择应用程序项目(而不是库)中的 libWhatever.a 文件。如果您看到位置:相对于项目 [或相对于组],这是您的问题。

  1. 单击相对于项目并将其更改为相对于构建产品。
  2. 这将改变链接的类型,但它仍然会被破坏。
  3. 单击定位按钮并找到输出文件。

Adding a static lib to an existing project via Build Phases-> Link Binary with Librariesautomatically makes it "Relative to Group" (if both are siblings in the same workspace). Changing its location the way described above resolves the build dependency problem and in the project navigator your .a file should appear in black letters (instead of red).

通过Build Phases-> Link Binary with Libraries将静态库添加到现有项目会自动使其“相对于组”(如果两者都是同一个工作区中的兄弟)。以上述方式更改其位置可解决构建依赖问题,并且在项目导航器中,您的 .a 文件应以黑色字母(而不是红色)显示。

回答by Jonah

I described the way I've been setting up multiple projets in a workspace 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/

I agree that manually adjusting the build target order in the scheme should be unnecessary, based on Xcode's documentation, but that's the best solution I've found so far.

我同意根据 Xcode 的文档手动调整方案中的构建目标顺序应该是不必要的,但这是我迄今为止找到的最佳解决方案。



Edit: Whenever possible I recommend using https://github.com/CocoaPods/CocoaPodsto manage project dependencies at this point.

编辑:此时我建议尽可能使用https://github.com/CocoaPods/CocoaPods来管理项目依赖项。

回答by Jeffy

Try dragging the library project into the main project:

尝试将库项目拖到主项目中:

library project reference inside main project

主项目中的库项目引用

回答by Ben G

In my case, the only way i could have that sibling configuration build is by manually adding the path "../MyLibProject/build/Debug-iphoneos" (checking recursive just to make sure) in my Build Settings -> Library Search Paths. Adding the .a file in the Main project alone didn't prevent the "lib not found" error at link time.

就我而言,我可以构建同级配置的唯一方法是在我的构建设置 -> 库搜索路径中手动添加路径“../MyLibProject/build/Debug-iphoneos”(检查递归以确保)。仅在 Main 项目中添加 .a 文件并不能防止链接时出现“lib not found”错误。