Use of external C++ headers in Objective-C
Solution 1
#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.
Solution 2
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 may automatically 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 Flags build setting.-
Import the header in your files as follows:
#import <ThirdParty/third_party.hh>
Solution 3
In Xcode 9, I need to add header files path to the Header Search Paths
build setting, not 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.
Related videos on Youtube
meaning-matters
Should software be simple, usable, easy to understand, ...? I believe there's one unifying force that matters most: meaning. When focussing on what is most meaningful for users, properties like simplicity and usability will follow naturally. This principle can be applied to all levels: For example, from arranging code statements, to presenting data. It's my guide that has led to great results for already more than two decades. Cornelis
Updated on July 12, 2022Comments
-
meaning-matters over 1 year
In my iOS project I need to use an external library written in C++. The C++ header files are all in one directory.
I've added these C++ headers to my Xcode project, and also specified a header search path (in Build Settings).
The issue is that these C++ headers include each other using < > angle brackets. This results in:
'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.
- Why doesn't the search path work?
- Is there a way to resolve this without modifying these header files?
Thanks!
-
CouchDeveloper over 10 yearsDo you have the sources and the Xcode project of that C++ library?
-
meaning-matters over 10 years@CouchDeveloper No I don't.
-
Martin R over 10 yearsDoes setting the "Always Search User Paths" build setting help?
-
meaning-matters over 10 years@MartinR Holy smokes, yes! :-) Put it as answer and I will reward. I've removed the
-I
, nice. Why did-I
not work?
-
CouchDeveloper over 10 yearsNote that setting "Always Search User Paths" to YES may cause header issues. It's strongly recommended to set it to NO, and that's the correct way. It's still only there for backward compatibilities. The third party library's headers should be included via angle brackets.
-
Martin R over 10 years@CouchDeveloper: It was meant as a workaround, but you are right and your answer is problably the better solution.
-
Thibault D. almost 10 yearsThat helped, since that error was target-dependent in my case.
-
aledalgrande about 9 yearsEpic, first time I can make the angle includes work with a 3rd party library that I have in my project because I want to patch it. Thanks.
-
Chris Holloway over 8 years@CouchDeveloper agree that the this setting should be set to NO, but is there anything undesirable setting this setting to YES in the context of building an embedded framework?
-
meaning-matters almost 6 yearsThanks for pointing this out. (I had not time to check it though, I trust you.)