Swemoph
Swemoph

Reputation: 326

Compiling dylib file for iOS with CMake

I have a C++ project in the CLion IDE that I worked on a few months back. I never thought much of it, but as it turns out the code is useful for an iOS app I'm currently working on in Objective-C. I want to use Objective-C++ to interface with the C++ classes instead of recoding everything in Obj-C.

I have compiled my code from CLion into a .dylib file using add_library(CalcOS SHARED ${SOURCE_FILES}) in cmake. This is where i'm encountering issues as I get the following warning from Xcode:

URGENT: building for iOS simulator, but linking against dylib (/Users/me/Documents/old/Developer/Calc/Calc/libCalcOS.dylib) built for OSX. Note: This will be an error in the future.

Building the application and running it results in a crash on app launch with the following error:

dyld: Library not loaded: @rpath/libCalcOS.dylib Referenced from: /Users/me/Library/Developer/CoreSimulator/Devices/80285643-3064-4F0A-B921-90060F6A998F/data/Containers/Bundle/Application/B6BAAB1D-ABF4-4D0D-82CD-CFD243DEE622/Calc.app/Calc Reason: image not found

I'm lead to believe that I should compile the .dylib file for arm64 devices in order for it to work with iOS, however I am not sure how to accomplish this in CLion or CMake. Any pointers would be appreciated!

Upvotes: 1

Views: 2041

Answers (1)

Cinder Biscuits
Cinder Biscuits

Reputation: 5241

Download this toolchain: https://github.com/cristeab/ios-cmake

In your library's CMakeLists.txt, set the following to build for arm7:

SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -arch armv7")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -arch armv7")

Then you can use it by running:

cmake -DCMAKE_TOOLCHAIN_FILE=path_to_the_toolchain

Upvotes: 1

Related Questions