Xcode 4 中的静态库
(我的问题之前曾在这里被问过,但我看不到有效的答案。例如 带有静态库项目和应用程序项目的 Xcode4 工作区)
我正在尝试使用第 3 方提供的库。他们提供了构建 libLibraryName.a 文件的 XCode 项目。他们建议将该项目作为子项目添加到我自己的项目中,然后将产品 libLibraryName.a 文件添加到我的项目设置“将二进制与库链接”中描述的库集中。
该库确实正确构建:生成了 .a 文件。但该项目在 Products 组下显示了一个红色的 libLibraryName.a 文件。我无法让它变黑。父项目表示找不到用于链接的LibraryName。
作为测试,我使用 XCode 4 静态库模板创建了一个新的静态库项目。该项目表现出相同的行为 - 即使构建了 .a 文件,产品也从未显示为“黑色”。 (编辑:如果您为设备而不是模拟器构建,它确实会变黑)。
我知道 XCode 4 默认将中间文件和产品文件放置在共享位置。我尝试过此设置,并且更改了设置以将产品文件放置在构建设置中描述的文件夹中。这两种设置都不起作用。
人们还建议为设备而不是模拟器进行构建。我试过这个没有效果。
什么给?如何让静态库项目识别其构建产品的位置,并随后在另一个项目中引用该产品?
(My question has been asked here before but with no working answers that I can see. E.g. Xcode4 Workspace with Static library project & application project)
I'm trying to use a library provided by a 3rd party. They provide the XCode project which builds a libLibraryName.a file. They recommend adding the project as a subproject to my own, then adding the product libLibraryName.a file to the set of libraries described in my project settings "Link Binary with Libraries".
The library does build correctly: the .a file is generated. But the project shows a red libLibraryName.a file under the Products group. I can't get it to turn black. And the parent project says it cant find LibraryName for linking.
As a test I created a new static library project using the XCode 4 static-library template. This project exhibits the same behavior - the product never shows up 'black' even though the .a file is built. (Edit: it does turn black if you build for device, not simulator).
I know that XCode 4 places intermediate and product files in a shared location by default. I've tried this setting, and I've changed the setting to place the product files in the folders described in the build settings. Neither setting works.
Folks have also suggested building for a device rather than simulator. I've tried this to no avail.
What gives? How do I get a static library project to recognize where it built the product, and subsequently reference this product in another project?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
很多跳圈,但现在这是我的笔记,我已经开始工作了。
如果您创建一个新的 XCode4 iOS“Cocoa Touch Static Library”项目(并向其中添加一些代码),该项目将开箱即用地构建良好。但是,当您进行设备构建时,产品文件 libLibraryName.a 只会变黑(从红色,表示该文件不存在)。模拟器构建不会显示目标实际上已构建。
在项目目标构建设置中,“按配置构建产品路径”默认为
$(BUILD_DIR)/$(CONFIGURATION)$(EFFECTIVE_PLATFORM_NAME)
如果您将其更改为其他内容 (或者如果您从使用的 XCode3.x 升级了项目$(BUILD_DIR)/$(CONFIGURATION)$(EFFECTIVE_PLATFORM_NAME)$(IPHONEOS_DEPLOYMENT_TARGET)
作为默认值,我相信),那么产品文件 libLibraryName.a 永远不会变黑。对我来说,这表明 XCode 中某个地方存在错误。我可以忍受产品文件在构建后变黑的情况(这是一个很好的指示器,但是嘿,无论如何。)但是我确实需要我的消费项目能够找到库的正确构建,无论是模拟器或设备。在理想的情况下,应该有一个包含 i386/arm6/arm7 位的 .a 文件,但同样,这不是我的库/库项目。
XCode4 转换指南 是我看到了光明。它规定创建一个托管这两个项目的工作区,并且它们都将构建到同一个共享构建目录中。我以前没有使用过工作区,因此我使用文件/另存为工作区命令来创建新的工作区文件。然后,我添加了图书馆项目,并注意确保它被放置为我的主要项目的同级项目,而不是作为子项目。
我必须确保工作区配置为将构建输出放入公共文件夹中。在“工作区设置”对话框中,将“构建位置”设置设置为“将构建产品放置在派生数据位置”
我还必须确保在“管理方案”对话框中选中每个项目的“共享”复选框。
最后,为了指定主项目的库依赖项,我只需转到目标“构建阶段”选项卡的“将二进制文件与库链接”部分,然后单击“+”,然后选择libLibraryName.a Workspace 文件夹下的文件。请注意,我之前在没有工作空间且没有公共构建目录的情况下尝试过此操作,结果是 XCode 在链接期间找不到 .a 文件。
总而言之,它就像一种魅力。我不禁认为它应该容易得多——因为我相信它是在 XCode3 中的。
我很乐意阅读其他人在这方面的经验,或者有关其他(更简单?)使链接静态库正常工作的方法的反馈。
Lots of hoop jumping, but here are my notes now that it I got it working.
If you create a new stock XCode4 iOS "Cocoa Touch Static Library" project (and add some code to it) the project will build fine out of the box. But the Product file libLibraryName.a only turns black (from red, denoting the file doesn't exist) when you do a device build. A simulator build does not show that the target was built when in fact it was.
In the project target build-settings, the "Per-Configuration Build Products Path" defaults to
$(BUILD_DIR)/$(CONFIGURATION)$(EFFECTIVE_PLATFORM_NAME)
If you change this to something else (or if you upgraded the project from XCode3.x which used$(BUILD_DIR)/$(CONFIGURATION)$(EFFECTIVE_PLATFORM_NAME)$(IPHONEOS_DEPLOYMENT_TARGET)
as the default, I believe), then the Product file libLibraryName.a wont ever turn black. To me this says there's a bug in XCode somewhere.I can live without the Product file turning black after a build (it is a nice indicator, but hey, whatever.) But I do need my consuming project to be able to find the correct build of the library, be it for simulator or device. In an ideal world there would be a single .a file with i386/arm6/arm7 bits in it, but again, this isn't my library / library project.
The XCode4 Transition Guide is what showed me the light. It prescribes creating a workspace that hosts both of the projects, and they will both build into the same shared build directory. I was not using a workspace previously, so I used the File/Save As Workspace command to create the new workspace file. Then, I added the library project, taking care to ensure it was placed as a peer to my primary project and not as a child.
I had to make sure the workspace was configured to place build output into a common folder. In the Workspace Settings dialog, set the Build Location setting to "Place Build Products in Derived Data Location"
I also had to make sure to check the "shared" checkbox for each project in the Manage Schemes dialog.
Finally, to specify the library dependency for my main project, I simply went to the target Build Phases tab, Link Binaries with Libraries section, and clicked the '+', then selected the libLibraryName.a file from beneath the Workspace folder. Note that I'd tried this before when there was no workspace and no common build dir, and the result was that XCode couldn't find the .a file during link.
All said and done, it works like a charm. I can't help but think it should be a lot easier - as I believe it was in XCode3.
I'd be happy to read about anyone else's experience with all this, or any feedback about other (simpler?) ways to make linking static libs work well.
在这里查看我的答案,看看它是否对您有帮助:
将静态库链接到 XCode 4 中的 iOS 项目
这些基于我对自己的库的说明。我认为原始过程中缺少的步骤是,您在“链接二进制文件与库”中链接静态库的同时,没有将静态库作为目标依赖项添加到应用程序项目中(我的说明中的步骤 3)。您可能还需要执行步骤 #5,具体取决于静态库项目链接标头的方式。
当我使用自己的具有对静态库项目的跨项目引用的应用程序执行此过程时,它实际上比 Xcode 3 中的等效过程少了一步。
Check out my answer here and see if it helps you:
Linking a static library to an iOS project in XCode 4
These are based on my instructions for my own library. I think the missing step in your original process is that you don't add the static library to your app project as a target dependency (step 3 in my instructions) at the same time that you link it in "Link Binary with Libraries". You also might need to do step #5 depending on how the headers are linked by the static library project.
When I do this process with my own apps that have cross-project references to static library projects, it actually has one step less than the equivalent process in Xcode 3.
看看我的 解决方案说明和打开雷达条目。
红色的产品节点是 Xcode 的一个错误。您可以通过更改 Project 构建设置上的
SDKROOT
来使其工作。 目标构建设置不适用于 IDE 显示和显示。支持。编辑
供以后参考。
目前,我的看法改为Xcode项目并不完全能够处理多平台。虽然可以显示多平台,但通过
SDKROOT
设置,一次只能选择一个平台进行屏幕显示。如果您选择iOS,它将使用Debug-iphoneos
之类的东西来构建产品路径。因此所有 Mac OS X 目标都将丢失。如果您选择Mac OS X,它将使用调试
之类的东西。因此所有 iOS 目标的产品都将丢失。我认为 Xcode 仍然存在与此相关的内部错误。 Xcode 的稳定还需要很长时间。
Take a look of my solution note and Open Radar entry.
The red colored product node is a bug of Xcode. You can make it work by changing
SDKROOT
on Project build setting. Target build settings won't work for IDE display & support.Edit
For later reference.
Currently, my opinion is changed to the Xcode project is not completely able to deal with multiple platforms. Though it can display multiple platforms but only one platform can be chosen at one time to screen display by the
SDKROOT
setting. If you select iOS, it will use something likeDebug-iphoneos
for build product path. So all Mac OS X targets will be missing. If you choose Mac OS X, it will use something likeDebug
. So all iOS targets' products will be missing.I think Xcode still have internal bug related this. It's long time to go to make Xcode to be stabilized.
我和我的团队也有同样的问题。一位开发人员遇到了这个问题,但是我的 xcode 能够编译并正确找到标头。顺便说一句:所有“构建设置”均已正确配置(始终搜索用户路径、用户标头路径等)。
我意识到他的项目位于路径中带有空格的目录中(../my project/blah.xcodeproj)。改变这一点,Xcode 能够从同一工作区中的静态库中找到标头。
只需注意目录名称即可。
我的两分钱
I had the same issue with my team. One of the developers was suffering with this issue, however my xcode was able to compile and found the header properly. BTW: all "build Settings" were properly configured (always search user paths, user header paths, etc).
I realized that his project was in a directory with spaces in its path (../my project/blah.xcodeproj). Changing that, Xcode was able to find the headers from the static library within the same workspace.
Just be aware of directory names.
My two cents
一个澄清的细节(在深入研究构建输出直到我开始斗鸡眼之后):如果您发现您的库标头正在导出到
Build/Products/Debug
并且您的父项目正在查找 < code>Build/Products/Debug-iphonesimulator,您的库是为 OS X 而不是 iOS 构建的。您可以在项目设置的“体系结构”部分的“支持的平台”设置中更改此设置。如果您创建普通 C++ 静态库项目,OS X 似乎是默认设置,因此很容易遇到这种情况。One clarifying detail (after digging through build output until I started to go cross-eyed): if you are finding your library headers are being exported to
Build/Products/Debug
and your parent project is looking inBuild/Products/Debug-iphonesimulator
, your library is being built for OS X, not iOS. You can change this in the "Supported Platforms" setting in the "Architectures" section of the project settings. OS X appears to be the default setting if you create a vanilla C++ static lib project, so this situation is easy enough to encounter.我的一个图书馆遇到了这个问题。实际上,我还构建了另外 4 个库,它们看起来不错,但显示为黑色,但其中一个库却不然。我的红色库是通过更改库项目的构建设置中的 Base SDK 解决的。由于该库可以针对 Mac OS X 和 iOS 构建,因此被设置为 Mac OS X 设置。 iOS 目标库仍在构建,但从未变黑。一旦我将 Base SDK 构建设置更改为最新的 iOS,我的库就变成了黑色。
I was having this issue with one of my libraries. I actually have 4 other libraries I build that are included that look fine, appear black, but one that did not. My red library was solved by changing the Base SDK in the Build Settings of the library project. Since the library could build for Mac OS X and iOS it was set to the Mac OS X setting. The iOS target library still built but never turned black. Once I changed the Base SDK build setting to be latest iOS my library turned to black.