1 // Copyright (C) 2006-2019 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "DynLibLoaderGNU.hxx"
23 #include "Exception.hxx"
25 using namespace YACS::BASES;
27 #if defined(__APPLE__)
28 const char DynLibLoaderGNU::_extForDynLib[]=".dylib";
30 const char DynLibLoaderGNU::_extForDynLib[]=".so";
33 DynLibLoaderGNU::DynLibLoaderGNU(const std::string& libNameWithoutExtension):_libName(libNameWithoutExtension),
38 DynLibLoaderGNU::~DynLibLoaderGNU()
40 if(_handleOnLoadedLib)
41 dlclose(_handleOnLoadedLib);
44 bool DynLibLoaderGNU::isLibFileFindable() const
49 std::string DynLibLoaderGNU::getLibNameWithoutExt() const
55 Append a directory with name \b dirName to the searching paths.
56 \return If append succeeds 0 is returned.
57 If the directory does not exists 1 is returned.
58 If the addition of directory causes some troubles due to seach paths name 2 is returned.
60 int DynLibLoaderGNU::appendDirInSearchPath(const std::string& dirName)
66 Removes a directory with name \b dirName from the searching paths.
67 \return If removal succeeds 0 is returned.
68 If the directory does not exists 1 is returned.
69 If the path were not already in existing paths 2 is returned.
71 int DynLibLoaderGNU::removeDirInSearchPath(const std::string& dirName)
76 void *DynLibLoaderGNU::getHandleOnSymbolWithName(const std::string& symbName, bool stopOnError)
78 if(!_handleOnLoadedLib)
79 if(!isLibFileFindable())
81 std::cerr << "Dynamic library with name " << symbName << _extForDynLib;
82 std::cerr << " not existing in paths specified" << std::endl;
87 return resolveSymb(symbName, stopOnError);
90 bool DynLibLoaderGNU::load()
92 std::string fullLibName(_libName);
93 fullLibName+=_extForDynLib;
95 _handleOnLoadedLib=dlopen(fullLibName.c_str(),RTLD_LAZY | RTLD_GLOBAL);
96 char *message=dlerror();
99 std::string error = "Error while trying to load library with name " + fullLibName +
100 " with the following internal message: " + message;
101 throw YACS::Exception(error);
103 return _handleOnLoadedLib != NULL;
106 bool DynLibLoaderGNU::unload()
108 if (_handleOnLoadedLib)
110 dlclose(_handleOnLoadedLib);
111 _handleOnLoadedLib = NULL;
115 bool DynLibLoaderGNU::reload()
121 void *DynLibLoaderGNU::resolveSymb(const std::string& symbName, bool stopOnError)
124 void *ret=dlsym(_handleOnLoadedLib,symbName.c_str());
125 char *message=dlerror();
126 if(stopOnError && (NULL != message))
128 std::string error="Error detected on symbol ";
129 error=error+symbName +" search in library with name "+_libName+_extForDynLib+" with the following internal message "+message;
130 throw YACS::Exception(error);
136 const char *DynLibLoaderGNU::getExtensionForDynLib()
138 return _extForDynLib+1;