在 iOS 中导入 zlib 时出错:找不到符号 collect2: ld

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

error when import zlib in iOS: symbol(s) not found collect2: ld

ioslinkerzlib

提问by issac

I have included <zlib.h>in my iphone application and the source code I was mocking up the sample code of Molecules provided by Brad Larson, however, when I build the project, it returns the error as below. Can anyone point out for me whether this is a library linking problem or am I missing something else?

我已经<zlib.h>在我的 iphone 应用程序和源代码中包含了我模拟 Brad Larson 提供的 Molecules 示例代码,但是,当我构建项目时,它返回如下错误。谁能为我指出这是一个库链接问题还是我错过了其他东西?

"_deflate", referenced from: -[NSData(Gzip) gzipDeflate] in NSData+Gzip.o "_inflateEnd", 
referenced from: -[NSData(Gzip) initWithGzippedData:] in NSData+Gzip.o "inflateInit2", 
referenced from: -[NSData(Gzip) initWithGzippedData:] in NSData+Gzip.o "_inflate", 
referenced from: -[NSData(Gzip) initWithGzippedData:] in NSData+Gzip.o "_deflateEnd", 
referenced from: -[NSData(Gzip) gzipDeflate] in NSData+Gzip.o "deflateInit2", referenced 
from: -[NSData(Gzip) gzipDeflate] in NSData+Gzip.o ld: symbol(s) not found collect2: ld 
returned 1 exit status

回答by Ben Gottlieb

In your Target's Build Settingstab, scroll down to the Other Linker Flagssection and make sure -lzis in the field. This will link against the built-in zlib, and your error should go away.

在您的目标Build Settings选项卡中,向下滚动到该Other Linker Flags部分并确保-lz位于该字段中。这将链接到内置 zlib,您的错误应该会消失。

After changing the Linker Flags you must select Cleanfrom the Productmenu before building again.

更改链接器标志后,您必须在再次构建之前CleanProduct菜单中进行选择。

lz screenshot

lz截图

回答by Jason Coco

Add libz to your project. To do this, follow these steps in Xcode:

将 libz 添加到您的项目中。为此,请在 Xcode 中执行以下步骤:

  1. Open your project, select your project target and then click the blue project info icon on your toolbar (or press ?I)
    Example 1
  2. Click the + button in the lower-left corner of the screen to add a library, scroll down to the bottom of the library list and select libz.dylib; click the Add button.
    Example 2
  3. Once you've clicked add, you will see that the library name appears in oyur list of Linked Libraries. You will now be able to build your project without linking errors.
    Example 3
  1. 打开您的项目,选择您的项目目标,然后单击工具栏上的蓝色项目信息图标(或按 ?I)
    示例 1
  2. 点击屏幕左下角的+按钮添加库,向下滚动到库列表底部,选择libz.dylib;单击添加按钮。
    示例 2
  3. 单击添加后,您将看到库名称出现在您的链接库列表中。您现在可以在不链接错误的情况下构建您的项目。
    示例 3

You can use this same method to add any library from the list. If the library does not appear on the list, then you know that it is not part of the standard iPhone SDK and you may need to rethink your solution or build the library statically yourself and link your target to that (if even possible).

您可以使用相同的方法从列表中添加任何库。如果该库没有出现在列表中,那么您就知道它不是标准 iPhone SDK 的一部分,您可能需要重新考虑您的解决方案或自己静态构建库并将您的目标链接到该库(如果可能的话)。