【问题标题】:"unknown type name 'NSString'" error when trying to use Metal in a C++ project (vs code, mac m1)尝试在 C++ 项目中使用 Metal 时出现“未知类型名称‘NSString’”错误(vs code,mac m1)
【发布时间】:2021-08-23 00:05:41
【问题描述】:

如标题所示,我从包括 NSObjCRuntime.h、NSObject.h 和 NSZone.h 在内的文件中收到许多“未知类型名称 'NSString'”错误。然后我加了

#include <Foundation/NSString.h>

到 main.cpp 文件,但错误仍然存​​在。

c_cpp_properties.json

{
    "configurations": [
        {
            "name": "Mac",
            "includePath": [
                "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1",
                "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/12.0.0/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",
                "/opt/homebrew/include",
                "${workspaceFolder}/**"
            ],
            "defines": [],
            "macFrameworkPath": [],
            "cStandard": "c17",
            "cppStandard": "c++17",
            "intelliSenseMode": "macos-clang-arm"
        }
    ],
    "version": 4
}

tasks.json

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: clang++ build active file",
            "command": "/usr/bin/clang++",
            "args": [
                "-g",
                "${file}",
                "-I:/opt/homebrew/include",                
                "-I:/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1",
                "-I:/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/12.0.0/include",
                "-I:/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include",
                "-I:/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include",
                "-I:/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}",
        
            ],
            "options": {
                "cwd": "${workspaceFolder}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "Task generated by Debugger."
        }
    ],
    "version": "2.0.0"
}

ma​​in.cpp 的“包含”部分

#include <Foundation/NSString.h>
#include <Metal/Metal.h>
#include <MetalKit/MTKView.h>
#include <MetalKit/MetalKit.h>
using namespace std;

我错过了什么?

【问题讨论】:

  • NSString 是一个 Objective-C 对象。

标签: c++ visual-studio-code nsstring metal apple-m1


【解决方案1】:

Xcode 默认将 .cpp 文件编译为 C++ 文件,而用于 Objective-C 的 #imports 将不起作用。您应该将文件重命名为.mm 或在右侧的Inspector 窗口中更改其类型。

由于您使用的不是 Xcode,而是 vscode,因此重命名文件要容易得多,因为 clang 会自动将其编译为 Objective-C++

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-15
    • 1970-01-01
    • 2017-12-16
    • 2016-10-21
    • 1970-01-01
    相关资源
    最近更新 更多