dynamische Libraries



  • Hallo Leute.

    Ich will ein Plugin-System schreiben - welches automatisch dynamische Libraries laedt. In diesen dynamischen Libraries werden dann bestimmte Funktionen aufgerufen, etc. (ein simples Plugin-System halt :))

    Doch leider habe ich damit unter Unix keine Ahnung. Ich nehme mal an, es wird aehnlich wie unter Windows gehen?

    Waere nett wenn ihr mir ein paar Links und/oder ein paar gute Ratschlaege geben koenntet.

    Danke schonmal!



  • kuck mal in der manpage von dlsym da is unten nen ganz gutes beispiel.



  • Sehr schoen! Danke! 👍



  • Mal ein kleiner Auszug aus einer Main. Man holt sich eine Klasseninstanz aus der Lib

    typedef helworld* (*factory)(void);
    
    int  main(void)
    {
    void *h_helloworld = NULL;
    factory make;
    helworld *math;
    
    //char error[20];
    
        h_helloworld = dlopen("libhelloworld.so",RTLD_LAZY);
    //    h_helloworld = dlopen("/eigenlibs/libhelloworld.so",RTLD_LAZY);
        if (!h_helloworld)
        {
    	cout << "Lib nicht gefunden" << endl;	
        }
        else
        {
    	make = (factory)dlsym(h_helloworld,"gethelworld");
    	if (!make)
    	{
    	    cout << "Funktion nicht gefunden" << endl;
    	}
    	else
    	{
    
        math = (*make)();
    
        math->hellow();		
        math->meinfunc();		
    
    	}
    	dlclose(h_helloworld);
        }
    

    Die lib

    extern "C"
    {
        helworld *gethelworld()
        {
    	return new helworld;
        }
    
    }
    
    void helworld::hellow(void)
    {
    
    cout << "hallo welt die 2te" << endl;
    
    }
    
    int helworld::meinfunc(void)
    {
    
    cout << "meinfunc die 2te" << endl;
    return 1;
    }
    

Anmelden zum Antworten