Call a C++ function from a C++ library in Linux
<p>I want to call a function from C++ library on linux. I have a shared object of that library.
I want to call method getAge() that return an int from ydmg library.</p>

<p>Following is the code that I have written:</p>

<p>testydmg.cpp</p>

<pre><code>#include "ydmg/bd.h"
#include "yut/hash.h"
#include "dlfcn.h"

extern "C" int getAge();

class testydmg{
   public:
        testydmg::testydmg(const yutHash&amp; user){

        }
         testydmg::testydmg(){

        }

         testydmg::~testydmg(){

        }


        int testydmg::getFunction(){
                void *handle;
                int (*voidfnc)();
                handle = dlopen("ydmg.so",RTLD_LAZY);
                if(handle == NULL){
                        printf("error in opening ydmg lib");
                } else {
                        voidfnc = (int (*)())dlsym(handle, "getAge");
                        (*voidfnc)();
                        printf("class loaded");
                }
                ydmgBd obj;
                obj.getAge();
                printf("Inside getFunction()...");
                dlclose(handle);

        }
};
</code></pre>

<p>I compile and link the code as below:</p>

<blockquote>
  <p>gcc -fPIC -shared -l stdc++ -I/home/y/libexec64/jdk1.6.0/include -I/home/y/libexec64/jdk1.6.0/include/linux -I/home/y/include testydmg.cpp -o libTestYdmg.so libydmg.so</p>
</blockquote>

<p>Then I check for the method in the new shared object i.e. libTestYdmg.so</p>

<blockquote>
  <p>nm -C libTestYdmg.so | egrep getAge
  I get nothing by running the above command.</p>
</blockquote>

<p>Does it mean that it did not get the method getAge() from the library.
Could you please correct where I am going wrong ?</p>
