meaning-matters
meaning-matters

Reputation: 22936

Use of external C++ headers in Objective-C

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.

  1. Why doesn't the search path work?
  2. Is there a way to resolve this without modifying these header files?

Thanks!

Upvotes: 28

Views: 39597

Answers (5)

ingconti
ingconti

Reputation: 11636

my two cents for OSX / Mysql. (by the way I ask why that bogus use of <> in mysql... anyway..)

As per Xcode 11 warning, "Disabling it is strongly recommended.",

I prefer to patch another setting, leaving "Always Search User Paths" to "No".

I set:

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)

II: You can get a worst error...

"/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"

As that lib is not signed. Simply in Entitlemens:

(in XML): ..

<dict>
    <key>com.apple.security.cs.disable-library-validation</key>
    <true/>
</dict>
...

enter image description here

Upvotes: 0

Piasy
Piasy

Reputation: 1009

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.

Upvotes: 6

CouchDeveloper
CouchDeveloper

Reputation: 19098

Starting from a "blank" application project:

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

  2. 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            
    
  3. "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.

  4. "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.

  5. Link your app against the C++ standard library by adding -lc++ to the Other Linker Flags build setting.

  6. Import the header in your files as follows:

     #import <ThirdParty/third_party.hh>
    

Upvotes: 15

Donovanr
Donovanr

Reputation: 91

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"

This solved the problem I was having: with <boost/signals2.hpp> file not found with <angled> include, use "quotes" instead.

Upvotes: 0

Martin R
Martin R

Reputation: 539685

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

Upvotes: 44

Related Questions