Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 15 Oct 2000 00:36:41 -0700 (PDT)
From:      Christian Lacunza <celacunza@yahoo.com>
To:        Nicolai Petri <npp@distortion.dk>
Cc:        freebsd-questions@freebsd.org
Subject:   Re: How to compile and link a lib for use with dlopen() ?
Message-ID:  <20001015073641.15276.qmail@web6304.mail.yahoo.com>

next in thread | raw e-mail | index | archive | help

hi Nicolai.

try this code:

     -------------------- test.cpp --------------------
     #include <iostream.h>
     
     class A
     {
     };
     
     extern "C"
     {
         A* GetNewA()    /* This function I want exported */
         {
             cout << "returning a new A" << endl;
             return new A;
         }
     };
     
     -------------------- use.cpp --------------------
     #include <dlfcn.h>
     
     class A;
     typedef A* (func_t)();
     
     int main()
     {
         void*   h    =           dlopen( "./test.so.1", RTLD_NOW );
         func_t* GetA = (func_t*) dlsym( h, "GetNewA" );
         A* test = GetA();
     };

with these commands:

     $ CC -fPIC -c test.cpp
     $ CC -shared -o test.so.1 test.o
     $ CC -o use use.cpp
     $ ./use
     returning a new A
     $ 

-- christian.

> I`m porting some software from Windows (I know it....) and it is using
> external dll`s that`s dynamic loaded with LoadModule() and GetProcAddr().
> The Unix equivalent i think is dlopen() and dlsym().
> 
> My question is how to compile and link the following 2 pieces of code to
> make it work.
> 
> The code is just out of the head written and just as an example... It can`t
> compile,
> 
> I`ve tried some different compiler settings... But `nm` does not list my
> exported functions... ?? Please help.
> 
> ----------- utility.cpp ----------- This should be in test.so.1 lib.
> #include <stuff.>
> 
> class A
> {
>     public:
>     int GetValue();
>     void SetValue(int);
> };
> 
> extern "C"
> {
>     A* GetNewA()    /* This function I want exported */
>     {
>         return new A;
>     }
> };
> 
> ------ use.cpp ----------
> #include <stuff.h>
> 
> typedef  (A*)(EntryFunc*)();
> 
> int main()
> {
>     int h = dlopen("test.so.1");
>     EntryFunc GetA=dlsym(h,"GetNewA");
>     A* test = GetA();
>  /* Do other stuff */
> };
> 
> 
> Thanx for any help you can give me :o).
> 
> ---
> Nicolai Petri


__________________________________________________
Do You Yahoo!?
Yahoo! Messenger - Talk while you surf!  It's FREE.
http://im.yahoo.com/


To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-questions" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20001015073641.15276.qmail>