Exporting C++ Classes
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
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!
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!
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?
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?
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.
I was using the Windows terminology just because that's what I'm
familiar with.
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.
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.
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.
So I got my answer thanks.
I wasn't sure because in Windows you can't export classes using only the DLL.
Cheers.
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:
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.
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement