librairie dynamique et instanciation
Bonsoir, après avoir lu le tutorial http://hiko-seijuro.developpez.com/a...que-dynamique/ sur la création de librairie dynamique en c++ sous linux et testé son exemple, je m'interroge sur la manière d'instancier un objet dans le code de la librairie
mettons la classe "circle" (issue du tuto):
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
|
circle.h
#ifndef CIRCLE_H_
#define CIRCLE_H_
#include <iostream>
class circle
{
public:
virtual void draw();
};
typedef circle *(*maker_circle)();
#endif
circle.cpp
#include "circle.h"
using namespace std;
void circle::draw()
{
cout << " ### " << endl;
cout << " # # " << endl;
cout << " # # " << endl;
cout << " # # " << endl;
cout << " # # " << endl;
cout << " ### " << endl;
}
extern "C"
{
circle *make_circle()
{
return new circle();
}
} |
j'ai voulu ajouter une autre classe "test" :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
|
test.h
#ifndef TEST_H_
#define TEST_H_
#include <iostream>
class test
{
public:
Test();
~Test();
void print();
};
#endif
test.cpp
#include "test.h"
using namespace std;
Test::Test(){}
Test::~Test(){}
void Test::print(){
cout << " TEST " << endl;
} |
j'ai voulu l'instancier dans la méthode "draw" de "circle"
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
void circle::draw()
{
cout << " ### " << endl;
cout << " # # " << endl;
cout << " # # " << endl;
cout << " # # " << endl;
cout << " # # " << endl;
cout << " ### " << endl;
Test* test = new Test();
test->print();
} |
voici le main.cpp
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
|
#include "circle.h"
#include "test.h"
#include <cstdlib>
#include <iostream>
#include <dlfcn.h>
using namespace std;
int main(int argc, char **argv)
{
void *hndl;
maker_circle pMaker;
// Ouverture de la librairie
hndl = dlopen("./libcircle.so", RTLD_LAZY);
if(hndl == NULL) {
cerr << "dlopen : " << dlerror() << endl;
exit(EXIT_FAILURE);
}
// Chargement du créateur
void *mkr = dlsym(hndl, "make_circle");
if (mkr == NULL)
{
cerr << "dlsym : " << dlerror() << endl;
exit(EXIT_FAILURE);
}
pMaker = (maker_circle)mkr;
// Création, affichage puis destruction du cercle
circle *my_circle = pMaker();
my_circle->draw();
dlclose(hndl);
return EXIT_SUCCESS;
} |
je compile en librairie dynamique sans problème avec
Code:
1 2 3 4 5 6
|
libcircle.so: circle.cpp circle.h test.h test.cpp
g++ -shared -o libcircle.so circle.cpp
example: main.cpp libcircle.so
g++ -o example main.cpp -ldl |
mais à l'exécution, j'obtiens un "undefined symbol: _ZN4TestC1Ev"
ai-je mal instancié ? :?