(C++) shared objects und polymorphie



  • (war mir nich ganz sicher ob Linux/Unix oder Andere Compiler, aber mir wurde Linux/Unix empfohlen)

    Ich hab ein Projekt, das bisher in 2 Komponenten geteilt is: User Interace und Code. Der Code ist in einer .so versteckt. Nun verwende ich Polymorphie um späteren mit dlopen eingebundenen .so s das Leben zu erleichtern. (Zugegeben, die Architektur ist nich die einfachste).
    Nun krieg ich folgende Fehler:

    g++ -Wall -W -g -O0 -fPIC -L. -lcalculator -o calculator main.o
    ./libcalculator.so: undefined reference to `typeinfo for environment'
    ./libcalculator.so: undefined reference to `vtable for environment'
    collect2: ld returned 1 exit status
    make: *** [calculator] Fehler 1
    

    Was das bedeuten soll ist mir schleierhaft.

    Die Datei environment.h:

    #ifndef ENVIRONMENT_H
    #define ENVIRONMENT_H
    #include <string>
    #include "type.h"
    
    class environment {
    public:
        virtual ~environment() = 0;
        virtual int register_type(const std::string &, factory) = 0;
        virtual int tid(const std::string &) const = 0;
        virtual object create_object(int) const = 0;
        virtual object create_object(const std::string &) const = 0;
        virtual void *alloc(unsigned long) const;
        virtual void free(void *) const;
    };
    
    inline environment::~environment() {}
    #endif
    

    Und schließlich noch die relevanten Teile des Makefiles:

    CXXFLAGS=-Wall -W -g -O0 -fPIC
    CXX=g++
    cxx=$(CXX) $(CXXFLAGS)
    
    all: calculator 
    
    calculator: main.o libcalculator.so
        $(cxx) -L. -lcalculator -o calculator main.o
    
    main.o: main.cpp calc.h
        $(cxx) -o main.o -c main.cpp
    
    libcalculator.so: calc.o tokenizer.o type.o environment.o
        $(cxx) -shared -o libcalculator.so calc.o type.o environment.o
    
    calc.o: calc.cpp calc.h
        $(cxx) -o calc.o -c calc.cpp
    
    tokenizer.o: tokenizer.cpp tokenizer.h
        $(cxx) -o tokenizer.o -c tokenizer.cpp
    
    type.o: type.cpp type.h
        $(cxx) -o type.o -c type.cpp
    
    environment.o: environment.cpp environment.h environment_impl.h
        $(cxx) -o environment.o -c environment.cpp
    
    [...]
    

    Hat jemand eine Ahnung?

    Ach ja: ich habe eine Klasse environment_impl, die von environment erbt. Diese wird in einer Datei (calc.cpp) instanziiert.



  • Ich glaub ich weiß jetz zumindest, wo der Fehler liegt:
    evironment_impl.h:

    #ifndef ENVIRONMENT_IMPL_H
    #define ENVIRONMENT_IMPL_H
    #include "environment.h"
    #include <map>
    #include <vector>
    
    class environment_impl /*: public environment*/ {
        std::map<std::string, int> tids;
        std::vector<factory> types;
    public:
        environment_impl();
        virtual ~environment_impl();
        virtual int register_type(const std::string &, factory);
        virtual int tid(const std::string &) const;
        virtual object create_object(int) const;
        virtual object create_object(const std::string &) const;
        virtual void *alloc(unsigned long) const;
        virtual void free(void *) const;
    };
    #endif
    

    dieses /: public environment/ war vorher nicht auskommentiert. nur, jetzt machts so keinen sinn. mal weiterschaun.



  • etwas modifiziert krieg ich diese fehler:

    /tmp/ccnJ5IZT.o(.gnu.linkonce.t._ZN11environmentC2Ev+0x8): In function `environment::environment[not-in-charge]()':
    : undefined reference to `vtable for environment'
    /tmp/ccnJ5IZT.o(.gnu.linkonce.d._ZTI16environment_impl+0x10): undefined reference to `typeinfo for environment'
    

    *argh* 😞



  • @Moderatoren: Bitte nach C++ verschieben. Der letzte Test zeigt, dass es nichts mit shared objects zu tun hat.



  • okay


Anmelden zum Antworten