在 Objective-C 中使用外部 C++ 头文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17465902/
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
Use of external C++ headers in Objective-C
提问by meaning-matters
In my iOS project I need to use an external library written in C++. The C++ header files are all in one directory.
在我的 iOS 项目中,我需要使用一个用 C++ 编写的外部库。C++ 头文件都在一个目录中。
I've added these C++ headers to my Xcode project, and also specified a header search path (in Build Settings).
我已经将这些 C++ 头文件添加到我的 Xcode 项目中,并且还指定了一个头文件搜索路径(在构建设置中)。
The issue is that these C++ headers include each other using < > angle brackets. This results in:
问题是这些 C++ 头文件使用 < > 尖括号相互包含。这导致:
'filename.h' file not found with <angled> include, use "quotes" instead.
The weird thing is that Xcode does not complain about all headers. Also the same header #include'd in one file is fine, while an issue when #include'd in another. I think this is caused by the fact that these headers #include each other.
奇怪的是 Xcode 并没有抱怨所有的标题。同样的标题#include'd 在一个文件中也很好,而在另一个文件中#include'd 时会出现问题。我认为这是由于这些标头相互#include 造成的。
- Why doesn't the search path work?
- Is there a way to resolve this without modifying these header files?
- 为什么搜索路径不起作用?
- 有没有办法在不修改这些头文件的情况下解决这个问题?
Thanks!
谢谢!
回答by Martin R
#include <bla.h>
is meant for standard library or framework headers, and the search strategy Is different than that used for
用于标准库或框架头文件,搜索策略与用于的不同
#include "bla.h"
See for example
见例如
As a workaround, you can set the Xcode build setting "Always Search User Paths" to YES.
作为一种解决方法,您可以将 Xcode 构建设置“始终搜索用户路径”设置为 YES。
回答by CouchDeveloper
Starting from a "blank" application project:
从“空白”应用程序项目开始:
Create a folder "Libraries" in your application's project - preferable as a sibling to your MyApp.xcodeproj file, but it can be anywhere. Create subfolders for each Configuration (Debug, Release, etc.) and possibly for each architecture (armv7, armv7s, arm64) unless the binary is universal binary archive containing all architectures.
Get the headers of the third party library and the static library binaries (possibly more than one for different platforms, Configurations and architectures) and move them into the "Library" folder into corresponding subfolders (which you may need to create):
For example, assuming you had a universal binary (armv7, armv7s, arm64) and Debug and Release versions of that library: Now, the folder structure is assumed to be as follows:
$(SRCROOT)/Libraries Debug-iphoneos include ThirdParty third_party.hh ... libThirdParty.a Release-iphoneos include ThirdParty third_party.hh ... libThirdParty.a MyApp.xcodeproj
"Library Search Paths" Build Setting:
Drag the "Libraries" folder into your Xcode project. This mayautomatically create a library search path in the build settings. Please verify this, and if it is not correct, fix it.
Given the example, add the following library search paths for Debug and Release Configuration:
Debug: Library Search Paths:
$(SRCROOT)/Libraries/Debug-iphoneos
Release: Library Search Paths:
$(SRCROOT)/Libraries/Release-iphoneos
You may have different library search paths for particular Configuration and Target platform pairs. Set different path's in the build setting accordingly.
"Header Search Paths" Build Setting:
Given the example, add the following header search path to the Debug and the Release Configuration:
Debug: Header Search Paths:
$(SRCROOT)/Libraries/Debug-iphoneos/include
Release: Header Search Paths:
$(SRCROOT)/Libraries/Release-iphoneos/include
Likewise, you may have different paths for particular Config/Target pairs - although the headers may be the same.
Link your app against the C++ standard library by adding
-lc++
to the Other Linker Flagsbuild setting.Import the header in your files as follows:
#import <ThirdParty/third_party.hh>
在您的应用程序项目中创建一个文件夹“Libraries” - 最好作为 MyApp.xcodeproj 文件的同级文件,但它可以在任何地方。为每个配置(调试、发布等)以及可能为每个架构(armv7、armv7s、arm64)创建子文件夹,除非二进制文件是包含所有架构的通用二进制存档。
获取第三方库的头文件和静态库二进制文件(对于不同的平台、配置和架构可能不止一个)并将它们移动到“Library”文件夹中的相应子文件夹(您可能需要创建):
例如,假设您有一个通用二进制文件(armv7、armv7s、arm64)以及该库的 Debug 和 Release 版本: 现在,假设文件夹结构如下:
$(SRCROOT)/Libraries Debug-iphoneos include ThirdParty third_party.hh ... libThirdParty.a Release-iphoneos include ThirdParty third_party.hh ... libThirdParty.a MyApp.xcodeproj
“库搜索路径”构建设置:
将“Libraries”文件夹拖到您的 Xcode 项目中。这可能会在构建设置中自动创建库搜索路径。请验证这一点,如果不正确,请修复它。
给定示例,为调试和发布配置添加以下库搜索路径:
调试: 库搜索路径:
$(SRCROOT)/Libraries/Debug-iphoneos
发布:图书馆搜索路径:
$(SRCROOT)/Libraries/Release-iphoneos
对于特定的配置和目标平台对,您可能有不同的库搜索路径。相应地在构建设置中设置不同的路径。
“标题搜索路径”构建设置:
给定示例,将以下标头搜索路径添加到调试和发布配置:
调试:标题搜索路径:
$(SRCROOT)/Libraries/Debug-iphoneos/include
发布:标题搜索路径:
$(SRCROOT)/Libraries/Release-iphoneos/include
同样,对于特定的 Config/Target 对,您可能有不同的路径 - 尽管标头可能相同。
通过添加
-lc++
到其他链接器标志构建设置,将您的应用与 C++ 标准库链接起来。按如下方式导入文件中的标头:
#import <ThirdParty/third_party.hh>
回答by Piasy
In Xcode 9, I need to add header files path to the Header Search Paths
build setting, not User Header Search Paths
.
在 Xcode 9 中,我需要将头文件路径添加到Header Search Paths
构建设置,而不是User Header Search Paths
.
Xcode will append User Header Search Paths
to compile command as -iquote
options, but append Header Search Paths
as -I
options. That's the key difference.
Xcode 将User Header Search Paths
作为-iquote
选项附加到编译命令,但Header Search Paths
作为-I
选项附加。这是关键的区别。
回答by ingconti
my two cents for OSX / Mysql. (by the way I ask why that bogus use of <> in mysql... anyway..)
我的 OSX/Mysql 两分钱。(顺便问一下,为什么在 mysql 中虚假使用 <> ......无论如何......)
As per Xcode 11 warning, "Disabling it is strongly recommended.",
根据 Xcode 11 警告,“强烈建议禁用它。”,
I prefer to patch another setting, leaving "Always Search User Paths" to "No".
我更喜欢修补另一个设置,将“始终搜索用户路径”保留为“否”。
I set:
我设置:
HEADER_SEARCH_PATHS = "/usr/local/mysql/include".
HEADER_SEARCH_PATHS = "/usr/local/mysql/include"。
LINKER:
链接器:
I)If You got link error, add "libmysqlclient.a" usually in "/usr/local/mysql/lib", simply dragging from Finder)
I)如果你有链接错误,通常在“/usr/local/mysql/lib”中添加“libmysqlclient.a”,只需从Finder中拖动即可)
II: You can get a worst error...
II:你可能会得到一个最严重的错误......
"/usr/local/lib/libmysqlclient.21.dylib: code signature in (/usr/local/lib/libmysqlclient.21.dylib) not valid for use in process using Library Validation: mapping process and mapped file (non-platform) have different Team IDs"
“/usr/local/lib/libmysqlclient.21.dylib:(/usr/local/lib/libmysqlclient.21.dylib) 中的代码签名在使用库验证的进程中无效:映射进程和映射文件(非平台) 有不同的团队 ID”
As that lib is not signed. Simply in Entitlemens:
由于该库未签名。仅在权利中:
(in XML): ..
(在 XML 中):..
<dict>
<key>com.apple.security.cs.disable-library-validation</key>
<true/>
</dict>
...
回答by Donovanr
In XCode after setting the "User Header Search Paths" to point to your library's directory, you also have to make sure that a field called "Always Search User Paths" is set to "Yes"
在 XCode 中将“用户标题搜索路径”设置为指向您的库目录后,您还必须确保将名为“始终搜索用户路径”的字段设置为“是”
This solved the problem I was having: with <boost/signals2.hpp> file not found with <angled> include, use "quotes" instead.
这解决了我遇到的问题:使用 <angled> 包含找不到 <boost/signals2.hpp> 文件,请改用“quotes”。