为 iOS 编译 C++ 源代码
Compiling C++ source for iOS
过去 2 周我一直在为 ios 平台编译开源 C++ 库。
图书馆link:
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=Release .. && cmake --build .
终端上运行上述命令的快照。
生成的 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 (found version "1.2.11")
我在 linking 时收到以下错误:
它找不到我上面提到的体系结构的符号。这一定是因为它正在寻找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。打开项目构建设置:
3b。更改支持的平台、Base SDK 和架构:
3c。在目标 leveldb
的构建设置中,更改 Mach-O Type 和 Other Linker Flags:
3天。更改 Signing 设置(对我来说设置 Development Team 就足够了):
4。 Select 您要构建的构建配置:
- 按播放按钮
过去 2 周我一直在为 ios 平台编译开源 C++ 库。
图书馆link: 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=Release .. && cmake --build .
终端上运行上述命令的快照。
生成的 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 (found version "1.2.11")
我在 linking 时收到以下错误:
它找不到我上面提到的体系结构的符号。这一定是因为它正在寻找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。打开项目构建设置:
3b。更改支持的平台、Base SDK 和架构:
3c。在目标 leveldb
的构建设置中,更改 Mach-O Type 和 Other Linker Flags:
3天。更改 Signing 设置(对我来说设置 Development Team 就足够了):
4。 Select 您要构建的构建配置:
- 按播放按钮