develooper Front page | perl.perl5.porters | Postings from July 2003

Re: Which platforms need symbols to export to be explicitly listed?

From:
Randy Kobes
Date:
July 30, 2003 14:04
Subject:
Re: Which platforms need symbols to export to be explicitly listed?
Message ID:
Pine.LNX.4.56.0307301319580.16131@theoryx5.uwinnipeg.ca
On Wed, 30 Jul 2003, Steve Hay wrote:

> Randy Kobes wrote:
>
> >On Wed, 30 Jul 2003, Steve Hay wrote:
> >
> >>I'll just enclose the XS code that
> >>uses PerlIO_isutf8() inside
> >>
> >>#if PERL_VERSION >= 8 && defined(PerlIO_isutf8)
> >>...
> >>#endif
> >>
> >I don't have access to my Win32 machine with a compiler at the
> >moment to test this, but using "defined(PerlIO_isutf8)" in this
> >way may depend on the order the Perl header files are included,
> >as well as on the perl version. I checked for a 5.6.1, and
> >PerlIO_isutf8 isn't in the Perl headers, so this should be OK in
> >that case.  However, in the 5.8.0 C:\Perl\lib\CORE\perlio.h
> >there's
> >
> >#ifndef PerlIO_isutf8
> >extern int PerlIO_isutf8(PerlIO *);
> >#endif
> >
> >So, if this has been seen already, PerlIO_isutf8 would be
> >defined, but then a link error would result if PerlIO_isutf8
> >wasn't in the library.
> >
> >
> OK, now I'm really confused.
>
> What you say makes sense, albeit with some confusion over
> declaration/definition:  the "extern int PerlIO_isutf8(PerlIO *)" line
> above is a declaration of the function, not a definition of it, so what
> should "#if defined(PerlIO_isutf8)" do?
>
> I tried out this test program:
>
> #include <stdio.h>
> extern void foo(void);
> void main(void) {
> #if defined(foo)
>     foo();
> #else
>     printf("foo is NOT defined\n");
> #endif
> }
>
> This compiles & links OK, even though the promised "external" definition
> of foo() is not supplied.  The reason is, of course, that the "#if
> defined(foo)" test returns false.
>
> This made me think that what I had proposed would be OK after all.
> However, I then tried this:
>
> #include <stdio.h>
> void foo(void) {
>     printf("foo is defined\n");
> }
> void main(void) {
> #if defined(foo)
>     foo();
> #else
>     printf("foo is NOT defined\n");
> #endif
> }
>
> This, of course, also compiles & links OK, but, to my surprise, prints
> "foo is NOT defined"!  Am I missing something here?  Why does "#if
> defined(foo)" return false immediately after foo() has been defined?

I'm not sure how Perl sets up PerlIO_isutf8() as to
tell what defined(PerlIO_isutf8) would mean, but I think
your examples illustrate that a function declaration of
"foo" doesn't automatically define "foo", in the context of
#if defined(foo). So, for example,
====================================================
#include <stdio.h>
#ifdef USE_EXTERN
#define foo foo
#endif
void foo(void) {
    printf("foo is defined\n");
}
int main(void) {
#if defined(foo)
    foo();
#else
    printf("foo is NOT defined\n");
#endif
}
====================================================
will only print out "foo is defined" if you compile it
with -DUSE_EXTERN. You could also set it up in a macro:
====================================================
#include <stdio.h>
#ifdef USE_MACRO
#define foo (printf("foo is defined\n"))
#endif
int main(void) {
#if defined(foo)
    foo();
#else
    printf("foo is NOT defined\n");
#endif
}
=======================================================
which needs -DUSE_MACRO to print out that foo is defined.

-- 
best regards,
randy



nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About