All Classes Namespaces Functions Variables Typedefs Enumerations
getsharedlibpath_impl.h
1 /*
2  * Author: Markus Friedrich
3  *
4  * This file is free and unencumbered software released into the public domain.
5  *
6  * Anyone is free to copy, modify, publish, use, compile, sell, or
7  * distribute this software, either in source code form or as a compiled
8  * binary, for any purpose, commercial or non-commercial, and by any
9  * means.
10  *
11  * In jurisdictions that recognize copyright laws, the author or authors
12  * of this software dedicate any and all copyright interest in the
13  * software to the public domain. We make this dedication for the benefit
14  * of the public at large and to the detriment of our heirs and
15  * successors. We intend this dedication to be an overt act of
16  * relinquishment in perpetuity of all present and future rights to this
17  * software under copyright law.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
22  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
23  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
24  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25  * OTHER DEALINGS IN THE SOFTWARE.
26  *
27  * For more information, please refer to <http://unlicense.org/>
28  */
29 
30 #ifndef MBXMLUTILS_SHAREDLIBNAME
31 # error "MBXMLUTILS_SHAREDLIBNAME must be defined before including this implementation file."
32 #endif
33 
34 #include <boost/preprocessor/cat.hpp>
35 #ifdef _WIN32
36 # include <windows.h>
37 #else
38 # ifndef _GNU_SOURCE
39 # define _GNU_SOURCE // dladdr requires _GNU_SOURCE to be defined
40 # endif
41 # include <dlfcn.h>
42 # include <unistd.h>
43 #endif
44 
45 namespace {
46 
47 #ifdef _WIN32
48 extern "C" void *__ImageBase;
49 #else
50 char buffer[2048];
51 std::string pathAtLoadTime=getcwd(buffer, sizeof(buffer));
52 #endif
53 
54 }
55 
56 namespace MBXMLUtils {
57 
58 std::string BOOST_PP_CAT(get, BOOST_PP_CAT(MBXMLUTILS_SHAREDLIBNAME, SharedLibPath))() {
59  static std::string ret;
60  if(!ret.empty())
61  return ret;
62 
63  // get the shared library file path containing this function
64 #ifdef _WIN32
65  char moduleName[2048];
66  GetModuleFileName(reinterpret_cast<HMODULE>(&__ImageBase), moduleName, sizeof(moduleName));
67  ret=moduleName;
68 #else
69  Dl_info info;
70  dladdr(reinterpret_cast<void*>(&BOOST_PP_CAT(get, BOOST_PP_CAT(MBXMLUTILS_SHAREDLIBNAME, SharedLibPath))), &info);
71  // convert to absolute path and return
72  std::string name(info.dli_fname);
73  ret=name[0]=='/'?name:pathAtLoadTime+"/"+name;
74 #endif
75  return ret;
76 }
77 
78 }
79 
80 #undef MBXMLUTILS_SHAREDLIBNAME

Impressum / Disclaimer / Datenschutz Generated by doxygen 1.8.5 Valid HTML