Various patches against gmp-4.3.0

Peter Cordes peter at cordes.ca
Thu May 7 06:55:48 CEST 2009


On Wed, May 06, 2009 at 11:35:04AM +0200, Torbjorn Granlund wrote:
> Marc Glisse <marc.glisse at normalesup.org> writes:
> 
>   On Wed, 6 May 2009, Torbjorn Granlund wrote:
>   
>   >  >   o forgotten_extern_C.difffix for t-locale.cc where an extra extern
>   >  >   "C" is needed
>   >  >
>   >  > Why is this needed?
>   >
>   >  Without the extern "C", the variable "point_string" is subject to C++ name
>   >  mangling: on win32 it becomes "?point_string@@3PADA", leaving an unresolved
>   >  symbol  "_point_string".
>   >
>   > Well, this is a C++ file, point_string is supposed to be a C++ variable,
>   > so if your compiler mangles its use but not its declaration, it sounds
>   > like a serious compiler problem.
>   
>   Apparently this variable is shared with the C file clocale.c, so I
>   think it makes sense that there can be problems. Although I am not
>   sure the standard allows the C++ compiler to mangle the names of
>   arrays (only functions).
> 
> Could an
>   extern "C"
> cause problems for standard conforming compilers?

 No.  In C++, if you want to reference stuff defined in C source
files, the declarations should be inside extern "C" {} blocks.  So the
C++ compiler knows it has to do things the C-compatible way.  As I
understand it, the major effect is to turn off name mangling.  If C++
had a different function calling convention than C on a certain
platform, extern "C" declarations would solve that problem too.  If
structs had different alignment defaults in C vs. C++, you would need
extern "C", too.  (unlikely, I know, but maybe some C++ compilers take
extern "C" to mean "be compatible with some special ABI stuff, not
just the C version of that same compiler.)

 Since you do want to access a C object from C++, you should put the
declaration in an extern "C" block.  As Michiel says, not doing so
happens to work, but like so many other things, happens to work !=
standards conforming and portable.  And AFAIK, extern "C" is standard
C++, and supported by every compiler.

-- 
#define X(x,y) x##y
Peter Cordes ;  e-mail: X(peter at cor , des.ca)

"The gods confound the man who first found out how to distinguish the hours!
 Confound him, too, who in this place set up a sundial, to cut and hack
 my day so wretchedly into small pieces!" -- Plautus, 200 BC


More information about the gmp-devel mailing list