]> Creatis software - gdcm.git/blob - Testing/loadmodule.cxx
use GDCM_NAME_SPACE:: instead of gdcm::, even in Examples ...
[gdcm.git] / Testing / loadmodule.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: loadmodule.cxx,v $
5   Language:  C++
6   Date:      $Date: 2008/09/15 15:49:21 $
7   Version:   $Revision: 1.4 $
8                                                                                 
9   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
10   l'Image). All rights reserved. See Doc/License.txt or
11   http://www.creatis.insa-lyon.fr/Public/Gdcm/License.html for details.
12                                                                                 
13      This software is distributed WITHOUT ANY WARRANTY; without even
14      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15      PURPOSE.  See the above copyright notices for more information.
16                                                                                 
17 =========================================================================*/
18 /* For some bizarre reason global symbol, mainly std::string
19  * are creating seg fault when loading a shared lib, build on top
20  * of a static gdcm lib */
21
22 #include "gdcmFile.h"
23 #include <dlfcn.h>
24
25 int main(int, char* [])
26 {
27   //typedef itk::DynamicLoader dl;
28   typedef void* lh;
29   
30   // the type of function stored in the dynamic lib 
31   typedef void (*fnptr)(void);
32   
33   //This function gives a segmentation fault.
34   //std::cerr << "Last error before opening: " << dl::LastError() << std::endl;
35
36   //Fill in here  <path_to_lib>/libadll.so"
37   std::string libname = GDCM_LIBRARY_OUTPUT_PATH "/libdynmodule.so";
38
39   //open the dynamic lib
40   //lh handle =  dl::OpenLibrary( libname.c_str() );
41   lh handle =  dlopen(libname.c_str(), RTLD_LAZY);
42   std::cerr << "address of handle: " << handle << std::endl;
43   if (!handle)
44     {
45     std::cerr << "Ooops cannot open lib:" << libname << std::endl;
46     return 1;
47     }
48  
49   //std::cerr << "Error after opening: " << dl::LastError() << std::endl;
50
51   
52   std::cerr << "Getting function address from lib" << std::endl;
53   void * aptr = dlsym(handle, "afunc");
54   
55   if (aptr == 0)
56   {
57     std::cerr << "error in finding function" << std::endl;
58     //const char *er = dlerror();
59     //std::cerr << "dl says: " << dl::LastError() << std::endl;
60
61     std::cerr << "Closing libs" << std::endl;
62     //dl::CloseLibrary(handle);
63     dlclose(handle);
64     //std::cerr << "dl says: " << dl::LastError() << std::endl;
65
66     std::cerr << "Exiting" << std::endl;
67     return 1;
68   }
69
70   
71   std::cerr << "Casting function address from lib" << std::endl;
72   fnptr afuncptr = (fnptr)(aptr);
73
74   std::cerr << "Calling function from lib" << std::endl;
75   afuncptr(); 
76
77   // Create an instance of the GDCMImageIO class. This makes sure
78   // that the problematic gdcm-library is linked.
79   //itk::GDCMImageIO::Pointer gdcmio = itk::GDCMImageIO::New();
80   GDCM_NAME_SPACE::File *file = GDCM_NAME_SPACE::File::New();
81   file->Print(); // to avoid warning
82   
83   std::cerr << "Closing libs" << std::endl;
84   //dl::CloseLibrary(handle);
85   dlclose(handle);
86
87   //std::cerr << "dl says: " << dl::LastError() << std::endl;
88   
89   std::cerr << "Exiting" << std::endl;
90   return 0;
91   
92 }
93
94