简体   繁体   中英

How to include C++ library in Xcode

I am migrating an Android application, which uses a C++ library to iOS. C++ library provides main application functionality to the app. Is it possible to include a C++ library in XCode project?

I know that Apple LLVM compiler can compile C code, so a possible solution may be to compile C++ code into a shared library and then provide a C wrapper to be able to access C++ code from Objective-C. Similar solution was adopted with Android app and JNI.

Yes, you can.

There are two ways of doing this.

The easiest way would be to just add the sources inside your iOS project. If you have the sources of the library , then you can add it to your project, just make sure to have your main file (and other objective C files) named from .m to .mm to let XCode now that you are compiling for C++.

If you want to go for static linkage, I strongly recommend to create a static library project and add it to the same workspace as your Objective C main project , then declare it as dependency for your build target (check build targets / build schemas ... XCode stuff over here :) ).

If you just want to provide a static library binary and just go with static linkage from there (which I really don't recommend , no breakpoints and some other pain along the way) then you have to keep in mind the following :

  • you will need to have a separately compiled static library for each different platform (iOS simulator which is different from iPhone, arm64 , armv7 and armv7s ) and link accordingly
  • an alternative to the above way is to make a fat lib (universal lib)

Look in the "Link Binary With Libraries" build phase under your target. Right click on it, and select "Existing Files..." -- then select the library you wish to link to. You don't have to use a framework as implied earlier.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM