kopolov has asked for the wisdom of the Perl Monks concerning the following question:

Hi there, I have this function in C:
TM_PageTranslation* findPageByEffective(u32 lpid, u32 pid, u64 EA);
These are the types of each parameter: TM_PageTranslation - C struct u32 - unsigned int u64 - unsigned long long int This is my typemap:
TYPEMAP T_PageTranslation T_PTROBJ u64 T_U_LONG_LONG u64 * T_OPAQUEPTR u32 * T_OPAQUEPTR u32 T_U_INT ################################################## INPUT T_U_LONG_LONG $var = (unsigned long long int)SvUV($arg) ################################################## OUTPUT T_U_LONG_LONG sv_setuv($arg, (UV)$var);
(T_U_INT is defined in the default typemap) this is my XS code:
typedef struct TM_PageTranslation * T_PageTranslation; ... T_PageTranslation findPageByEffective(lpid, pid, EA) u32 lpid u32 pid u64 EA
When I run the make command I get the following warning: "trans.c:470: warning: assignment from incompatible pointer type" line 470 int rans.C is this:
XS(XS_trans_findPageByEffective); /* prototype to pass -Wmissing-proto +types */ XS(XS_trans_findPageByEffective) { #ifdef dVAR dVAR; dXSARGS; #else dXSARGS; #endif if (items != 3) croak_xs_usage(cv, "lpid, pid, EA"); { u32 lpid = (unsigned int)SvUV(ST(0)); u32 pid = (unsigned int)SvUV(ST(1)); u64 EA = (unsigned long long int)SvUV(ST(2)); T_PageTranslation RETVAL; RETVAL = findPageByEffective(lpid, pid, EA); <--- This is line 470 ST(0) = sv_newmortal(); sv_setref_pv(ST(0), "T_PageTranslation", (void*)RETVAL); } XSRETURN(1); }
How do I fixed this?

Replies are listed 'Best First'.
Re: perlXS assignment from incompatible pointer type
by Anonymous Monk on Mar 22, 2016 at 23:40 UTC
    And what about the definition of struct TM_PageTranslation? For all we know there is no such struct... Maybe it's something like
    typedef struct { ... } TM_PageTranslation;
    If so, remove struct from typedef struct TM_PageTranslation * T_PageTranslation. That's the most likely cause of error, IMO.

    If that doesn't work, try to post SSCCE. See sscce.org for details.

      I've taken a look at your previous posts and indeed... So, OP, you'll have to learn about a pretty dumb feature of C (that has nothing to do with XS per se). There is a difference between
      typedef struct Foo { ... } Foo;
      and
      typedef struct { ... } Foo;
      In the first case, you can use struct Foo and Foo interchangeably. I think... Compatibility rules for types in C are pretty complex and obscure. Anyway, in the second case you don't have struct Foo, you have just Foo which is a name for an anonymous struct (while Foo in struct Foo is not a name but a tag). Yes, it's pretty confusing... but don't worry: there are some things in C that are a lot more obscure and error prone than that :) I suggest you to avoid typedefs for the time being.

        Regarding your first example: there's no compatibility problem. "A typedef declaration does not introduce a new type, only a synonym for the type so specified." If you didn't add qualifications, they are one and the same type (an alias).

      You're a genius :) Thank you !!! Removed "struct" (and thanks for the lesson in structs)