简体   繁体   中英

How to fix “undefined symbols for architecture arm64”

I'm running VS Code on MacOS, and I'm using clang to compile a simple "Hello World!" program in C++. However, when I try to run my program, VS Code gives me the following error message: Undefined symbols for architecture arm64: followed by dozens of references to the std library. At the bottom of the terminal, it says:

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) What does this mean? and (b) how can I fix it?

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

Thanks!

If you have multiple CPP files in the project then you need to add "${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
      }
    }
  ]
}

If you're having a vector; error from vscode also add the c_cpp_properties.json file in .vscode directory :

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

}

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