【问题标题】:xCode project not locating c++ std libraries on macOS Catalina 10.15.5xCode 项目未在 macOS Catalina 10.15.5 上找到 c++ 标准库
【发布时间】:2020-09-28 00:20:19
【问题描述】:

我有一个使用 Metal 的 xCode Swift 5 项目,它有两个目标,主应用程序和一个构建第三方 C 库的静态库目标。当我编译项目时,错误消息(见下文)表明 xCode 正在 iPhone SDK 中搜索系统 C 头文件,但它没有找到,也没有在任何地方找到它们。我怎样才能解决这个问题?请:)

  • 静态库目标构建第三方C库,并有一个Obj-c公共h/m文件(AGBGPUType.h,AGBGPUType.m)。
  • 在 Swift/Metal 项目的桥接头文件 (Common.h) 中引用了此库头文件。
  • 所有内部库头文件和相关源代码都包含在 Swift/Metal 项目中 - 这不是工作区,它是具有两个目标的单个项目。
  • 构建应用程序目标的方案设置为首先编译库,然后是应用程序,我正在构建的设备是运行 iOS 13.5 的 iPhone Xs Max。

我已经设置了 C 库的公共 Obj-c (.h & .m) 以登录到控制台并创建对字体库的引用 - 这意味着它可以访问第三方 C 库代码功能方式。

库obj-c文件为:

#import "AGBGPUType.h"
#import "ft2build.h"
#include FT_FREETYPE_H
#include FT_STROKER_H

@implementation AGBGPUType

FT_Library _fontLibrary;
FT_Face _face;
FT_GlyphSlot _glyph;
FT_Stroker _stroker;

-(void) simple {
  int giddy = 5000;
  NSLog(@"From main library objc AGBGPUType.m %d", giddy);

  if(FT_Init_FreeType(&_fontLibrary)) {
      NSLog(@"Could not init Freetype library");
  }

}

@end

Swift/Metal 应用程序中的桥接头文件有:

#ifndef Common_h
#define Common_h
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#import <simd/simd.h>

#import "AGBGPUType/AGBGPUType.h"
...
#endif

构建应用时出现以下错误:

CompileMetalFile /Users/username/Documents/xCode/IncuFlic/IncuFlic/Shaders.metal (in target 'IncuFlic' from project 'IncuFlic')
    cd /Users/username/Documents/xCode/IncuFlic
    export SDKROOT=/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS13.5.sdk
    /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/metal -c -target air64-apple-ios13.5 -gline-tables-only -MO -I/Users/username/Library/Developer/Xcode/DerivedData/IncuFlic-fxtjxtzaumyrcuhiwrtiajlsnzak/Build/Products/Debug-iphoneos/include -F/Users/username/Library/Developer/Xcode/DerivedData/IncuFlic-fxtjxtzaumyrcuhiwrtiajlsnzak/Build/Products/Debug-iphoneos -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS13.5.sdk -ffast-math -serialize-diagnostics /Users/username/Library/Developer/Xcode/DerivedData/IncuFlic-fxtjxtzaumyrcuhiwrtiajlsnzak/Build/Intermediates.noindex/IncuFlic.build/Debug-iphoneos/IncuFlic.build/Metal/Shaders.dia -o /Users/username/Library/Developer/Xcode/DerivedData/IncuFlic-fxtjxtzaumyrcuhiwrtiajlsnzak/Build/Intermediates.noindex/IncuFlic.build/Debug-iphoneos/IncuFlic.build/Metal/Shaders.air -index-store-path /Users/username/Library/Developer/Xcode/DerivedData/IncuFlic-fxtjxtzaumyrcuhiwrtiajlsnzak/Index/DataStore "" -MMD -MT dependencies -MF /Users/username/Library/Developer/Xcode/DerivedData/IncuFlic-fxtjxtzaumyrcuhiwrtiajlsnzak/Build/Intermediates.noindex/IncuFlic.build/Debug-iphoneos/IncuFlic.build/Metal/Shaders.dat /Users/username/Documents/xCode/IncuFlic/IncuFlic/Shaders.metal

In file included from /Users/username/Documents/xCode/IncuFlic/IncuFlic/Shaders.metal:35:
In file included from /Users/username/Documents/xCode/IncuFlic/IncuFlic/Common.h:33:
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS13.5.sdk/usr/include/stdio.h:64:
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS13.5.sdk/usr/include/_stdio.h:68:
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS13.5.sdk/usr/include/sys/cdefs.h:807:2: error: Unsupported architecture
##error Unsupported architecture##
 ^
