Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 311
meaning-matters Avatar asked Jul 04 '13 08:07

meaning-matters


People also ask

What is the purpose of C header files?

A header file is a file containing C declarations and macro definitions (see Macros) to be shared between several source files. You request the use of a header file in your program by including it, with the C preprocessing directive ' #include '.

What is extern Objective-C?

Keyword extern is used for declaring extern variables in Objective-C. This modifier is used with all data types like int, float, double, array, pointer, function etc. Basically extern keyword extends the visibility of the C variables and C functions. Probably that's is the reason why it was named as extern.

Can we create external C header file in C?

So the question arises, is it possible to create your own header file? The answer to the above is yes. header files are simply files in which you can declare your own functions that you can use in your main program or these can be used while writing large C programs.

Do you need to compile header files C?

You don't need to compile header files. It doesn't actually do anything, so there's no point in trying to run it. However, it is a great way to check for typos and mistakes and bugs, so it'll be easier later.

What is a header file in Objective-C?

Header Files in Objective-C. The header file (with the file extension .h) defines the class and everything about it for the world to know. The idea is to separate the definition of a class from its nitty gritty implementation details. In this case, the header file acts as the interface to the rest of your program.

What are the types of header files in C++?

There are of 2 types of header file: 1 Pre-existing header files: Files which are already available in C/C++ compiler we just need to import them. 2 User-defined header files: These files are defined by the user and can be imported using “ 3 include”. More ...

What are classes and objects in Objective C?

Classes and Objects in Objective-C. In general, files of code in Objective-C are organized as classes. Classes are usually represented by two files: a header file and a corresponding implementation file. A class is meant to define an object and how it works.

What happens if a header file is included twice in C?

When a header file is included twice within a program, the compiler processes the contents of that header file twice. This leads to an error in the program. To eliminate this error, conditional preprocessor directives are used. This construct is called wrapper “#ifndef”.


2 Answers

#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

  • What is the difference between #include <filename> and #include "filename"?

As a workaround, you can set the Xcode build setting "Always Search User Paths" to YES.

like image 140
Martin R Avatar answered Oct 01 '22 10:10

Martin R


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>
    
like image 21
CouchDeveloper Avatar answered Oct 01 '22 10:10

CouchDeveloper