2009-07-13 7 views
6

J'écris une bibliothèque, qui est dynamiquement chargée dans mon application principale avec dlsym. J'ai les fichiers suivants:Pourquoi une erreur de recherche de symbole apparaît-elle?

library.h

#include <ilibrary.h> 
#include <igateway.h> 

class LibraryImpl; 

class Library: public ILibrary { 
public: 
    static ILibrary* instance(); 

    IGateway* getGateway() const; 

protected: 
    Library(); 
    virtual ~Library(); 

private: 
    static ILibrary* instance_; 
    LibraryImpl* library_; 
}; 

extern "C" { 
    IMPORT_EXPORT ILibrary* getLibrary(); 
} 

library.cpp

#include "library.h" 

#include "business/BCGateway.h" 


class LibraryImpl { 
public: 
    IGateway* getGateway(); 
}; 

IGateway* LibraryImpl::getGateway() { 
    return BCGateway::instance(); 
} 



ILibrary* Library::instance_ = NULL; 
ILibrary* Library::instance() { 
    return instance_ ? instance_ : (instance_ = new Library); 
} 

Library::Library() { 
    library_ = new LibraryImpl(); 
} 

Library::~Library() { 
    delete library_; 
} 

IGateway* Library::getGateway() const { 
    return library_->getGateway(); 
} 


extern "C" { 
    IMPORT_EXPORT 
    ILibrary* getLibrary(){ 
     return Library::instance(); 
    } 
} 

affaires/BCGateway.h

#include <igateway.h> 

class BCGateway: public IGateway { 
public: 
    static IGateway* instance(); 

protected: 
    BCGateway(); 

private: 
    static IGateway* instance_; 
}; 

affaires/BCGateway.cpp

#include "BCGateway.h" 

IGateway* BCGateway::instance_ = NULL; 
IGateway* BCGateway::instance(){ 
    return instance_ ? instance_ : (instance_ = new BCGateway); 
} 

Je peux me connecter à la bibliothèque et charger avec succès l'instance de bibliothèque. Mais quand je l'appelle Library-> getGateway() dans ma principale application que je reçois l'erreur suivante:

symbol lookup error: ./gateways/libSwisscomXtraZone.so: undefined symbol: _ZN9BCGateway8instanceEv

Pouvez-vous s'il vous plaît me donner un indice, comment je peux résoudre ce problème? Je suis coincé.

Merci.

Répondre

7

Je mets l'erreur par c++filt, il est dit que le nom mutilée signifie

BCGateway::instance() 

Cela suggère que vous appelez BCGateway::instance() quelque part et oublié de créer un lien contre BCGateway.o ou vous même avez oublié de définir BCGateway::instance().

+0

Ecrit mon dernier commentaire, j'ai vu, que j'ai juste oublié d'ajouter le BCGateway.h et BCGateway.cpp à mon fichier de projet, à partir duquel le makefile est généré. Donc, cette classe n'a en effet pas été déclarée dans ma bibliothèque. Depuis que je suis très nouveau pour les bibliothèques C++, j'ai pensé que c'est un problème avec la déclaration de la bibliothèque ou plus. Merci beaucoup pour votre aide. –

1

tous les membres statiques doivent être initialisés dans un fichier cpp. Voyant que BCGateway :: instance n'est pas intialisée à aucun moment, elle ne pourra pas trouver le symbole. Le problème est, cependant, que si vous créez un BCGateway.cpp et que vous initialisez l'instance, vous finirez par n'avoir qu'une seule instance sur, potentiellement, de nombreux processus. Cela peut ou peut ne pas être un problème selon la façon dont vous utilisez la DLL.

+0

Les instances ne seront pas partagées entre les processus. Pas même dans une bibliothèque partagée. –

+0

Tous les symboles doivent être «définis». Celui-ci est seulement "déclaré". Je suppose que c'est ce que tu veux dire. (Par opposition à «initialisé») – xtofl

+0

Cheers xtofl. Vous avez tout à fait raison :) – Goz

Questions connexes