Jump to content
  • Advertisement
Sign in to follow this  
CableGuy

Exporting C++ Classes

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
Share on other sites
I know about run-time dynamic linking. But what I'm looking for is
load time linking(I don't know how it is called exactly).
I found some info on the net about this but it only concerned regular
C functions, and I'm interested in loading C++ classes.
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
If you're using GetProcAddress() on Windows, then you're probably after runtime loading, which is what the mini HOWTO I linked to above is all about.

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


// foo.hh
#ifndef FOO_HH_INCLUDED
#define FOO_HH_INCLUDED

class 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=. ./main
foo::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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!