Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why am I getting a symbol lookup error?

Tags:

c++

I am writing a library, which is dynamically loaded in my main-application with dlsym. I have the following files:

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();
    }
}

business/BCGateway.h

#include <igateway.h>

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

protected:
    BCGateway();

private:
    static IGateway* instance_;
};

business/BCGateway.cpp

#include "BCGateway.h"

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

I can connect to the library and successfully load the Library-instance. But when I call library->getGateway() in my main-app I get the following error:

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

Can you please give me a hint, how I can resolve this? I'm stuck.

Thanks.

like image 825
Sämy Avatar asked Jul 13 '09 11:07

Sämy


People also ask

What is a symbol lookup error?

/opt/scali/bin/mpimon: symbol lookup error: /opt/scali/bin/mpimon: undefined symbol: scampi_log_create. For applications to run successfully in Platform MPI, your environment must be set correctly. The error message above occurs when you have an incorrect LD_LIBRARY_PATH setting.

Why is there an undefined symbol error?

A symbol remains undefined when a symbol reference in a relocatable object is never matched to a symbol definition. Similarly, if a shared object is used to create a dynamic executable and leaves an unresolved symbol definition, an undefined symbol error results.


2 Answers

I put the error through c++filt, it says that the mangled name stands for

BCGateway::instance()

This suggests that you call BCGateway::instance() somewhere and forgot to link against BCGateway.o or you even forgot to define BCGateway::instance().

like image 111
Johannes Schaub - litb Avatar answered Sep 20 '22 08:09

Johannes Schaub - litb


well all static members must be intialised in a cpp file. Seeing as BCGateway::instance is not intialised at any point it will be unable to find the symbol. The problem is, however, that if you create a BCGateway.cpp and initialise the instance then you will end up only having one instance across, potentially, many processes. This may or may not be a problem depending on how you use the DLL.

like image 34
Goz Avatar answered Sep 17 '22 08:09

Goz