Mac上的VSCODE:Linker命令失败,出口代码失败

发布于 2025-01-25 19:04:31 字数 1779 浏览 2 评论 0 原文

我使用Mac上的VSCODE是C ++的新手,我尝试使用示例程序进行测试。

在标题文件中,我声明了课程。

#ifndef STUDENT_H
#define STUDENT_H
#include <iostream>
using namespace std;

class Student{
    public:
    string name;
    int rollno;

    Student();
    Student(string s, int id);
    void printDetails();
};
#endif

实现在.cpp文件中完成。

#include <iostream>
#include "student.h"
using namespace std;

Student::Student(){}
Student::Student(string s, int id):name(s),rollno(id){}
void Student::printDetails(){
    cout<< rollno << "-" << name<<endl;
}

最后,我添加了 main.cpp

#include <iostream>
#include "student.h"
using namespace std;

int main(){
    Student students[8];
    students[0] = Student("Tom", 1);
    students[1] = Student("Jerry", 2);

    for(int i = 0; i < 2; i++){
        students[i].printDetails();
    }
}

但是,当我遵循。编译器发布错误

Undefined symbols for architecture x86_64:
  "Student::printDetails()", referenced from:
      _main in Main-896002.o
  "Student::Student(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, int)", referenced from:
      _main in Main-896002.o
  "Student::Student()", referenced from:
      _main in Main-896002.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

可以通过手动链接 main.cpp student.cpp 来解决问题,

g++ Main.cpp student.cpp -o Main

但是,有一种更有效的方法作为手册当项目规模较大时,工作很麻烦?

I am new to C++ using vscode on MAC and I tried to test using a sample program.

In the header file, I declare the class.

#ifndef STUDENT_H
#define STUDENT_H
#include <iostream>
using namespace std;

class Student{
    public:
    string name;
    int rollno;

    Student();
    Student(string s, int id);
    void printDetails();
};
#endif

The implementation is done in the .cpp file.

#include <iostream>
#include "student.h"
using namespace std;

Student::Student(){}
Student::Student(string s, int id):name(s),rollno(id){}
void Student::printDetails(){
    cout<< rollno << "-" << name<<endl;
}

Finally, I added Main.cpp.

#include <iostream>
#include "student.h"
using namespace std;

int main(){
    Student students[8];
    students[0] = Student("Tom", 1);
    students[1] = Student("Jerry", 2);

    for(int i = 0; i < 2; i++){
        students[i].printDetails();
    }
}

However, when I followed the procedures suggested in https://code.visualstudio.com/docs/cpp/config-clang-mac . The compiler issued errors

Undefined symbols for architecture x86_64:
  "Student::printDetails()", referenced from:
      _main in Main-896002.o
  "Student::Student(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, int)", referenced from:
      _main in Main-896002.o
  "Student::Student()", referenced from:
      _main in Main-896002.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

The issue can be resolved by manually linking Main.cpp with student.cpp, i.e.,

g++ Main.cpp student.cpp -o Main

However, is there a more effective way to do so as the manual work is troublesome when the project size is large?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文