From owner-freebsd-questions Sun Oct 15 0:36:44 2000 Delivered-To: freebsd-questions@freebsd.org Received: from web6304.mail.yahoo.com (web6304.mail.yahoo.com [128.11.22.141]) by hub.freebsd.org (Postfix) with SMTP id D0D9C37B670 for ; Sun, 15 Oct 2000 00:36:41 -0700 (PDT) Message-ID: <20001015073641.15276.qmail@web6304.mail.yahoo.com> Received: from [64.161.130.86] by web6304.mail.yahoo.com; Sun, 15 Oct 2000 00:36:41 PDT Date: Sun, 15 Oct 2000 00:36:41 -0700 (PDT) From: Christian Lacunza Subject: Re: How to compile and link a lib for use with dlopen() ? To: Nicolai Petri Cc: freebsd-questions@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Sender: owner-freebsd-questions@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG hi Nicolai. try this code: -------------------- test.cpp -------------------- #include class A { }; extern "C" { A* GetNewA() /* This function I want exported */ { cout << "returning a new A" << endl; return new A; } }; -------------------- use.cpp -------------------- #include 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 > > class A > { > public: > int GetValue(); > void SetValue(int); > }; > > extern "C" > { > A* GetNewA() /* This function I want exported */ > { > return new A; > } > }; > > ------ use.cpp ---------- > #include > > 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