1

Objective-C - is a language belonging to C group languages.

In that case, I can use any C++ libraries in my iOS project, am I right?

How can I do that? Any suggestions?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Arthur
  • 1,740
  • 3
  • 16
  • 36
  • the `Objective-C` is a thin layer above the `ANSI C`, the `C++` is on different branch... but according to your logic the `C#` code should run in `Xcode` because there a letter `C` in its name... – holex Apr 09 '13 at 11:40

2 Answers2

3
  • If you wish to integrate the source code of the library into your application, then your question is indeed a duplicate of How can I use C++ with Objective-C in XCode as mentionned by tilo

  • If you wish to use the built library (for instance a .so file), you need to:

    1. Create your Xcode project
    2. Include the library in your resources
    3. Add the headers to your project
    4. Load the library at runtime (see code below)

.

    // 1. Load library
    void *lib = dlopen("path/to/you/lib.so", RTLD_LAZY);

    // 2. Handle errors (load failed)
    if (!lib) 
    {
        fprintf(stderr, "dlopen(): %s\n", dlerror());
        exit(EXIT_FAILURE);
    }

    // 3. Get lib functions addresses
    any_library_function = (void *)GetProcAddress(lib, "any_library_function");

    // 4. Use the functions
    any_library_function(…);

Note: You may need to rename the source file in which you use that code from .m to .mm. Best is to do that directly in Xcode after the file is created.

Community
  • 1
  • 1
Jean
  • 7,623
  • 6
  • 43
  • 58
0

Yes, you can. Either your C++ library exposes bindings with C linkage that you can use directly in Objective-C, or you can use Objective-C++ to access the C++ bindings.