如何在 Xcode 中包含 C++ 库

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

How to include C++ library in Xcode

c++iosxcode

提问by Dan Tumaykin

I am migrating an Android application, which uses a C++ library to iOS. C++ library provides main application functionality to the app. Is it possible to include a C++ library in XCode project?

我正在将使用 C++ 库的 Android 应用程序迁移到 iOS。C++ 库为应用程序提供主要的应用程序功能。是否可以在 XCode 项目中包含 C++ 库?

I know that Apple LLVM compiler can compile C code, so a possible solution may be to compile C++ code into a shared library and then provide a C wrapper to be able to access C++ code from Objective-C. Similar solution was adopted with Android app and JNI.

我知道 Apple LLVM 编译器可以编译 C 代码,因此可能的解决方案是将 C++ 代码编译到共享库中,然后提供一个 C 包装器,以便能够从 Objective-C 访问 C++ 代码。Android 应用程序和 JNI 也采用了类似的解决方案。

回答by MichaelCMS

Yes, you can.

是的你可以。

There are two ways of doing this.

有两种方法可以做到这一点。

The easiest way would be to just add the sources inside your iOS project. If you have the sources of the library , then you can add it to your project, just make sure to have your main file (and other objective C files) named from .m to .mm to let XCode now that you are compiling for C++.

最简单的方法是在你的 iOS 项目中添加源代码。如果您有库的源代码,那么您可以将其添加到您的项目中,只需确保您的主文件(和其他目标 C 文件)从 .m 命名为 .mm 以让 XCode 现在您正在为 C++ 编译.

If you want to go for static linkage, I strongly recommend to create a static library project and add it to the same workspace as your Objective C main project , then declare it as dependency for your build target (check build targets / build schemas ... XCode stuff over here :) ).

如果您想进行静态链接,我强烈建议您创建一个静态库项目并将其添加到与您的 Objective C 主项目相同的工作区,然后将其声明为您的构建目标的依赖项(检查构建目标/构建模式.. . XCode 的东西在这里 :) )。

If you just want to provide a static library binary and just go with static linkage from there (which I really don't recommend , no breakpoints and some other pain along the way) then you have to keep in mind the following :

如果您只想提供一个静态库二进制文件,并从那里开始使用静态链接(我真的不推荐这样做,没有断点和其他一些痛苦),那么您必须记住以下几点:

  • you will need to have a separately compiled static library for each different platform (iOS simulator which is different from iPhone, arm64 , armv7 and armv7s ) and link accordingly
  • an alternative to the above way is to make a fat lib (universal lib)
  • 您需要为每个不同的平台(不同于 iPhone、arm64、armv7 和 armv7s 的 iOS 模拟器)分别编译一个静态库并进行相应的链接
  • 上述方法的替代方法是制作一个胖库(通用库)

回答by Nagarjun

Look in the "Link Binary With Libraries" build phase under your target. Right click on it, and select "Existing Files..." -- then select the library you wish to link to. You don't have to use a framework as implied earlier.

查看目标下的“Link Binary With Libraries”构建阶段。右键单击它,然后选择“现有文件...”——然后选择要链接到的库。您不必使用之前暗示的框架。