Xcode does not find C++ static library

2019-05-23 02:09发布

问题:

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 !

回答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.