簡體   English   中英

找不到符號鏈接庫

[英]Symbol not found linking a library

我正在編寫一個簡單的游戲引擎,並且具有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

我可以做什么? 謝謝。

如果要從多個.cpp文件引用內聯函數的代碼,請將其放在頭文件中。

參考這里

必須在使用它的每個翻譯單元中定義一個外部鏈接inline函數(實際上是相同的)。

因此,要么從定義中刪除inline ,要么將定義放在頭文件中。

干杯,……

您的代碼(經過稍微修改以包含文本的子類)在我的機器上(Mac也是,GCC 4.2.1)可以正常編譯。

嘗試刪除所有.o文件,並使用干凈目錄進行編譯。 如果那也失敗了,我會嘗試刪除內聯定義。

嘗試從實現中刪除inline限定符。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM