简体   繁体   English

尝试在 C++ 项目中使用 Metal 时出现“未知类型名称‘NSString’”错误(vs 代码,mac m1)

[英]"unknown type name 'NSString'" error when trying to use Metal in a C++ project (vs code, mac m1)

As indicated by the title, I got many "unknown type name 'NSString'" errors from files including NSObjCRuntime.h, NSObject.h, and NSZone.h.如标题所示,我从包括 NSObjCRuntime.h、NSObject.h 和 NSZone.h 在内的文件中收到许多“未知类型名称 'NSString'”错误。 Then I added然后我加了

#include <Foundation/NSString.h>

to main.cpp file, but the errors persist.到 main.cpp 文件,但错误仍然存在。

c_cpp_properties.json 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任务.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"
}

The "include" part of main.cpp main.cpp 的“包含”部分

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

What did I miss?我错过了什么?

Xcode will compile .cpp files as C++ files by default and #import s for Objective-C stuff won't work. Xcode 默认会将.cpp文件编译为 C++ 文件,而 Objective-C 的#import文件将不起作用。 You should rename your file to .mm or change its type in Inspector window on the right.您应该将文件重命名为.mm或在右侧的Inspector window 中更改其类型。

Since you aren't using Xcode, but rather vscode, it's much easier to just rename the file, cause then clang will automatically compile it as Objective-C++由于您没有使用 Xcode,而是使用 vscode,因此重命名文件要容易得多,因为 clang 将自动将其编译为 Objective-C++

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM