我正在尝试将.cpp实现文件与头文件链接-我从mac终端收到此错误消息-
rowlandev:playground rowlandev$ g++ main.cpp -o main
Undefined symbols for architecture x86_64:
"Person::Person()", referenced from:
_main in main-32e73b.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
这是我的cpp文件中的代码:
#include <iostream>
#include "playground.h"
using namespace std;
Person::Person() {
cout << "this is running from the implementation file" << endl;
}
这是我主要功能的代码:
#include <iostream>
#include "playground.h"
using namespace std;
int main() {
Person chase;
}
这是我的头文件中的代码:
#include <string>
using namespace std;
#ifndef playground_h
#define playground_h
class Person {
public:
Person();
private:
string name;
int age;
};
#endif /* playground_h */
我该怎么做才能解决此错误?随意添加我可以做的其他事情来改进我刚刚编写的代码。向任何事物开放。
尝试阅读源代码中的可执行文件时,这是一本好书,以了解发生了什么:编译/链接过程如何工作?
这里发生的是链接器不知道在Person::Person()
中调用的main()
位置。请注意,当您调用g ++时,从未为它提供过为其编写代码的文件Person::Person()
。
调用g ++的正确方法是: $ g++ -o main main.cpp person.cpp
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句