【问题标题】:How to fix "undefined symbols for architecture arm64"如何修复“架构 arm64 的未定义符号”
【发布时间】:2021-09-30 05:20:23
【问题描述】:

我在 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"
            }
      ]
}

谢谢!

【问题讨论】:

  • 您的活动文件构建失败。查看您的集成终端以获取实际详细信息。
  • 你在编译的时候关注过你想要构建的cpp吗?
  • 显示错误,会有所帮助
  • @LouisGo @Lala5th 终端显示Undefined symbols for architecture arm64:,然后提供大量来自std 命名空间的引用。我认为这与我如何为 VS Code 提供 iostream.h 的包含路径有关?
  • Edit 您的问题并提供您的 helloworld.cpp 和错误消息。您不需要包含所有 std 未定义的参考错误,但其他人有有意义的细节。它应该会有所帮助。

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


【解决方案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

}

【讨论】:

    猜你喜欢
    • 2013-10-13
    • 1970-01-01
    • 2021-08-23
    • 2021-07-25
    • 2018-11-14
    • 2015-12-05
    相关资源
    最近更新 更多