簡體   English   中英

Mac OS - VS Code Insiders - Code Runner:架構 arm64 的未定義符號

[英]Mac OS - VS Code Insiders - Code Runner: Undefined symbols for architecture arm64

在過去的一周里,我一直被困在這個問題上。 當我使用 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 中,它也會起作用。 我想也許有某種 linker 問題? 當我搜索錯誤時,它被提到了很多。

這是代碼: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;
}

鏈表.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);
};

鏈表.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 上重新安裝代碼運行器擴展,看看它是否能解決問題。

找到一個“臨時”答案:在代碼運行器中:執行器 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",

由於某種原因,MAC 上的 vscode 不會自動鏈接文件。 您可能必須在終端中運行代碼。

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

然后就可以執行程序了

./main

注意不要忘記與您正在編譯的文件位於同一路徑

使用 XCode IDE 運行 C++ 代碼時,CPP 文件不會添加到您嘗試自動構建的目標中。 即使您可以在目標目錄下看到它們,但這並不意味着它已添加到此目標的已編譯 CPP 文件中。

您可以將源文件添加到 XCode 右側的目標成員資格。

另一方面,您可以使用 Makefile 和其他選項進行構建,因為您指定了需要手動編譯的源,如下所示

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

通常,當您嘗試使用 XCode 構建 C++ 目標並且遇到undefined symbols錯誤時,這意味着您缺少某些標頭/函數的實現

對於 mac 用戶尤其是 mac m1、m2 的用戶來說,這是一個很常見的錯誤。 我認為這是由底層架構引起的。 通過在編譯命令中列出所有需要鏈接的文件來修復錯誤。

g++ file1.cpp file2.cpp ... -o main

我的例子是:

g++ main.c++ agent.c++ model.c++ -o main

好吧,直到我們讓visual studio與 CPP for mac 一起工作。 用這個。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM