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

Hi gurus,
I intend to dive into mysterious XS field. But my first try failed.;( Below is head and C files.
test.h #define TESTVAL 3 extern int max(int, int); test.c #include "./test.h" int max(int a, int b) { return ((a>b)? a:b); }
I ran 4 steps as perldoc h2xs: All things look fine, but when I run dmake, it throws error:
C:\Mytest>perl Makefile.PL Checking if your kit is complete... Looks good Writing Makefile for Mytest C:\Mytest>dmake cp lib/Mytest.pm blib\lib\Mytest.pm AutoSplitting blib\lib\Mytest.pm (blib\lib\auto\Mytest) C:\strawberry\perl\bin\perl.exe C:\strawberry\perl\lib\ExtUtils\xsubpp + -typemap C:\strawberry\perl\lib\ExtUtils\typemap Mytest.xs > Mytest.xsc && C: +\strawberr y\perl\bin\perl.exe -MExtUtils::Command -e "mv" -- Mytest.xsc Mytest.c Please specify prototyping behavior for Mytest.xs (see perlxs manual) gcc -c -I. -s -O2 -DWIN32 -DHAVE_DES_FCRYPT -DUSE_SITECUSTOMIZE - +DPERL_IMPL ICIT_CONTEXT -DPERL_IMPLICIT_SYS -fno-strict-aliasing -DPERL_MSVCRT_RE +ADFIX -s - O2 -DVERSION=\"0.01\" -DXS_VERSION=\"0.01\" "-IC:\strawberr +y\perl\lib \CORE" Mytest.c In file included from Mytest.xs:7: ./test.h:1: error: syntax error before "int" dmake: Error code 129, while making 'Mytest.o'
Any thoughts? I believe monks' help will let me pass Dark Ages as quickly as possible. ;) TIA




I am trying to improve my English skills, if you see a mistake please feel free to reply or /msg me a correction

Replies are listed 'Best First'.
Re: How convert header file to XS by h2xs?
by syphilis (Archbishop) on Mar 19, 2010 at 10:10 UTC
    In the Mytest directory can you successfully run:
    gcc -c -I. -s -O2 -DWIN32 -DHAVE_DES_FCRYPT -DUSE_SITECUSTOMIZE - +DPERL_IMPLICIT_CONTEXT -DPERL_IMPLICIT_SYS -fno-strict-aliasing -DPER +L_MSVCRT_READFIX -s -O2 -DVERSION=\"0.01\" -DXS_VERSION=\"0 +.01\" "-IC:\strawberry\perl\lib\CORE" test.c
    It runs fine for me (based on the test.h and test.c that you provided). If it doesn't work for you, then I suggest that the actual 'test.h' you're using is different to the one you provided in your original post. If, however, it does run fine for you, then try running:
    gcc -c -I. -s -O2 -DWIN32 -DHAVE_DES_FCRYPT -DUSE_SITECUSTOMIZE - +DPERL_IMPLICIT_CONTEXT -DPERL_IMPLICIT_SYS -fno-strict-aliasing -DPER +L_MSVCRT_READFIX -s -O2 -DVERSION=\"0.01\" -DXS_VERSION=\"0 +.01\" "-IC:\strawberry\perl\lib\CORE" Mytest.c
    That's the same as the first command I provided - except that 'test.c' has been replaced by 'Mytest.c'. That's also now the same command that failed for you in your original post - so it should fail again this time in exactly the same way. Does it ?

    The point I'm trying to make is that something here just doesn't add up :-)

    Cheers,
    Rob
      then I suggest that the actual 'test.h' you're using is different to the one you provided

      That's also my interpretation, in particular as the 'syntax error before "int"' in test.h is being reported for line 1... (any error before any of the ints in that file should be on line 2)

      In case compiling test.c (the first command you suggested) should fail, the OP might want to replace option -c in that commandline with -E in order to take a look at the preprocessed output.

      The point I'm trying to make is that something here just doesn't add up :-) directly! but I checked Mytest.xs:
      #include "EXTERN.h" #include "perl.h" #include "XSUB.h" #include "ppport.h" #include "test.h" #include "const-c.inc" MODULE = Mytest PACKAGE = Mytest INCLUDE: const-xs.inc
      I can't find something strange. So, how can I do next? check Mytest.c or fallback directory?
        So, how can I do next?

        As the compiler obviously thinks the error has to do with test.h, it would seem to make sense to focus on that issue.  Could you try what Rob and I suggested above, i.e. simply try to compile test.c alone (which includes test.h) — possibly with option -E to get an idea of what code the compiler is actually trying to compile.

Re: How convert header file to XS by h2xs?
by Anonymous Monk on Mar 19, 2010 at 07:26 UTC
    I repeated your steps I think you should update your toolchain and your test files (which don't seem to match).
    $ pmvers Devel::PPPort ExtUtils::Constant ExtUtils::MakeMaker ExtUtils +::ParseXS C::Scan Devel::PPPort: 3.19 ExtUtils::Constant: 0.22 ExtUtils::MakeMaker: 6.56 ExtUtils::ParseXS: 2.2205 C::Scan: 0.74
    h2xs.PL
      I upgrade all modules you list, but nothing change.I guess whether C file or header file has some problem so that ./test.h:1: error: syntax error before "int" happened.




      I am trying to improve my English skills, if you see a mistake please feel free to reply or /msg me a correction

        Examine Mytest.xs and Mytest.c
Re: How convert header file to XS by h2xs?
by Anonymous Monk on Mar 19, 2010 at 08:36 UTC
    I login in ubuntu 9.04 and rerun the steps, Unfortunately, all things are perfect. Does it mean my tool chain(dmake,gcc etc) has problems?
    Any reply are really appreciated!