如何解决为 iOS 编译 C++ 源代码
过去 2 周我一直在为 ios 平台编译一个开源 C++ 库。
图书馆链接: https://github.com/Amulet-Team/leveldb-mcpe
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)
mkdir -p build && cd build
cmake -DCMAKE_BUILD_TYPE=发布 .. && 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()
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.3.sdk/usr/lib/libz.tbd(找到版本“1.2.11”)
它找不到我上面提到的架构的符号。这一定是因为它正在寻找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 .自定义构建设置:
3c。在目标 leveldb
的构建设置中,更改 Mach-O 类型 和其他链接器标志:
- 按下播放按钮
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。