2

So I had some issues with this before but I deleted my old question and updated it to this one. Here it goes:

I want to use a C++ Library in my iOS Swift project. With the help of this community I compiled my library as a static library (.a file). Now I created a new project to test this and I did only the following steps :

1. Create new Swift Project

2. Under Build Phases -> Link Binary with Libraries add my library (like so)

3. Add a new C++ File without a header file and add a bridging header with following contents :

//Wrapper.cpp

extern "C" void test()
{
    //Just to test the swift - c++ connection later this file 
    //shall import the main header of the library.
}

//CAS Test-Bridging-Header.h

void test();

But now, when I compile I get this error (you can also see my project hierarchy on the left).

Thanks for all your help in advance !

Jony
  • 53
  • 10

1 Answers1

1

To make Xcode find the library you have at least two options:

  1. Set Library Search Paths under Build Settings. This may be messy because you have to make sure that you build the library for the right architecture, e.g. simulator or actual device, and use the appropriate path.

  2. Create a new workspace, add both the library project and the app project to the workspace. In Build Phases for the app target select the static library (.a) file in the dialog that pops up after you click the + sign in the Link Binary with Libraries section. Xcode will build the library for the right architecture and link the app with the correct static lib.

Now, building the library for the correct architecture may be tricky, regardless of whether you choose option 1 or 2 above. A few things to try:

  • Create an External Build System project in Xcode and use the makefile that came with the library source. (File -> New -> Project... -> Cross-platform.)
  • Create a Cocoa Touch Static Library project in Xcode. (File -> New -> Project... -> iOS.) You will need to add the library sources to the project. This is probably your best bet if the library's build is not too complex.

There is also the option to include C++ library sources directly into your Swift project.

See if this post appears helpful: Compiling external C++ library for use with iOS project.

Community
  • 1
  • 1
Anatoli P
  • 4,791
  • 1
  • 18
  • 22
  • 1
    Looking at your other question and the answer you got, I suspect that you may see some undefined symbol errors once you fix Library Search Paths. That will happen if you built the static library for an architecture that doesn't match the one you are building your iOS app for. – Anatoli P Mar 21 '17 at 02:28
  • Thanks for your answer ! I did not test it jet but I will right away. About your comment : How do I fix that ? I built the library using the OS X command line. Will building the library using Xcode fix the issue ? – Jony Mar 21 '17 at 09:56
  • Oh my bad ! Just read your second point again ! I know it is a lot to ask for but could you clarify what i have to do in your second point ? Thank you so much =) – Jony Mar 21 '17 at 10:01
  • I created a new workspace and added my app .cxodeproj. What to do next ? Thanks a lot in advance ;) – Jony Mar 21 '17 at 10:24
  • 1
    You need to add both .xcodeproj files (the library's and your app's) to the workspace. Of course, you will need to create the project for your lib first, which is the tricky part. I've expanded the answer. Please ask if you run into specific problems. – Anatoli P Mar 22 '17 at 01:17
  • Thanks a lot for your answer =) Right of the bat - I will play around with that fornow but the library I am trying to use relies on another one. DO you have an idea of how to implement that ? – Jony Mar 22 '17 at 15:10
  • So I "played" around with this for the last 5 hours =) Now I figured out I will build the first library (the on that the second depends on) as a static .a file and use that to build the second library as .a file to use that in my project. Sooooo I made a new cocoa touch static library and added all the C++ Source code. Now when I compile I get some strange errors - I added them to my question. Is that approach a good idea ? What about those errors - no idea what they mean XD – Jony Mar 22 '17 at 22:39
  • 1
    I think this is getting much more complicated than just using a static library in an iOS project written in Swift. You may want to create a simple C++ library of your own and experiment building it and using it in Swift. The real-world libraries you are trying to use appear to have tricky inter-dependencies and some assembly code. I would start asking separate questions whenever new problems arise. – Anatoli P Mar 23 '17 at 02:56