Search code examples
c++iosstatic-librariesdynamic-library

Compiling C++ source for iOS


I have been stuck at compiling an open source c++ library for ios platform for the past 2 weeks.

Library link : https://github.com/Amulet-Team/leveldb-mcpe

Below is the contents of the CMakeList.txt file.

cmake_minimum_required(VERSION 3.2)

if (WIN32)
# set windows 7 as the minimum version
add_definitions(-D_WIN32_WINNT=0x0601)
endif()

project(leveldb)

include(CheckCXXCompilerFlag)

list(APPEND SOURCES db/builder.cc)
list(APPEND SOURCES db/c.cc)
list(APPEND SOURCES db/db_impl.cc)
list(APPEND SOURCES db/db_iter.cc)
list(APPEND SOURCES db/dbformat.cc)
list(APPEND SOURCES db/filename.cc)
list(APPEND SOURCES db/log_reader.cc)
list(APPEND SOURCES db/log_writer.cc)
list(APPEND SOURCES db/memtable.cc)
list(APPEND SOURCES db/repair.cc)
list(APPEND SOURCES db/table_cache.cc)
list(APPEND SOURCES db/version_edit.cc)
list(APPEND SOURCES db/version_set.cc)
list(APPEND SOURCES db/write_batch.cc)
list(APPEND SOURCES table/block.cc)
list(APPEND SOURCES table/block_builder.cc)
list(APPEND SOURCES table/filter_block.cc)
list(APPEND SOURCES table/format.cc)
list(APPEND SOURCES table/iterator.cc)
list(APPEND SOURCES table/merger.cc)
list(APPEND SOURCES table/table.cc)
list(APPEND SOURCES table/table_builder.cc)
list(APPEND SOURCES table/two_level_iterator.cc)
list(APPEND SOURCES util/arena.cc)
list(APPEND SOURCES util/bloom.cc)
list(APPEND SOURCES util/cache.cc)
list(APPEND SOURCES util/coding.cc)
list(APPEND SOURCES util/comparator.cc)
list(APPEND SOURCES util/crc32c.cc)
list(APPEND SOURCES util/env.cc)
list(APPEND SOURCES util/filter_policy.cc)
list(APPEND SOURCES util/hash.cc)
list(APPEND SOURCES util/histogram.cc)
list(APPEND SOURCES util/logging.cc)
list(APPEND SOURCES util/options.cc)
list(APPEND SOURCES util/status.cc)
list(APPEND SOURCES db/zlib_compressor.cc)
list(APPEND SOURCES db/zstd_compressor.cc)
list(APPEND SOURCES port/port_posix_sse.cc)
include_directories(. include)

if (UNIX)
  list(APPEND SOURCES port/port_posix.cc)
  list(APPEND SOURCES util/env_posix.cc)

  add_definitions(-DLEVELDB_PLATFORM_POSIX "-DDLLX=")
  if(APPLE)
    add_definitions(-DOS_MACOSX)
  endif()

  CHECK_CXX_COMPILER_FLAG("-std=c++11" COMPILER_SUPPORTS_CXX11)

  if(COMPILER_SUPPORTS_CXX11)
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
  else()
    message(FATAL_ERROR "The compiler ${CMAKE_CXX_COMPILER} has no C++11 support. Please use a different C++ compiler.")
  endif()

elseif (WIN32)
  list(APPEND SOURCES port/port_win.cc)
  list(APPEND SOURCES util/env_win.cc)
  list(APPEND SOURCES util/win_logger.cc)
  add_definitions(-DLEVELDB_PLATFORM_WINDOWS "-DDLLX=__declspec(dllexport)")
endif()

add_library(leveldb SHARED ${SOURCES})

find_package(ZLIB REQUIRED)
if (ZLIB_FOUND)
  include_directories( ${ZLIB_INCLUDE_DIRS} )
  target_link_libraries( leveldb ${ZLIB_LIBRARIES} )
endif(ZLIB_FOUND)

I used the following on the command line to generate a dylib file.

mkdir -p build && cd build

cmake -DCMAKE_BUILD_TYPE=Release .. && cmake --build .

Snapshot of running the above commands on terminal.

enter image description here

The dylib generated is for MacOS. I want to do the same for iOS. I tried changing a part of the above CMakeList.txt as shown below :

if(COMPILER_SUPPORTS_CXX11)
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -arch armv6 -arch armv7 -arch armv7s -arch arm64")
else()
  message(FATAL_ERROR "The compiler ${CMAKE_CXX_COMPILER} has no C++11 support. Please use a different C++ compiler.")
endif()

But it still searches for the libz.tbd file at the following path

/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.3.sdk/usr/lib/libz.tbd (found version "1.2.11")

and I get the following error at the time of linking:

enter image description here

It could not find the symbols for the architectures I mentioned above. This must be because it is searching for the zlib library file under MacOS SDK.

How do I modify the CMakeList.txt file so that it compiles for iOS? It would be of great help if anyone could take a look at the library and let me know whether it will be possible to build it for iOS at all. I have asked the same question to the developer of the library on discord but he said that he doesn't have an apple device to confirm that. Someone please help me out.


Solution

  • 1 . Install cmake

    brew install cmake
    

    2 . Generate Xcode project (being in a root of the cloned leveldb working copy):

    mkdir out && cd out && cmake -G Xcode .. && open leveldb.xcodeproj
    

    3 . Customize build settings:

    3a. Open project build settings: enter image description here

    3b. Change Supported Platforms, Base SDK and Architecture: enter image description here

    3c. In build settings of the target leveldb, Change Mach-O Type and Other Linker Flags: enter image description here

    3d. Change Signing settings (for me setting Development Team was enough): enter image description here

    4 . Select the Build Configuration that you want to build with: enter image description here enter image description here

    1. Press Play button