In file included from /Users/username/Documents/xCode/IncuFlic/IncuFlic/Shaders.metal:35:
In file included from /Users/username/Documents/xCode/IncuFlic/IncuFlic/Common.h:33:
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS13.5.sdk/usr/include/stdio.h:64:
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS13.5.sdk/usr/include/_stdio.h:71:
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS13.5.sdk/usr/include/_types.h:27:
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS13.5.sdk/usr/include/sys/_types.h:33:
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS13.5.sdk/usr/include/machine/_types.h:34:2: error: architecture not supported
##error architecture not supported##
 ^
In file included from /Users/username/Documents/xCode/IncuFlic/IncuFlic/Shaders.metal:35:
In file included from /Users/username/Documents/xCode/IncuFlic/IncuFlic/Common.h:33:
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS13.5.sdk/usr/include/stdio.h:64:
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS13.5.sdk/usr/include/_stdio.h:71:
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS13.5.sdk/usr/include/_types.h:27:
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS13.5.sdk/usr/include/sys/_types.h:55:9: error: unknown type name '__int64_t'
typedef __int64_t       __darwin_blkcnt_t;      /* total blocks */
        ^
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS13.5.sdk/usr/include/sys/_types.h:56:9: error: unknown type name '__int32_t'
typedef __int32_t       __darwin_blksize_t;     /* preferred block size */
        ^
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS13.5.sdk/usr/include/sys/_types.h:57:9: error: unknown type name '__int32_t'
typedef __int32_t       __darwin_dev_t;         /* dev_t */
        ^
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS13.5.sdk/usr/include/sys/_types.h:60:9: error: unknown type name '__uint32_t'
typedef __uint32_t      __darwin_gid_t;         /* [???] process and group IDs */
        ^
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS13.5.sdk/usr/include/sys/_types.h:61:9: error: unknown type name '__uint32_t'
typedef __uint32_t      __darwin_id_t;          /* [XSI] pid_t, uid_t, or gid_t*/
        ^
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS13.5.sdk/usr/include/sys/_types.h:62:9: error: unknown type name '__uint64_t'; did you mean 'uint64_t'?
typedef __uint64_t      __darwin_ino64_t;       /* [???] Used for 64 bit inodes */
        ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/metal/ios/lib/clang/3902.67/include/metal/metal_types:48:25: note: 'uint64_t' declared here
typedef __UINT64_TYPE__ uint64_t;
                        ^
In file included from /Users/username/Documents/xCode/IncuFlic/IncuFlic/Shaders.metal:35:
In file included from /Users/username/Documents/xCode/IncuFlic/IncuFlic/Common.h:33:
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS13.5.sdk/usr/include/stdio.h:64:
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS13.5.sdk/usr/include/_stdio.h:71:
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS13.5.sdk/usr/include/_types.h:27:
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS13.5.sdk/usr/include/sys/_types.h:68:9: error: unknown type name '__darwin_natural_t'
typedef __darwin_natural_t __darwin_mach_port_name_t; /* Used by mach */
        ^
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS13.5.sdk/usr/include/sys/_types.h:70:9: error: unknown type name '__uint16_t'
typedef __uint16_t      __darwin_mode_t;        /* [???] Some file attributes */
        ^
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS13.5.sdk/usr/include/sys/_types.h:71:9: error: unknown type name '__int64_t'
typedef __int64_t       __darwin_off_t;         /* [???] Used for file sizes */
        ^
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS13.5.sdk/usr/include/sys/_types.h:72:9: error: unknown type name '__int32_t'
typedef __int32_t       __darwin_pid_t;         /* [???] process and group IDs */
        ^
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS13.5.sdk/usr/include/sys/_types.h:73:9: error: unknown type name '__uint32_t'
typedef __uint32_t      __darwin_sigset_t;      /* [???] signal set */
        ^
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS13.5.sdk/usr/include/sys/_types.h:74:9: error: unknown type name '__int32_t'
typedef __int32_t       __darwin_suseconds_t;   /* [???] microseconds */
        ^
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS13.5.sdk/usr/include/sys/_types.h:75:9: error: unknown type name '__uint32_t'
typedef __uint32_t      __darwin_uid_t;         /* [???] user IDs */
        ^
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS13.5.sdk/usr/include/sys/_types.h:76:9: error: unknown type name '__uint32_t'
typedef __uint32_t      __darwin_useconds_t;    /* [???] microseconds */
        ^
