Undefined reference Fehler wenn alles Okay ist ?!



  • Help
    Ich habe eine template-Klasse geschrieben, und auf H und CPP aufgeteilt wie eine normale Klasse. Ich habe wirklich keine Ahnung was der Compiler von mir will.
    NetBeans 6.9 mit GNU GCC compiler.
    Linux Ubuntu

    [ ... ]
    build/Debug/GNU-Linux-x86/main.o: In function `main':
    /home/apieceofcode/apps/mess_DL_app/main.cpp:38: undefined reference to `FACTORY<BASE>::FACTORY()'
    /home/apieceofcode/apps/mess_DL_app/main.cpp:39: undefined reference to `FACTORY<BASE>::load(char*)'
    /home/apieceofcode/apps/mess_DL_app/main.cpp:40: undefined reference to `FACTORY<BASE>::operator()()'
    /home/apieceofcode/apps/mess_DL_app/main.cpp:42: undefined reference to `FACTORY<BASE>::close()'
    /home/apieceofcode/apps/mess_DL_app/main.cpp:43: undefined reference to `FACTORY<BASE>::~FACTORY()'
    /home/apieceofcode/apps/mess_DL_app/main.cpp:43: undefined reference to `FACTORY<BASE>::~FACTORY()'
    collect2: ld returned 1 exit status
    make[2]: *** [dist/Debug/GNU-Linux-x86/mess_dl_app] Error 1
    make[1]: *** [.build-conf] Error 2
    make: *** [.build-impl] Error 2
    
    BUILD FAILED (exit value 2, total time: 1s)
    
    // main.cpp
    #include <cstdlib>
    #include <iostream>
    #include <dlfcn.h>
    #include "base.h"
    #include "FACTORY.h"
    
    using namespace std;
    
    int main(int argc, char** argv) {
    
        FACTORY<BASE> f = FACTORY<BASE>();
        f.load("/home/apieceofcode/libmess_DL_lib1.so");
        BASE *test = f();
        cout << test->who() << endl;
        f.close();
        return 0;
    }
    
    // FACTORY.h
    //#ifndef FACTORY_H
    //#define	FACTORY_H
    
    #include <dlfcn.h>
    #define NULL 0
    
    template<class T>
    class FACTORY {
    public:
        typedef T*(*MAKER)();
    
        FACTORY();
        virtual ~FACTORY();
    
        T* operator()(); // maker(.)
        void* operator()(char* symbol); // dlsym(.)
    
        bool load(char* filename);
        bool close();
        char* getname();
    
    private:
        char* filename;
        char* classname;
        void* handle;
        MAKER mkr;
    };
    
    //#endif	/* FACTORY_H */
    
    // FACTORY.cpp
    #include "FACTORY.h"
    
    template<class T> FACTORY<T>::FACTORY() {
        handle = NULL;
        mkr = NULL;
        filename = "";
    }
    
    template<class T> FACTORY<T>::~FACTORY() {
        if (handle)
            dlclose(handle);
    }
    
    template<class T>T* FACTORY<T>::operator ()() {
        return mkr();
    }
    
    template<class T>void* FACTORY<T>::operator ()(char* symbol) {
        return dlsym(handle, symbol);
    }
    
    template<class T>bool FACTORY<T>::load(char* filename) {
        handle = dlopen(filename, RTLD_LAZY);
        if (!handle) return false;
    
        mkr = (MAKER)dlsym(handle, "DL_mkr");
        if (!mkr) return false;
    
        typedef char* (*NAMEGETTER)();
        NAMEGETTER tmp = (NAMEGETTER)dlsym(handle, "DL_name");
        if (!tmp) return false;
        classname = tmp();
        if (!classname) return false;
    
        return true;
    }
    
    template<class T> bool FACTORY<T>::close() {
        if (handle)
            dlclose(handle);
    }
    




  • Schau dir mal an wie Templates kompiliert werden.
    Modell der einschließenden Kompilierung und Modell der getrennten Kompilierung.

    Lg freeG


Anmelden zum Antworten