簡體   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)

如標題所示,我從包括 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
}

任務.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"
}

main.cpp 的“包含”部分

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

我錯過了什么?

Xcode 默認會將.cpp文件編譯為 C++ 文件,而 Objective-C 的#import文件將不起作用。 您應該將文件重命名為.mm或在右側的Inspector window 中更改其類型。

由於您沒有使用 Xcode,而是使用 vscode,因此重命名文件要容易得多,因為 clang 將自動將其編譯為 Objective-C++

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM