简体   繁体   中英

"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. Then I added

#include <Foundation/NSString.h>

to main.cpp file, but the errors persist.

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"
}

The "include" part of 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. You should rename your file to .mm or change its type in Inspector window on the right.

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++

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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