In file included from /Users/username/Documents/xCode/IncuFlic/IncuFlic/Shaders.metal:35:
In file included from /Users/username/Documents/xCode/IncuFlic/IncuFlic/Common.h:33:
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS13.5.sdk/usr/include/stdio.h:64:
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS13.5.sdk/usr/include/_stdio.h:71:
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS13.5.sdk/usr/include/_types.h:27:
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS13.5.sdk/usr/include/sys/_types.h:80:
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS13.5.sdk/usr/include/sys/_pthread/_pthread_types.h:58:25: error: pointer type must have explicit address space qualifier
        void (*__routine)(void *);      // Routine to call
                               ^
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS13.5.sdk/usr/include/sys/_pthread/_pthread_types.h:59:7: error: pointer type must have explicit address space qualifier
        void *__arg;                    // Argument to pass
             ^
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS13.5.sdk/usr/include/sys/_pthread/_pthread_types.h:60:38: error: pointer type must have explicit address space qualifier
        struct __darwin_pthread_handler_rec *__next;


其他环境细节

我的道路 sudo nano /etc/paths

/usr/local/bin
/usr/bin
/bin
/usr/sbin
/sbin

当前活跃的开发者目录 xcode-select -p

/Applications/Xcode.app/Contents/Developer

GCC 编译器 gcc -v

Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/4.2.1
Apple clang version 11.0.3 (clang-1103.0.32.62)
Target: x86_64-apple-darwin19.5.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

C 预处理器 cpp -v

Apple clang version 11.0.3 (clang-1103.0.32.62)
Target: x86_64-apple-darwin19.5.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
 "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang" -cc1 -triple x86_64-apple-macosx10.15.0 -Wdeprecated-objc-isa-usage -Werror=deprecated-objc-isa-usage -E -disable-free -disable-llvm-verifier -discard-value-names -main-file-name - -mrelocation-model pic -pic-level 2 -mthread-model posix -mframe-pointer=all -fno-strict-return -masm-verbose -munwind-tables -target-sdk-version=10.15.4 -target-cpu penryn -dwarf-column-info -debugger-tuning=lldb -target-linker-version 556.6 -v -resource-dir /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/11.0.3 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk -I /usr/include -I/usr/local/include -internal-isystem 
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/local/include -internal-isystem 
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/11.0.3/include -internal-externc-isystem 
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include -internal-externc-isystem 
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include -Wno-objc-signed-char-bool-implicit-int-conversion -Wno-extra-semi-stmt -Wno-quoted-include-in-framework-header -fdebug-compilation-dir /Users/annstramer -ferror-limit 19 -fmessage-length 80 -stack-protector 1 -fstack-check -mdarwin-stkchk-strong-link -fblocks -fencode-extended-block-signature -fregister-global-dtors-with-atexit -fobjc-runtime=macosx-10.15.0 -fmax-type-align=16 -fdiagnostics-show-option -fcolor-diagnostics -traditional-cpp -o - -x c -
clang -cc1 version 11.0.3 (clang-1103.0.32.62) default target x86_64-apple-darwin19.5.0

ignoring nonexistent directory "/usr/include"
ignoring nonexistent directory "/usr/local/include"
ignoring nonexistent directory "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/local/include"

ignoring nonexistent directory "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/Library/Frameworks"

include "..." search starts here:
include <...> search starts here:
 /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/11.0.3/include
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include
 /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks (framework directory)
End of search list.
  • 通过阅读几篇文章,包括Broken c++ std libraries on macOS High Sierra 10.13How can I find all Clang versions installed on my Mac?可能 xCode 没有找到 C 安装的标头。错误消息表明 xCode 正在 iPhone13.5 sdk 中查找文件,但终端命令似乎确认该 sdk 中不包含 C 版本。
  • xCode 开发者目录中存在 C 安装。
  • 当库作为预建存档包含在 Obj-c 项目或 Swift 项目中时,项目编译时不会出错,会找到 C 标头,将消息记录到控制台并创建对字体库的引用。 Golden - 但在 Swift/Metal 项目中的桥接头根本不起作用。该桥接头中有几个结构可以在 Swift/Metal 代码的其他领域完美运行。
  • xCode 的(蛮力)全新安装并没有解决问题,安装命令行工具也没有解决问题,这两者都是在安装最近的 Catalina 升级之后,而不是相对最近的 Catalina 的全新安装,然后只是今天安装命令行工具升级。

