title: c++ loading dylib functions at runtime error
body: <p>So I'm trying to load a .dylib file at runtime in c++ and calling a function within it. It does not seem to be any problem loading the file but when i try to create a function-pointer to the "print" function it's result is NULL.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>/* main.cpp */
  
  #include &lt;iostream&gt;
  #include &lt;string&gt;
  #include &lt;dlfcn.h&gt;
  #include "test.hpp"
  
  int main(int argc, const char * argv[]) {
      std::string path = argv[0];
      std::size_t last = path.find_last_of("/");
  
      // get path to execution folder
      path = path.substr(0, last)+"/";
  
      const char * filename = (path+"dylibs/libtest.dylib").c_str();
  
      // open libtest.dylib
      void* dylib = dlopen(filename, RTLD_LAZY);
  
      if (dylib == NULL) {
          std::cout &lt;&lt; "unable to load " &lt;&lt; filename &lt;&lt; " Library!" &lt;&lt; std::endl;
          return 1;
      }
  
      // get print function from libtest.dylib
      void (*print)(const char * str)= (void(*)(const char*))dlsym(dylib, "print");
  
      if (print == NULL) {
          std::cout &lt;&lt; "unable to load " &lt;&lt; filename &lt;&lt; " print function!" &lt;&lt; std::endl;
          dlclose(dylib);
          return 2;
      }
  
      // test the print function
      print("Herro Word!");
  
      dlclose(dylib);
      return 0;
  }
  </code></pre>
  
  <p>test dylib headerfile</p>
  
  <pre><code>/* test.hpp */
  
  #ifndef test_hpp
  #define test_hpp
  
  void print(const char * str);
  
  #endif
  </code></pre>
  
  <p>the dylib c++ file</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include "test.hpp"
  
  void print(const char * str) {
      std::cout &lt;&lt; str &lt;&lt; std::endl;
  }
  </code></pre>
  
  <p>the output when running is:</p>
  
  <pre><code>unable to load /Users/usr/Library/Developer/Xcode/DerivedData/project/Build/Products/Debug/dylibs/libtest.dylib print function!
  Program ended with exit code: 2
  </code></pre>
  
  <p>I am quite new to c++ and have never loaded dylibs before. Any help would be much appreciated!</p>