【问题标题】:Error to compile Xcode Swift project using C++ library linked on Build Phases使用 Build Phases 上链接的 C++ 库编译 Xcode Swift 项目时出错
【发布时间】:2018-12-06 11:42:44
【问题描述】:

我在网上搜索了很多,但没有找到解决我的问题的方法。 我的目标是在 C++ 中创建一个简单的类并在 Swift 上使用它。为此,我确实遵循了本教程-> http://www.swiftprogrammer.info/swift_call_cpp.html(非常好)。 基本上我已经按照以下步骤操作了:

  1. 创建junk.cppjunk.h 文件
  2. 使用g++ or/and clang++编译
  3. 创建 .a 文件:$ ar r libjunkcpp.a junk.o
  4. Build Phases -> Link Binary With Libraries -> Add 中链接到 Xcode

所以当我这样做时,Xcode 没有编译更多我的项目,在 Xcode 的左侧出现错误消息:

链接器命令失败,退出代码为 1(使用 -v 查看调用)

在日志上的错误消息是:

ld:存档没有目录文件 '/Users/augustosansoncadini/Documents/XcodeProjects/Ex_cpp_Swift_CmdLineTool/junk/libjunkcpp.a' 对于架构 x86_64

我认为需要编译到它的架构,但是当我输入 g++ -v 时,结果是:

Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk/usr/include/c++/4.2.1
Apple LLVM version 10.0.0 (clang-1000.11.45.5)
Target: x86_64-apple-darwin18.2.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

所以我的结论是架构目标是正确的。

我认为这张图片可以提供帮助:

注意: 当我从 Link Binary With Libraries 中删除 .a 文件时,代码编译得很好。

【问题讨论】:

    标签: c++ swift xcode g++ objective-c-swift-bridge


    【解决方案1】:

    您错过的是调用ranlib 命令来生成目录:

    $ ranlib libjunkcpp.a
    

    arranlib 命令必须始终配对。

    【讨论】:

    【解决方案2】:

    http://www.swiftprogrammer.info/swift_call_cpp.html 的同一教程结束时遇到了类似的问题。

    但是我在构建项目时遇到的错误是:

    ld: library not found for -ljunkcpp
    clang: error: linker command failed with exit code 1 (use -v to see invocation)
    

    不知道“ljunkcpp”命名(与“libjunkcpp”)突然来自哪里,但我解决它的方法是:

    1. 完全删除了libjunkcpp.a
    2. 再次使用g++ -c junk.cpp 编译。
    3. 再次运行ar r hellocpp.a junk.o(注意不同的hellocpp.a 文件名)。
    4. 删除了旧的构建阶段链接到libjunkcpp.a
    5. Link Binary With Libraries 中改为链接 hellocpp.a。重建并运行。
    Hello, World!
    The integer from C++ is 1234
    

    一定是名字冲突。希望这对某人有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-04-16
      • 1970-01-01
      • 2018-11-01
      • 1970-01-01
      • 2012-12-24
      • 2014-11-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多