找不到链接库的符号
我正在编写一个简单的游戏引擎,并且有 EntityComponent.h 文件:
#ifndef Psycho2D_Core_EntityComponent_
#define Psycho2D_Core_EntityComponent_
#include <string>
namespace psycho2d{
class EntityComponent{
private:
std::string m_name;
public:
EntityComponent(const std::string &name);
virtual ~EntityComponent();
const std::string& getName() const;
virtual void initialize() = 0;
virtual void loadProperties() = 0;
virtual void update() = 0;
virtual void destroy() = 0;
};
}
#endif
和相关的 EntityComponent.cpp 文件:
#include "EntityComponent.h"
#include <string>
psycho2d::EntityComponent::EntityComponent(const std::string &name){
this->m_name = name;
}
psycho2d::EntityComponent::~EntityComponent(){}
inline const std::string& psycho2d::EntityComponent::getName() const{
return this->m_name;
}
这两个文件是框架的一部分(我正在 Mac 上工作)。他们编译得很好。 问题是当我编写使用该库的可执行文件时。 我创建了 EntityComponent 的子类,并且它可以编译。但是,如果我调用 getName() 函数,链接器会告诉我:
"psycho2d::EntityComponent::getName() const", referenced from:
_main in main.o
Symbol(s) not found
Collect2: ld returned 1 exit status
我能做什么? 谢谢。
I'm writing a simple game engine, and I have the EntityComponent.h file:
#ifndef Psycho2D_Core_EntityComponent_
#define Psycho2D_Core_EntityComponent_
#include <string>
namespace psycho2d{
class EntityComponent{
private:
std::string m_name;
public:
EntityComponent(const std::string &name);
virtual ~EntityComponent();
const std::string& getName() const;
virtual void initialize() = 0;
virtual void loadProperties() = 0;
virtual void update() = 0;
virtual void destroy() = 0;
};
}
#endif
And the relative EntityComponent.cpp file:
#include "EntityComponent.h"
#include <string>
psycho2d::EntityComponent::EntityComponent(const std::string &name){
this->m_name = name;
}
psycho2d::EntityComponent::~EntityComponent(){}
inline const std::string& psycho2d::EntityComponent::getName() const{
return this->m_name;
}
These two file are part of a framework (I'm working on a Mac). They compile well.
The problem is when I write an executable that use the library.
I have created a sub-class of EntityComponent, and it compiles. But, if I call the getName() function, the linker tell me:
"psycho2d::EntityComponent::getName() const", referenced from:
_main in main.o
Symbol(s) not found
Collect2: ld returned 1 exit status
What I can do?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您想从多个 .cpp 文件中引用内联函数,请将内联函数的代码放在头文件中。
请参考此处。
Put the code for the inline function in the header file, if you want to reference it from multiple .cpp files.
Reference here.
外部链接
内联
函数必须在使用它的每个翻译单元中定义(实际上相同)。因此,要么从定义中删除
inline
,要么将定义放在头文件中。干杯&呵呵,
An external linkage
inline
function must defined (as effectively the same) in every translation unit where it's used.So either remove the
inline
from the definition, or place the definition in the header file.Cheers & hth.,
您的(稍加修改以包含文本子类)代码在我的机器(Mac 也是如此。GCC 4.2.1)上可以正常编译
。尝试删除所有 .o 文件并使用干净的目录进行编译。如果这也失败了,我会尝试删除内联定义。
Your (slightly modified to include a subclass to text) code compiles fine on my machine (A Mac, too. GCC 4.2.1)
Try removing all .o-files and compile with a clean directory. If that fails too, I'd try to remove the inline-definition.
尝试从您的实现中删除
inline
限定符。Try removing the
inline
qualifier from your implementation.