如何修复“架构 arm64 的未定义符号”

时间:2021-07-23 01:43:09

标签: c++ visual-studio-code clang std

我在 MacOS 上运行 VS Code,我正在使用 clang 编译一个简单的“Hello World!” C++程序。但是,当我尝试运行我的程序时,VS Code 给了我以下错误消息:Undefined symbols for architecture arm64: 后跟对 std 库的数十个引用。在终端的底部,它说:

ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Build finished with error(s).
The terminal process terminated with exit code: -1.

(a) 这是什么意思? (b) 我该如何解决?

HelloWorld.cpp

#include <iostream>

using namespace std;

int main()
{
      cout << "Hello World!" << endl;
      return 0;
}

tasks.json

{
      "tasks": [
            {
                  "type": "cppbuild",
                  "label": "C/C++: clang build active file",
                  "command": "/usr/bin/clang",
                  "args": [
                        "-g",
                        "${file}",
                        "-o",
                        "${fileDirname}/${fileBasenameNoExtension}"
                  ],
                  "options": {
                        "cwd": "${fileDirname}"
                  },
                  "problemMatcher": [
                        "$gcc"
                  ],
                  "group": {
                        "kind": "build",
                        "isDefault": true
                  },
                  "detail": "Task generated by Debugger."
            }
      ],
      "version": "2.0.0"
}

launch.json

{
      "version": "0.2.0",
      "configurations": [
            {
                  "name": "clang - Build and debug active file",
                  "type": "cppdbg",
                  "request": "launch",
                  "program": "${workspaceFolder}/HelloWorld.cpp",
                  "args": [],
                  "stopAtEntry": false,
                  "cwd": "${fileDirname}",
                  "environment": [],
                  "externalConsole": false,
                  "MIMode": "lldb",
                  "preLaunchTask": "C/C++: clang build active file"
            }
      ]
}

谢谢!

1 个答案:

答案 0 :(得分:-1)

如果项目中有多个CPP文件,则需要添加"${fileDirname}/*.cpp"

tasks.json

  "version": "2.0.0",
  "tasks": [
    {
      "type": "shell",
      "label": "clang++ build active file",
      "command": "/usr/bin/clang++",
      "args": [
        "-std=c++17",
        "-stdlib=libc++",
        "-g",
        "${fileDirname}/*.cpp",
        // "${file}",
        "-o",
        "${fileDirname}/${fileBasenameNoExtension}"
      ],
      "options": {
        "cwd": "${workspaceFolder}"
      },
      "problemMatcher": ["$gcc"],
      "group": {
        "kind": "build",
        "isDefault": true
      }
    }
  ]
}

如果你有一个向量;来自 vscode 的错误还在 .vscode 目录中添加了 c_cpp_properties.json 文件:

{
"configurations": [
    {
        "name": "Mac",
        "includePath": [
            "${workspaceFolder}/**"
        ],
        "defines": [
        ],
        "macFrameworkPath": [
            "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks"
        ],
        "compilerPath": "/usr/bin/clang",
        "cStandard": "c11",
        "cppStandard": "c++17",
        "intelliSenseMode": "clang-x64"
    }
],
"version": 4

}