为 iOS 编译 C++ 源代码

问题描述

过去 2 周我一直在为 ios 平台编译一个开源 C++ 库。

图书馆链接https://github.com/Amulet-Team/leveldb-mcpe

以下是 CMakeList.txt 文件内容

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)

我在命令行中使用了以下内容生成一个 dylib 文件

mkdir -p build && cd build

cmake -DCMAKE_BUILD_TYPE=发布 .. && cmake --build .

在终端上运行上述命令的快照。

enter image description here

生成的 dylib 适用于 MacOS。我想为 iOS 做同样的事情。我尝试更改上述 CMakeList.txt 的一部分,如下所示:

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()

但它仍然在以下路径搜索libz.tbd文件

/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.3.sdk/usr/lib/libz.tbd(找到版本“1.2.11”)

并且我在链接时收到以下错误

enter image description here

它找不到我上面提到的架构的符号。这一定是因为它正在寻找MacOS SDK下的zlib库文件

如何修改 CMakeList.txt 文件以便它为 iOS 编译?如果有人可以查看该库并让我知道是否有可能为 iOS 构建它,那将是非常有帮助的。我在 discord 上向图书馆的开发人员提出了同样的问题,但他说他没有苹果设备来确认这一点。有人请帮帮我。

解决方法

1。安装 cmake

brew install cmake

2。生成 Xcode 项目(位于克隆的 leveldb 工作副本的根目录中):

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

3 .自定义构建设置:

3a。打开项目构建设置: enter image description here

3b。更改支持的平台基础 SDK架构enter image description here

3c。在目标 leveldb 的构建设置中,更改 Mach-O 类型其他链接器标志enter image description here

3d。更改签名设置(对我来说设置开发团队就足够了): enter image description here

4。选择要使用的构建配置: enter image description here enter image description here

  1. 按下播放按钮