构建 ceres-solver 作为 Mac Catalyst 的静态库

问题描述

我正在尝试在最新的 Apple Silicon M1 芯片上为我们的 iOS 应用添加测试。我们的应用程序对第​​ 3 方库的依赖之一是对 Ceres-Solver。我已经尝试了几天来为最新平台编译 ceres,但我所有的尝试都失败了。

所以我们使用 CMake 生成构建文件,然后我尝试使用 Xcode 和 xcodebuild 进行编译。构建成功,但每当我尝试将 libceres.a链接到我们的应用程序时,我都会收到:

Building for Mac Catalyst,but the linked library 'libceresMacOS.a' was built for macOS. You may need to restrict the platforms for which this library should be linked in the target editor,or replace it with an XCFramework that supports both platforms.

我觉得这很奇怪,因为我确实在 Xcode 中构建,并且在编译 ceres 和我们的应用程序时我的目标是相同的平台(“我的 Mac”)。我的一个怀疑是我在 CMake 命令中设置了一些错误的标志,这就是我正在运行它

cmake $fileDir/ceres-solver \
  -G"$GENERATOR_NAME" \
  -DCMAKE_BUILD_TYPE=Release \
  -DENABLE_BITCODE=OFF \
  -DENABLE_ARC=OFF \
  -DCMAKE_INSTALL_PREFIX=$fileDir/libs-macos \
  -DCMAKE_EXPORT_NO_PACKAGE_REGISTRY=TRUE \
  -DEIGENSPARSE=ON \
  -DEIGEN_INCLUDE_DIR_HINTS=$fileDir/eigen-git-mirror \
  -DEIGEN_INCLUDE_DIR=$fileDir/eigen-git-mirror \
  -DMINIGLOG=ON \
  -DCXX11_THREADS=ON \
  -DOPENMP=OFF \
  -DTBB=OFF

我尝试为新的苹果目标添加标志: -DCMAKE_C_FLAGS="-target x86_64-apple-iOS13.0-macabi" 但这没有任何效果,因为当我检查输出时,我看到使用 -target arm64-apple-macos11.1 调用了 clang。 而且我在构建设置过程中也遇到错误

 building for macOS-arm64 but attempting to link with file built for unkNown-x86_64
    Undefined symbols for architecture arm64:
      "_main",referenced from:
         implicit entry/start for main executable
    ld: symbol(s) not found for architecture arm64

我也试过 xcodebuild -project "Ceres.xcodeproj" -configuration "Release" -scheme "ceres" -destination "platform=macOS,variant=Mac Catalyst" 但这给了我一个错误

xcodebuild: error: Unable to find a destination matching the provided destination specifier:
        { platform:macOS,variant:Mac Catalyst }

    Available destinations for the "ceres" scheme:
        { platform:macOS,arch:arm64,id:00008103-001419A0029A001E }

    Ineligible destinations for the "ceres" scheme:
        { platform:macOS,name:Any Mac }

所以我在这里的想法不多了,如果有人能帮助我,我将不胜感激。 顺便提一下,我在 macOS Big Sur 11.1 上使用 Xcode 12.4 版

非常感谢

解决方法

经过更多研究后,我想出了如何进行这项工作,以防有人偶然发现同样的问题。 我最终阅读了这个问题描述 (link) 并让我觉得我应该尝试使用不同的代码生成器。 我的 cmake 配置是正确的,但显然使用 XCode 作为代码生成器存在错误(此处为 -G"$GENERATOR_NAME" )。设置 GENERATOR_NAME=Ninja 后,我设法编译了一个适用于 Mac Catalyst 的库版本。