ios Xcode 4 中的静态库

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

Static libraries in Xcode 4

iosxcode4static-librariesxcode3to4

提问by TomSwift

(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)

(我的问题之前曾在这里问过,但没有我能看到的有效答案。例如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.afile. They recommend adding the project as a subproject to my own, then adding the product libLibraryName.afile to the set of libraries described in my project settings "Link Binary with Libraries".

我正在尝试使用第三方提供的库。他们提供了构建libLibraryName.a文件的 XCode 项目。他们建议将项目作为子项目添加到我自己的项目中,然后将产品libLibraryName.a文件添加到我的项目设置“Link Binary with Libraries”中描述的一组库中。

The library does build correctly: the .a file is generated. But the project shows a red libLibraryName.afile under the Products group. I can't get it to turn black. And the parent project says it cant find LibraryNamefor linking.

该库确实构建正确:生成了 .a 文件。但是该项目在 Products 组下显示了一个红色的libLibraryName.a文件。我不能让它变黑。并且父项目说它无法找到用于链接的LibraryName

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).

作为测试,我使用 XCode 4 静态库模板创建了一个新的静态库项目。该项目表现出相同的行为 - 即使构建了 .a 文件,产品也永远不会显示为“黑色”。(编辑:如果您为设备而不是模拟器构建它确实会变黑)。

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.

我知道 XCode 4 默认将中间文件和产品文件放在共享位置。我已尝试过此设置,并且已更改设置以将产品文件放置在构建设置中描述的文件夹中。这两种设置都不起作用。

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?

是什么赋予了?如何让静态库项目识别它构建产品的位置,然后在另一个项目中引用该产品?

回答by TomSwift

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.aonly 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.awont 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 Guideis 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 Workspacecommand 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.afile 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.

  • 如果您创建一个新的 XCode4 iOS“Cocoa Touch 静态库”项目(并向其中添加一些代码),该项目将开箱即用。但是产品文件libLibraryName.a仅在您进行设备构建时变为黑色(从红色变为红色,表示该文件不存在)。模拟器构建并未显示目标已构建,而实际上已构建。

  • 在项目目标构建设置中,“每配置构建产品路径”默认为$(BUILD_DIR)/$(CONFIGURATION)$(EFFECTIVE_PLATFORM_NAME)如果您将其更改为其他内容(或者如果您从用作$(BUILD_DIR)/$(CONFIGURATION)$(EFFECTIVE_PLATFORM_NAME)$(IPHONEOS_DEPLOYMENT_TARGET)默认值的XCode3.x 升级项目,我相信),则文件libLibraryName.a永远不会变黑。对我来说,这表示 XCode 中的某个地方存在错误。

  • 我可以在构建后没有产品文件变黑的情况下生活(这是一个很好的指标,但是嘿,无论如何。)但是我确实需要我的消费项目能够找到库的正确构建,无论是模拟器还是设备. 在理想的世界中,会有一个包含 i386/arm6/arm7 位的 .a 文件,但同样,这不是我的库/库项目。

  • XCode4转换指南是什么给我的光。它规定创建一个承载这两个项目的工作区,并且它们都将构建到同一个共享构建目录中。我以前没有使用工作区,所以我使用文件/另存为工作区命令来创建新的工作区文件。然后,我添加了图书馆项目,注意确保它被放置为我的主要项目的对等项,而不是作为一个孩子。

  • 我必须确保将工作区配置为将构建输出放入公共文件夹中。在 Workspace Settings 对话框中,将 Build Location 设置为“Place Build Products in Derived Data Location”

  • 我还必须确保在“管理方案”对话框中选中每个项目的“共享”复选框。

  • 最后,要为我的主项目指定库依赖项,我只需转到目标 Build Phases 选项卡,Link Binaries with Libraries 部分,然后单击“+”,然后从 Workspace 文件夹下方选择libLibraryName.a文件。请注意,我之前在没有工作区和通用构建目录时尝试过此操作,结果是 XCode 在链接期间找不到 .a 文件。

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.

总而言之,它就像一个魅力。我不禁认为它应该容易得多 - 因为我相信它在 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.

我很乐意阅读其他人在这方面的经验,或者任何关于其他(更简单?)使静态库链接正常工作的方法的反馈。

回答by Halle

Check out my answer here and see if it helps you:

在这里查看我的答案,看看它是否对您有帮助:

Linking a static library to an iOS project in XCode 4

将静态库链接到 XCode 4 中的 iOS 项目

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.

这些是基于我对我自己的图书馆的说明。我认为您的原始流程中缺少的步骤是您没有将静态库作为目标依赖项添加到您的应用程序项目中(我的说明中的第 3 步),同时您将它链接到“链接二进制与库”中。您可能还需要执行步骤 #5,具体取决于静态库项目如何链接标头。

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 3 中的等效过程少了一步。

回答by Eonil

Take a look of my solution noteand Open Radar entry.

看看我的解决方案说明Open Radar 条目

The red colored product node is a bug of Xcode. You can make it work by changing SDKROOTon Projectbuild setting. Targetbuild settings won't work for IDE display & support.

红色的产品节点是 Xcode 的一个 bug。你可以把它通过改变工作SDKROOT项目建设环境。目标构建设置不适用于 IDE 显示和支持。

Edit

编辑

For later reference.

供以后参考。

Currently, my opinion is changed to the Xcode projectis not completely able to deal with multiple platforms. Though it can display multiple platformsbut only one platform can be chosen at one time to screen display by the SDKROOTsetting. If you select iOS, it will use something like Debug-iphoneosfor build product path. So all Mac OS X targets will be missing. If you choose Mac OS X, it will use something like Debug. So all iOS targets' products will be missing.

目前,我的观点是修改为Xcode项目并不是完全能够处理多平台的。虽然可以显示多个平台,但通过SDKROOT设置一次只能选择一个平台进行屏幕显示。如果您选择iOS,它将使用类似的东西Debug-iphoneos来构建产品路径。因此,所有 Mac OS X 目标都将丢失。如果您选择Mac OS X,它将使用类似Debug. 所以所有iOS目标的产品都将丢失。

I think Xcode still have internal bug related this. It's long time to go to make Xcode to be stabilized.

我认为 Xcode 仍然存在与此相关的内部错误。要让Xcode稳定下来,还有很长的路要走。

回答by Diego Marafetti

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).

我和我的团队也有同样的问题。其中一位开发人员遇到了这个问题,但是我的 xcode 能够正确编译并找到标头。顺便说一句:所有“构建设置”都已正确配置(始终搜索用户路径、用户标题路径等)。

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.

我意识到他的项目位于一个路径中有空格的目录中(../my project/blah.xcodeproj)。改变这一点,Xcode 能够在同一工作区中从静态库中找到头文件。

Just be aware of directory names. My two cents

请注意目录名称。我的两分钱

回答by Tim Keating

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/Debugand your parent project is looking in Build/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.

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/Debugand your parent project is looking in Build/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.

回答by melissa

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.

我的一个图书馆遇到了这个问题。我实际上还有 4 个我构建的其他库,它们看起来不错,看起来是黑色的,但一个没有。我的红色库是通过更改库项目的Build Settings中的Base SDK解决的。由于库可以为 Mac OS X 和 iOS 构建,因此它被设置为 Mac OS X 设置。iOS 目标库仍在构建,但从未变黑。一旦我将 Base SDK 构建设置更改为最新的 iOS,我的库就变成了黑色。