c++ - 如何修复 "clang: error: linker command failed with exit code 1 (use -v to see invocation)"错误?

标签 c++ clang

这个问题在这里已经有了答案:





What is an undefined reference/unresolved external symbol error and how do I fix it?

(37 个回答)


1年前关闭。




我有一个程序应该按名称创建一个排序的链表。但是,运行它后,输出给我一个错误:
架构 x86_64 的 undefined symbol :

"StudentList::insertNode(Student)", referenced from:
      buildList(StudentList&) in main-acc0ea.o
  "StudentList::StudentList()", referenced from:
      _main in main-acc0ea.o
  "StudentList::~StudentList()", referenced from:
      _main in main-acc0ea.o
  "StudentList::displayList(double) const", referenced from:
      _main in main-acc0ea.o
  "StudentList::displayList(double, double) const", referenced from:
      _main in main-acc0ea.o
  "StudentList::displayList() const", referenced from:
      _main in main-acc0ea.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
我的代码:
主文件
#include <iostream>
#include "StudentList.h"
using namespace std;
void buildList(StudentList &);
int main()
{
   // Define a StudentList object
   StudentList list;
   buildList(list); // insert data into the list
   list.displayList();
   double gpa;
   cout << "Enter a gpa: ";
   cin >> gpa;
   list.displayList(gpa);
   double from, to;
   cout << "Enter a gpa range: ";
   cin >> from >> to;
   list.displayList(from, to);
   system("pause");
   return 0;
}
void buildList(StudentList &list)
{
   // Define and initialize an array of Student objects
   Student s[10] =
   { { 2.3, "Tom" },{ 2.5, "John" },{ 3.1, "Paul" },{ 3.9, "Linda" },{ 3.6, "Bob" },{ 2.7, "Ann" },{ 4.0, "Mary" },
   { 3.2, "Andy" },{ 0, "#" } };
   //Insert data from array into the linked list
   for (int i = 0; s[i].name != "#"; i++)
   {
       list.insertNode(s[i]);
   }
}
学生列表.h
#ifndef STUDENTLIST_H
#define STUDENTLIST_H
#include<string>
struct Student
{
   double gpa;
   std::string name;
};
class StudentList
{
private:
   // Declare a structure for the list
   struct ListNode
   {
       Student stu; // The value in this node
       ListNode *next; // To point to the next node
   };
   ListNode *head; // List head pointer
   int count; // To keep track of the number of nodes in the list
public:
   StudentList(); // Constructor
   ~StudentList(); // Destructor

                   // Linked list operations
   int getCount() const { return count; }
   void insertNode(Student);
   void displayList() const;
   void displayList(double) const;
   void displayList(double,double) const;
   /* Write your code here */
};
#endif
学生列表.cpp:
#include <iostream> // For cout and NULL
#include "StudentList.h"
using namespace std;
//**************************************************
// Constructor
// This function allocates and initializes a sentinel node
// A sentinel (or dummy) node is an extra node added before the first data record.
// This convention simplifies and accelerates some list-manipulation algorithms,
// by making sure that all links can be safely dereferenced and that every list
// (even one that contains no data elements) always has a "first" node.
//**************************************************
StudentList::StudentList()
{
   head = new ListNode; // head points to the sentinel node
   head->stu.gpa = -1;
   head->stu.name = "";
   head->next = NULL;
   count = 0;
}
//**************************************************
// displayList shows the value
// stored in each node of the linked list
// pointed to by head.
//**************************************************
void StudentList::displayList() const
{
   ListNode *pCur; // To move through the list
                   // Position pCur: skip the head of the list.
   pCur = head->next;
   // While pCur points to a node, traverse the list.
   cout << endl;
   while (pCur)
   {
       // Display the value in this node.
       cout << pCur->stu.gpa << " " << pCur->stu.name << endl;
       // Move to the next node.
       pCur = pCur->next;
   }
   cout << endl;
}
//**************************************************
// The insertNode function inserts a node with
// stu copied to its value member.
//**************************************************
void StudentList::insertNode(Student dataIn)
{
   ListNode *newNode; // A new node
   ListNode *pCur; // To traverse the list
   ListNode *pPre; // The previous node
                   // Allocate a new node and store num there.
   newNode = new ListNode;
   newNode->stu = dataIn;
   // Initialize pointers
   pPre = head;
   pCur = head->next;
   // Find location: skip all nodes whose gpa is less than dataIn's gpa
   while (pCur != NULL && pCur->stu.name < dataIn.name)
   {
       pPre = pCur;
       pCur = pCur->next;
   }
   // Insert the new node between pPre and pCur
   pPre->next = newNode;
   newNode->next = pCur;
   // Update the counter
   count++;
}
//**************************************************
// Destructor *
// This function deletes every node in the list. *
//**************************************************
StudentList::~StudentList()
{
}

void StudentList::displayList(double aboveGPA) const
{
   ListNode *pCur; // To move through the list
                   // Position pCur: skip the head of the list.
   pCur = head->next;
   // While pCur points to a node, traverse the list.
   cout << endl;
   while (pCur)
   {
       if (pCur->stu.gpa >= aboveGPA)
       {
           // Display the value in this node.
           cout << pCur->stu.gpa << " " << pCur->stu.name << endl;
       }
       // Move to the next node.
       pCur = pCur->next;
   }
   cout << endl;
}

void StudentList::displayList(double gpa1, double gpa2) const
{
   ListNode *pCur; // To move through the list
                   // Position pCur: skip the head of the list.
   double upperLimit, loweLimit;
   if (gpa1 > gpa2)
   {
       upperLimit = gpa1;
       loweLimit = gpa2;
   }
   else
   {
       upperLimit = gpa2;
       loweLimit = gpa1;
   }
   pCur = head->next;
   // While pCur points to a node, traverse the list.
   cout << endl;
   while (pCur)
   {
       if (pCur->stu.gpa >= loweLimit && pCur->stu.gpa<=upperLimit)
       {
           // Display the value in this node.
           cout << pCur->stu.gpa << " " << pCur->stu.name << endl;
       }
       // Move to the next node.
       pCur = pCur->next;
   }
   cout << endl;
}
我不知道我的代码是错误的还是这个问题:
Visual Studio Code clang error: linker command failed with exit code 1 on Mac
我尝试在我的任务 json 中添加“${fileDirname}/*.cpp”,但它仍然给我同样的错误。这个错误还有其他解决方案吗?

最佳答案

链接器找不到它提示的符号。可能是因为它没有查看 StudentList 对象文件。
尝试找出它是如何被调用的,并检查是否将 StudentList 对象文件提供给它。如果不是,请找出原因。

关于c++ - 如何修复 "clang: error: linker command failed with exit code 1 (use -v to see invocation)"错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64313086/

相关文章:

c++ - 如何在 CMake 中删除特定系统包含目录

c++ - 为什么 clang 不警告从 double 到 int 的隐式转换,而是在从 long 到 int 时警告?

c++ - clang 是否支持 "-fprofile-generate"和 "-fprofile-use"?

c++ - 使用隐式可转换对象调用 move 重载函数时出现编译错误

c++ - "if constexpr"在模板之外有用吗?

c++ - std::array 的堆栈分配和堆栈溢出错误

c++ - 交叉编译错误 Visual Studio C++

c++ - 如何从 vector 中获取随机且唯一的值?

c++ - 内存使用检查

c++ - cv-qualified struct 的成员不是类似的 cv-qualified