0

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.

5
  • 1
    What does "nothing works" mean? Commented Jul 23, 2021 at 4:32
  • @RetiredNinja Please have a look at this : github.com/Mojang/leveldb-mcpe/issues/42#issue-945064795. I was trying to build the library from Mojang LevelDB project and though it generated the static library it gave undefined symbol error when it was added to xcode. I have opened an issue on their github project. Then I found out Amulet Team's LevelDB project forked from Mojang. The same is happening with this. Can u guide me on how I should create a static library from the Amulet Team's LevelDB project? I have very less experience using makefile. Commented Jul 23, 2021 at 10:33
  • With cmake you can generate xcode project. Then, try adding -lz to OTHER_LDFLAGS. Commented Jul 24, 2021 at 6:09
  • @EugeneDudnyk I don't have any experience with CMake. I used a CMake toolchain for iOS from github.com/leetal/ios-cmake to generate an xcode project. But it seems to have Signing errors while generating the project. So the library never gets genarated. Can u help me with getting the library compiled for ios? Maybe I can connect with you on discord or email. Commented Jul 26, 2021 at 5:21
  • Have you tried make TARGET_OS=IOS? Commented Jul 26, 2021 at 7:43

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you so much. I was able to build the dynamic library. Now I have copied the dylib file and have added it to my xcode project. Can u guide me on whether I should add a copy phase to copy the library to Product or Framework folder? Also what Run search path should I provide so that the library gets loaded by the dynamic loader.
Another thing, would it be possible for you to connect with me through some online platform so that I can learn a few things regarding compiling dynamic libraries and also using them in other xcode project (especially the linking part). Again, I am really grateful for your help.
Sorry, I don't have a spare time for this. Yes, you should copy dylib into Frameworks folder. By default, iOS app has LD_RUNPATH_SEARCH_PATHS set to Frameworks folder, so there shouldn't be any problem with that. If in doubt, create a new project from Framework template and verify runpath-related build settings there, and replicate them in leveldb target, keeping in mind that dylib will be located in the app payload directly in Frameworks/ folder, the template framework's dynamic library is located in Frameworks/<Name>.framework/ folder.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.