# Exporting C++ Classes

This topic is 4273 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

In windows with VS when you compile a dll you get a lib file which you use to link with your programs, So the linker can know the symbols and everything. From what i understood in linux you link directly to the "DLL"(shared library). If this is the case how do you export C++ classes? Thanks

##### Share on other sites
Actually, the lib file is unneccessary with a DLL, and that's kind of the idea. The library is dynamic linking in nature, hence the DLL name (Dynamic Link Library) - it links/loads at run time through the function LoadLibrary() and known extern-ed functions can be accessed via GetProcAddress(), I'm sure that was known though but I included it for clarity.

Here is a good tutorial outlining both compiling without using the generated lib and header and without:

http://www.flipcode.com/articles/article_creatingdlls.shtml

Hope that helps!

##### Share on other sites
I know about run-time dynamic linking. But what I'm looking for is
and AFAIK you can't import C++ classes using GetProcAddress(unless you create some
stub function like createTheClass(TheClass **obj) )
but with the generated lib file you can use them directly(the code itself
still resides in the dll).

Any other idea?

##### Share on other sites
Just a reminder I want to do it under linux.
I was using the Windows terminology just because that's what I'm
familiar with.

##### Share on other sites
C++ dlopen mini HOWTO.

Hope this helps.

##### Share on other sites
I read this thread a couple of times, but I don't get where the problem is. :(

if you have some whatever.so and an appropriate header( like whatever.h ), then all you need to do is using the header( for symbol-declaration ) and linking against that shared lib.

e.g.

the library: $gcc -shared whatever.o -o libwhatever.so your executable:$ gcc -o thegame -I/path/to/libwhatever/include/stuff -L/path/to/libwhatever/libfile -lwhatever thegame.o
--
And then ofcourse its linked dynamicly.

If that is not what you're searching for then please bring an example or something, how it's intended to be.

So saying "you don't link a libfile in" is right, but instead you (may?!) use the .so-file directly.

##### Share on other sites
That what i was asking for.
So I got my answer thanks.
I wasn't sure because in Windows you can't export classes using only the DLL.

Cheers.

##### Share on other sites

If you're, instead, wanting to link to the shared object at build-time:

// foo.hh#ifndef FOO_HH_INCLUDED#define FOO_HH_INCLUDEDclass foo {public:  foo();  ~foo();  void bar() const;};#endif

// foo.cc#include "foo.hh"#include <iostream>foo::foo() {  std::cout << __PRETTY_FUNCTION__ << std::endl;}foo::~foo() {  std::cout << __PRETTY_FUNCTION__ << std::endl;}void foo::bar() const {  std::cout << __PRETTY_FUNCTION__ << std::endl;}

// main.cc#include "foo.hh"int main() {  foo f;  f.bar();}

$g++ -fPIC -shared -o libfoo.so foo.cc$ g++ main.cc -o main -L . -lfoo\$ LD_LIBRARY_PATH=. ./mainfoo::foo()void foo::bar() const;foo::~foo()

In either case, there is no need for anything like a ".lib" file.

EDIT: too late. I'm leaving this in case it's useful to someone.

Thanks.

1. 1
2. 2
Rutin
20
3. 3
khawk
16
4. 4
A4L
14
5. 5

• 11
• 16
• 26
• 10
• 11
• ### Forum Statistics

• Total Topics
633756
• Total Posts
3013708
×