Re: name mangling problem
On Jul 23, 12:53 pm, p...@informatimago.com (Pascal J. Bourguignon)
wrote:
Jaco Naude <naude.j...@gmail.com> writes:
I am running into a problem where I am trying to import C DLL into a
Visual C++ 2008 application. The functions in the C DLL are obviously
not name mangled (verified using Dependency Walker). When I try to use
any of the functions from the DLL, VC++ mangles these names (or so it
looks like) and complains that it can't link to mangled versions of
the function names. To avoid this I've figured that I need to declare
the functions I would like to use from the DLL as follows:
extern "C"
{
void Py_Initialize(void);
}
Is there any easier way of doing this than to declare all the
functions in the DLL using extern "C"?
Yes:
#define C(X) extern "C" { X; }
C(void Py_Initialize(void))
C(void Py_Middlize(void))
C(void Py_Terminalize(void))
Assuming typing 'C( )' is easier than typing 'extern "C"{ }'...
That is, quite frankly, horrible. And what happens if you use C
as a name somewhere? Not to mention that the person reading the
code will have no idea what is going on.
With any decent editor, you can set up an abbreviation so that
you just have to type C to get your extern "C", if it is really
typing which is bothering you. (Although seriously, how long
does it take to type `extern "C"?) More generally, however, the
usual solution is to define an extern "C" block for the entire
library, e.g.:
#ifdef __cplusplus
extern "C" {
#endif
void Py_Initialize( void ) ;
void Py_Middlize( void ) ;
// ...
#ifdef __cplusplus
}
#endif
(I don't normally approve of #ifdef, but this is one of those
cases where it is so ubiquious, any other solution would cause
the reader to wonder why it wasn't done this way.)
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34