我的问题:

  1. 我是否纠正了 xCode 未找到 C 安装?
  2. cpp -v 找到xCode 的C 安装,为什么xCode 在编译时没有?
  3. 请问,解决这个问题的正确专业方法是什么?

提前感谢您的见解。

【问题讨论】:

    标签: ios c swift xcode metalkit


    【解决方案1】:

    这里有一个很好的解释作为这个问题的概述,来自 Cecelia Humlelu。 https://www.youtube.com/watch?v=NvURClY2Xzk

    我发现这个关于如何集成 C 库的解释也非常直接,并且与 xCode 11 相近:https://www.youtube.com/watch?v=WQI02KR9kQw

    我通过在 ObjC 库目标中构建 C 库,然后使用 ObjC 访问库来将 C 库集成到 Swift 项目中。

    • 由于 Swift 可以直接使用 C,我删除了 Obj-C 库目标,从 命令行 构建了 C 库,并将存档和标头放入项目中(参见第 2 个视频链接) . Cecelia 视频的前 6 分钟是理解这些更改为何奏效的关键,从 6:45 开始的 C/Swift 部分清楚地概述了我所遵循的内容。信守承诺。
    • 在这个项目中,桥接头被导入到金属着色器中。我正在使用 Obj-C header 文件在桥接头中导入 C 库。删除此 Obj-C 标头并编写 C 函数一开始似乎可行。
    • 但是,当涉及到使用库的核心功能时,又出现了另一个问题。这个库涉及处理字符串,并且要将字符串从 Swift 函数调用传递到 C 函数调用,必须将指针参数添加到使用库代码的 C 函数调用中。在 GPU 上运行代码需要地址空间限定符。由于 C 函数是通过 Metal 在桥接头中导入的,因此编译器需要代码来指定地址空间标识符。问题是 C 没有用于指定指针参数地址空间的构造,因此无法在桥接头中导入这些函数声明。请注意:有些 C 声明不需要地址空间限定符,讨论超出了这个问题的范围。
    • 在 xCode 项目中,每个目标只允许一个桥接头,但还有另一种导入头文件的方法。我为库头文件创建了一个模块映射,并将其添加到项目构建设置中 Swift Compiler Search Paths 的 Import Paths 设置中。由于该库使用复杂的宏来公开功能,因此仍需要 C 或 Obj-C 包装器才能使用 Swift 中的库,但该库已完全集成并可​​从 CPU 代码访问。
    • 在工作解决方案中,Swift 将模块映射中的头文件识别为全局定义的,因此可以从项目中的任何 Swift 文件中调用它们,库头文件不再在目标的桥接头中引用。
    • 我按照视频中有关为 C 库创建模块映射的指导中的信函进行操作。

    这是 Obj-C 源文件声明:

    #import <LibraryWrapperCode.h>
    #import <MetalKit/MetalKit.h>
    #include "ft2build.h"
    #include FT_FREETYPE_H
    
    @implementation LibraryWrapperCode {
        FT_Library _fontLibrary;
        FT_Face _face;
        FT_GlyphSlot _glyph;
    }
    
    

    模块图:

    module GPUTextCWrapper {
      header "../GPUText/libraryWrapperCode.h"
      export *
    }
    

    C 库是从任何 Swift 文件中导入的:

    import GPUTextCWrapper
    

    我仍然不清楚我在做什么会生成此问题中记录的错误消息,但我认为我导入库的方式是编译器两次找到它,第二次,尝试重新编译库。如果我找到更直接的解释,我会再次更新这个答案。

    【讨论】:

    • 感谢您提供 Cecelia 视频的链接。对我来说关键部分是在 7:50 到 8:50 之间,她解释了如何在不使用标准桥的情况下导入一堆 C 模块。我也在使用一个 Metal 项目,它的桥干扰了我的 C 头文件的函数指针。
    猜你喜欢
    • 2020-12-28
    • 1970-01-01
    • 2020-11-02
    • 1970-01-01
    • 2020-09-22
    • 1970-01-01
    • 2018-02-10
    • 2021-06-29
    • 1970-01-01
    相关资源
    最近更新 更多