* guile-2.0 scm_t_subr typedef
@ 2011-03-01 1:50 David Fang
2011-03-01 20:33 ` Ludovic Courtès
0 siblings, 1 reply; 2+ messages in thread
From: David Fang @ 2011-03-01 1:50 UTC (permalink / raw)
To: guile-devel
Hi,
I'm running into a compile-time problem passing a C function
pointer to scm_c_define_gsubr, compiling in C++. And yes, I know I'm
using -Werror.
e.g.:
cc1plus: warnings being treated as errors
../../../src/guile/hackt-config.cc: In function 'void
HAC::guile_wrap::wrap_package_string_init()':
../../../src/guile/hackt-config.cc:32: warning: ISO C++ forbids casting
between pointer-to-function and pointer-to-object
This is due to the installed definition of scm_t_subr in libguile/__scm.h:
/* The type of subrs, i.e., Scheme procedures implemented in C. Empty
function declarators are used internally for pointers to functions of
any arity. However, these are equivalent to `(void)' in C++, are
obsolescent as of C99, and trigger `strict-prototypes' GCC warnings
(bug #23681). */
#ifdef BUILDING_LIBGUILE
typedef SCM (* scm_t_subr) ();
#else
typedef void *scm_t_subr;
#endif
which is used in the prototype in libguile/gsubr.h:
SCM_API SCM scm_c_define_gsubr (const char *name,
int req, int opt, int rst, scm_t_subr fcn);
and after installation, BUILDING_LIBGUILE is no longer defined. Had the
typedef been kept as SCM (*scm_t_subr)(), then the C++ compiler would have
no reason to complain, as no cast between *function and *object would be
needed.
Can someone explain the rationale for the conditional typedef?
Would it be possible to change it back to a true function pointer?
Even it were conditionally defined to:
typedef void (*scm_t_subr)();
that would be good enough to proceed.
Fang
David Fang
http://www.csl.cornell.edu/~fang/
http://www.achronix.com/
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: guile-2.0 scm_t_subr typedef
2011-03-01 1:50 guile-2.0 scm_t_subr typedef David Fang
@ 2011-03-01 20:33 ` Ludovic Courtès
0 siblings, 0 replies; 2+ messages in thread
From: Ludovic Courtès @ 2011-03-01 20:33 UTC (permalink / raw)
To: David Fang; +Cc: guile-devel
Hi David,
David Fang <fang@csl.cornell.edu> writes:
> /* The type of subrs, i.e., Scheme procedures implemented in C. Empty
> function declarators are used internally for pointers to functions of
> any arity. However, these are equivalent to `(void)' in C++, are
> obsolescent as of C99, and trigger `strict-prototypes' GCC warnings
> (bug #23681). */
(See <http://savannah.gnu.org/bugs/?23681>.)
> #ifdef BUILDING_LIBGUILE
> typedef SCM (* scm_t_subr) ();
> #else
> typedef void *scm_t_subr;
> #endif
[...]
> and after installation, BUILDING_LIBGUILE is no longer defined. Had
> the typedef been kept as SCM (*scm_t_subr)(), then the C++ compiler
> would have no reason to complain
As explained above, it would complain because in C++ an empty declarator
means that the function takes zero arguments, whereas in C it means that
the function prototype is undefined—i.e., the function can take any
number of arguments of any type.
If you think the comment can be improved, please let me know.
> cc1plus: warnings being treated as errors
> ../../../src/guile/hackt-config.cc: In function 'void
> HAC::guile_wrap::wrap_package_string_init()':
> ../../../src/guile/hackt-config.cc:32: warning: ISO C++ forbids
> casting between pointer-to-function and pointer-to-object
In C++, instead of writing, say:
--8<---------------cut here---------------start------------->8---
extern SCM bar (SCM x, SCM y);
...
scm_c_define_gsubr ("foo", 1, 2, 3, &bar);
--8<---------------cut here---------------end--------------->8---
you must write:
--8<---------------cut here---------------start------------->8---
extern SCM bar (SCM x, SCM y);
...
scm_c_define_gsubr ("foo", 1, 2, 3, (scm_t_subr) &bar);
--8<---------------cut here---------------end--------------->8---
(Tested with G++ 4.5.1 -Wall.)
If you use ‘guile-snarf’, it should do the right thing.
Hope this helps,
Ludo’.
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2011-03-01 20:33 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-03-01 1:50 guile-2.0 scm_t_subr typedef David Fang
2011-03-01 20:33 ` Ludovic Courtès
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).