【问题标题】:Mac OS - VS Code Insiders - Code Runner: Undefined symbols for architecture arm64Mac OS - VS Code Insiders - Code Runner:架构 arm64 的未定义符号
【发布时间】:2021-03-06 03:54:24
【问题描述】:

过去一周我一直被这个问题困扰。当我使用 VS Code Insiders - Code Runner Extension 或命令编译代码时:clang++ -std=c++14 main.cpp,它给了我以下错误:

Undefined symbols for architecture arm64:
  "LinkedList::insertHead(int)", referenced from:
      _main in main-6d6a24.o
  "LinkedList::insertTail(int)", referenced from:
      _main in main-6d6a24.o
  "operator<<(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, LinkedList const&)", referenced from:
      _main in main-6d6a24.o
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

但是,我能够使用下面的 Makefile 编译代码:

all: main

main: main.o linkedList.o
    clang++ -std=c++14 -o $@ $^

main.o: main.cpp linkedList.h
    clang++ -std=c++14 -c $<

linkedList.o: linkedList.cpp linkedList.h
    clang++ -std=c++14 -c $<

clean:
    rm -f main *.o
    rm -f linkedList *.o

如果我将 int main() {} 放在linkedList.cpp 中,它也会起作用。 我想也许有某种链接器问题?查错的时候提到了很多。

代码如下: main.cpp:

#include "linkedList.h"
#include <iostream>

int main() {
  LinkedList l;
  LinkedList l2;

  for (int i = 0; i < 10; i++) {
    l.insertHead(i);
  }

  for (int i = 0; i < 10; i++) {
    l2.insertTail(i);
  }

  std::cout << l << std::endl;
  std::cout << l2 << std::endl;

  return 0;
}

linkedList.h:

#include <iostream>

struct Node {
  int data;
  Node *next = nullptr;
};

class LinkedList {
 private:
  Node *head;
  Node *tail;
  void inserFirst(int);
  Node *createNode(int);

 public:
  void insertHead(int);
  void insertTail(int);
  friend std::ostream &operator<<(std::ostream &out, const LinkedList &list);
};

linkedList.cpp:

#include "linkedList.h"
  
void LinkedList::inserFirst(int item) {
  Node *n = createNode(item);
  head = n;
  tail = n;
}

Node* LinkedList::createNode(int item) {
  Node *n = new Node;
  n->data = item;
  n->next = nullptr;
  return n;
}

void LinkedList::insertHead(int item) {
  if (head == nullptr) {
    inserFirst(item);
  } else {
    Node *n = createNode(item);
    n->next = head;
    head = n;
  }
}

void LinkedList::insertTail(int item) {
  if (head == nullptr) {
    inserFirst(item);
  } else {
    Node *n = createNode(item);
    tail->next = n;
    tail = n;
  }
}

std::ostream &operator<<(std::ostream &out, const LinkedList &list) {
  Node *n = list.head;
  while (n != nullptr) {
    out << n->data;
    n = n->next;
  }
  return out;
}

让我困惑的是,既然代码可以用 Makefile 编译,为什么不能用 code runner 编译呢?

只是一个快速更新:我在 CLion 上测试了代码并且它已经编译,所以我将在 Vs Code 上重新安装代码运行器扩展,看看它是否能解决问题。

【问题讨论】:

  • 这能回答你的问题吗? C++: Undefined symbols for architecture x86_64
  • 因为你没有编译linkedList.cpp
  • 对,我删除了我之前的回复,因为 C++: Undefined symbols for architecture x86_64 确实有助于提供线索。我修改了settings.json,现在可以了,谢谢!

标签: c++ c++11 visual-studio-code linker apple-silicon


【解决方案1】:

由于某种原因,MAC 上的 vscode 不会自动链接文件。您可能需要在终端中运行代码。

g++ main.cpp linkedList.cpp -o main

然后就可以执行程序了

./main

注意不要忘记与正在编译的文件在同一路径上

【讨论】:

    【解决方案2】:

    使用 XCode IDE 运行 C++ 代码时,CPP 文件不会添加到您尝试自动构建的目标中。虽然你可以在目标目录下看到它们,但这并不意味着它已添加到该目标的已编译 CPP 文件中。

    您可以将源文件添加到 XCode 右侧的目标成员中。

    另一方面,您可以使用 Makefile 和其他选项进行构建,因为您在此处指定需要手动编译的源代码

    linkedList.o: linkedList.cpp linkedList.h
        clang++ -std=c++14 -c $<
    

    一般来说,当您尝试使用 XCode 构建 C++ 目标并遇到 undefined symbols 错误时,这意味着您缺少某些标头/函数的实现

    【讨论】:

      【解决方案3】:

      找到一个“临时”答案: 在 Code-runner: Executor Map 中,在设置中进行编辑。 json,我改了

      "cpp": "cd $dir && clang++ -std=c++14 $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
      

      在 $fileName 之后添加linked_list.cpp(实现文件)

      "cpp": "cd $dir && clang++ -std=c++14 $fileName linked_list.cpp -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
      

      【讨论】:

        猜你喜欢
        • 2021-12-21
        • 2013-10-13
        • 1970-01-01
        • 2013-06-10
        • 2021-08-23
        • 2021-07-25
        相关资源
        最近更新 更多