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

问题描述 投票:0回答:2

我在 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;
}

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

启动.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"
            }
      ]
}

谢谢!

c++ visual-studio-code clang std
2个回答
0
投票

如果您遇到这种错误,理想情况下您会希望使用此命令链接所有文件,注意:这是当您使用 mac m1 时

在终端上运行此命令,替换该目录中的所有文件。编译多个源文件并将它们链接为arm64架构

例如:

  g++ -arch arm64 -o output_file main.cpp student.cpp other_file.cpp - 
    I/path/to/include -L/path/to/lib -lrequired_lib

  eg: g++ -arch arm64 -o output_file main.cc student.cc student.h  

然后./output_file


-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

}

© www.soinside.com 2019 - 2024. All rights reserved.