Re: [anjuta-list] Is something like this exists for gcc/configure & friends
- From: Igor Korot <ikorot01 gmail com>
- To: Jens Mühlenhoff <j muehlenhoff gmx de>
- Cc: anjuta-list gnome org
- Subject: Re: [anjuta-list] Is something like this exists for gcc/configure & friends
- Date: Fri, 15 Apr 2016 10:24:52 -0400
Hi, Jens,
On Fri, Apr 15, 2016 at 5:25 AM, Jens Mühlenhoff <j muehlenhoff gmx de> wrote:
Hello Igor.
Am 14.04.2016 um 19:11 schrieb Igor Korot:
extern "C" makes it work as now the function will be exported without
name mangling.
However, since I need to return a C++ object this will not work as the
function has C-linkage.
What do you mean with will not work?
A pointer is a pointer. You can return a pointer to a C++ object using an extern "C" function.
Consider following piece of code:
class Base
{
public:
virtual void MyPureVirtual() = 0;
};
class Derived : public Base
{
public:
virtual void MyPureVirtual() { };
};
extern "C" Base *MyFunc()
{
Base *pd = new Derived();
return pd;
}
(NOTE: MSVC-specifics removed)
Using extern "C" doesn't help since C does not know anything about
inheritance, C++
objects and C++ interfaces.
So when running this code I will get just a plain old pointer in the
memory (an address).
Now when I remove the 'extern "C"' it will work because I'm using C++
features with the
C++ code.
I can see it clearly in the debugger - running the original code when
I get out of MyFunc()
all I see is an untyped pointer to memory (an address). When I comment
out ' extern "C" '
the caller of MyFunc() does see a pointer to base which is a Derived object.
So with MSVC I can use "/EXPORT:" link option to assign a name to the
exported function.
So, if I give a linker following "/EXPORT:MyFunc", MSVC will find the
appropriate function
and will export it with the name "_MyFunc", as if I use 'extern "C"'
instead of "@v MyFunc@v".
Isn't this even more complicated then declaring a extern "C" block?
Nope - it is actually easier.
Instead of writing ' extern "C" ' on each function I just give a
linker " /EXPORT:MyFunc "
option (without a quotes). Then the code will behave as if ' extern
"C" ' was used.
I just need to know if something like this exists for gcc/link.
You mean I can write something like this:
#ifdef WIN32
__declspec(dllexport)
#endif
MyClass *MyFunc()
{
}
and then do this:
void *dl_ptr = dlopen("my_dll" );
MYFUNC func = (MYFUNC) dlsym( dl_ptr, "MyFunc" );
and it will work. This code will find MyFunc inside libmy_dll.so
library and everything will be good?
That won't work.
PS: Have you read this page?
http://www.tldp.org/HOWTO/html_single/C++-dlopen/
You can either flatten your class and make the instance an opaque pointer (which is great for using C++
classes in languages other than C++) or export an abstract class (also known as an interface) to be
consumed by C++ clients.
I just went thru the article mentioned.
It appears it is for *nix only.
I need a generic solution for the problem explained above.
However I will look at it more when I come home from work.
Thank you.
--
Mit freundlichen Grüßen
Jens Mühlenhoff
_______________________________________________
anjuta-list mailing list
anjuta-list gnome org
https://mail.gnome.org/mailman/listinfo/anjuta-list
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]