开发者

Symbol not found linking a library

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 v开发者_StackOverflow中文版oid 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.


Put the code for the inline function in the header file, if you want to reference it from multiple .cpp files.

Reference here.


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.,


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.


Try removing the inline qualifier from your implementation.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