尝试在C++项目中使用 Metal 时出现"unknown type name 'NSString'"错误(与代码,mac m1 相比)



正如标题所示,我得到了很多"未知类型名称"NSString'";文件中的错误,包括NSObjCRuntime.h、NSObject.h和NSZone.h。然后我添加了

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

;包括";主.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窗口中更改其类型。

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

最新更新