This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[C++] extern "C" typedefs
- To: gcc-bugs at gcc dot gnu dot org
- Subject: [C++] extern "C" typedefs
- From: Nathan Sidwell <nathan at codesourcery dot com>
- Date: Mon, 22 May 2000 22:07:03 +0100
- CC: nathan at codesourcery dot com
- Organization: CodeSourcery, LLC
Hi,
I've come across an awkward problem with typedefs in extern "C"
regions. This is culled from the gtkmm & gtk+ libraries, where it is
currently tripping me up. I attach 3 files, header file nathan201.h and
two source files nathan201.C (the library definition), and nathan202.C
(the user code).
The problem is that, unless -pedantic is given, a typedef of the form
typedef void (*fptr) ();
is treated as
typedef void (*fptr) (...);
rather than
typedef void (*fptr) (void);
[this extension is not documented in extend.texi]
nathan202.C is valid code, but will not compile unless -pedantic is given
because otherwise `fn' cannot be converted to `void (*) (...)'.
nathan@uha:15>./g++ -B ./ -c current/nathan202.C
current/nathan202.C: In function `int main ()':
current/nathan202.C:8: cannot convert `void (*) ()' to `void (*) (...)'
for argument `1' to `f1 (void (*) (...))'
So we have an extension which is breaking conformant code -- bad, but
it gets worse.
So compile nathan202.C with -pedantic, and then link it with nathan201.o.
Unless nathan201.C was also compiled with -pedantic, we'll get a link error
because f1 will be mangled differently.
nathan@uha:14>./g++ -B ./ -o current/main nathan201.o nathan202.o
nathan202.o: In function `main':
nathan202.o(.text+0x1f): undefined reference to `f1(void (*)(void))'
collect2: ld returned 1 exit status
Now, I can compile the library with -pedantic, but then I have to leave it
somewhere wierd (or convince the sysadmin to do it for me). But, if I do
this, then other users will get link errors, unless they to compile
with -pedantic. Arrrg!
Now, one solution is to change the typedef, but I think we should fix
the compiler, if possible. Does anyone know where this extension is in
active use?
nathan
--
Dr Nathan Sidwell :: http://www.codesourcery.com :: CodeSourcery LLC
'But that's a lie.' - 'Yes it is. What's your point?'
nathan@codesourcery.com : http://www.cs.bris.ac.uk/~nathan/ : nathan@acm.org
extern "C" {
typedef void (*Fptr1) ();
typedef void (*Fptr2) (void);
};
void f1 (Fptr1);
void f2 (Fptr2);
#include "nathan201.h"
void f1 (Fptr1) {};
void f2 (Fptr2) {};
#include "nathan201.h"
void fn () {};
int main ()
{
f1 (&fn);
f2 (&fn);
return 0;
}