Re: [anjuta-list] Is something like this exists for gcc/configure & friends



Sebastien,

On Thu, Apr 14, 2016 at 12:29 PM, Sébastien Granjoux <seb sfo free fr> wrote:
Hi Igor,


Le 13/04/2016 21:38, Igor Korot a écrit :

In Windows I can write a special file (.def file) where I can choose the
name of
my exported functions. Or I can use /EXPORT linker option to give the
linker
those names explicitly.
Is such functionality exists in the gcc realm? Either as a command line
linker
option, an __attribute__ directive or #pragma directive?


gcc realm is easier, this functionality doesn't exist because all functions
are exported.

That's why I personally prefer to work in Linux. ;-)



I'm trying to avoid writing 'extern "C"' for every exported function
in the .so file...


extern "C" is quite different, it's used to tag a function as C in a C++
program.

Yes.
So here is the story.

Originally my code was look like this:

extern "C"
#ifdef WIN32
__declspec(dllexport)
#endif
void *MyFunc()
{
}

However, since I found out I need to return a C++ object I started to
look for a way to use a original function name.

In MSVC the function name will be exported as "@vMyFunc v" or
something like this.
With gcc the function name will be exported as "vMyFunc v" or
something like this.

The point is: the name of the exported function will not be kept, and
I will need to look
for the mangled name in order for the function  to be found either
with ::GetProcAddress()
on Windows, or dlsym() in Linux.

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.

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".

Now if the same functionality exist in gcc/linker or configure
&friends, it would be nice.

Basically I'm after the option which will allow me to use a function
name as "MyFunc" and not
"v MyFunc@v" or anything like this:

Is such functionality exists? Either as a linker option or gcc option.

Thank you.



But since C++ will mangle the names I want to keep the names as they are
written
in the source code.


I think you can use mangled in shared libraries too.

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?

Thank you.



Best Regards,

Sébastien


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]