Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Inline::C Cookbook addition

5 views
Skip to first unread message

Ron Grunwald via inline

unread,
Dec 7, 2016, 7:00:04 PM12/7/16
to inl...@perl.org
Hello all,

Following the recent discussion about accessing Perl variables directly from a C function, I've put together some sample code that I think would be appropriate to add into the Inline::C Cookbook. I thought to present the example here first to allow users to comment or provide suggestions.

The code was tested in the following environments,
- Linux Fedora 20.0, ActivePerl 5.14.4, Inline::C 0.76
- OS X Yosemite 10.10.5, ActivePerl 5.24.0, Inline::C 0.76

The code example:

use strict;
use warnings;
use Inline C => Config => ccflagsex => "-std=c99";
use Inline C => "DATA";

our $mesh_data = "MESH-POINTS 0.0 0.0 0.5 0.25 1.0 0.5 1.5 0.75";
CalcSurfaceHeights();

__DATA__
__C__
#define N_MP 4

void CalcSurfaceHeights() {
   double x[N_MP], y[N_MP], z;
   char   *mesh_data = SvPV_nolen(get_sv("main::mesh_data", 0));

   sscanf(mesh_data, "MESH-POINTS %lf%lf%lf%lf%lf%lf%lf%lf",
                     x, y, x+1, y+1, x+2, y+2, x+3, y+3);

   for (int ix=0; ix < N_MP; ix++) {
      z = 0.5*( sin(x[ix]) + sin(y[ix]) );

      printf("Surface-Height: %6.3f Mesh-Point: %6.2f, %6.2f\n",
             z, x[ix], y[ix]);
   }
}

________________________________________



sisy...@optusnet.com.au

unread,
Dec 8, 2016, 2:15:02 AM12/8/16
to Ron Grunwald, inl...@perl.org

From: Ron Grunwald via inline
Sent: Thursday, December 08, 2016 1:44 AM
To: inl...@perl.org
Subject: Inline::C Cookbook addition
Hello all,

> The code example:
>
> use strict;
> use warnings;
> use Inline C => Config => ccflagsex => "-std=c99";
> use Inline C => "DATA";
>
> our $mesh_data = "MESH-POINTS 0.0 0.0 0.5 0.25 1.0 0.5 1.5 0.75";
> CalcSurfaceHeights();
>
> __DATA__
> __C__
> #define N_MP 4
>
> void CalcSurfaceHeights() {
> double x[N_MP], y[N_MP], z;
> char *mesh_data = SvPV_nolen(get_sv("main::mesh_data", 0));
>
> sscanf(mesh_data, "MESH-POINTS %lf%lf%lf%lf%lf%lf%lf%lf",
> x, y, x+1, y+1, x+2, y+2, x+3, y+3);
>
> for (int ix=0; ix < N_MP; ix++) {
> z = 0.5*( sin(x[ix]) + sin(y[ix]) );
>
> printf("Surface-Height: %6.3f Mesh-Point: %6.2f, %6.2f\n",
> z, x[ix], y[ix]);
> }
> }

The "use Inline C => Config => ..." line can be removed if you rewrite the
__C__ section as:

__DATA__
__C__
#define N_MP 4

void CalcSurfaceHeights() {
double x[N_MP], y[N_MP], z;
int ix;
char *mesh_data = SvPV_nolen(get_sv("main::mesh_data", 0));

sscanf(mesh_data, "MESH-POINTS %lf%lf%lf%lf%lf%lf%lf%lf",
x, y, x+1, y+1, x+2, y+2, x+3, y+3);

for (ix=0; ix < N_MP; ix++) {
z = 0.5*( sin(x[ix]) + sin(y[ix]) );

printf("Surface-Height: %6.3f Mesh-Point: %6.2f, %6.2f\n",
z, x[ix], y[ix]);
}
}

All I've done is move the declaration of "ix" from the for() loop to the
declaration section at the beginning of CalcSurfaceHeights().
This then enables the code to compile using compilers (such as Microsoft
compilers) that are not C99-compliant.

This mailing list is very quiet these days and I think your request will
probably fall "through the cracks" unless you submit it as a github "Issue"
or a github pull request.

Cheers,
Rob

Ron Grunwald via inline

unread,
Dec 8, 2016, 5:45:02 PM12/8/16
to sisy...@optusnet.com.au, inl...@perl.org
Hi Rob,

So I gather that the example in its original state did not compile on Windows? I would be somewhat surprised that there are still C compilers in use that are not C99 compliant.

Many thanks for your suggestions, and I will follow your advice on using the github process.

Cheers,
Ron.

sisy...@optusnet.com.au

unread,
Dec 8, 2016, 6:30:02 PM12/8/16
to Ron Grunwald, inl...@perl.org
Hi Ron,

The original rendition compiles on Windows ok if the compiler being used is
gcc. (I think you said you had tested it on ActivePerl 5.24.0 and found it
to be fine.)

However, with my MS compiler (Microsoft (R) C/C++ Optimizing Compiler
Version 14.00.40310.41 for AMD64), I got:

########################################
C:\_32\pscrpt\inline>perl try.pl

Microsoft (R) Program Maintenance Utility Version 7.00.8882
Copyright (C) Microsoft Corp 1988-2000. All rights reserved.

Running Mkbootstrap for try_pl_7dcf ()
"C:\_64\ap1600\bin\perl.exe" -MExtUtils::Command -e chmod -- 644
"try_pl
_7dcf.bs"
"C:\_64\ap1600\bin\perl.exe"
C:\_64\ap1600\lib\ExtUtils\xsubpp" -typem
ap "C:\_64\ap1600\lib\ExtUtils\typemap" -typemap
"C:\_32\pscrpt\inline\typemap"
try_pl_7dcf.xs > try_pl_7dcf.xsc &&
"C:\_64\ap1600\bin\perl.exe" -MExtUtils::Co
mmand -e mv -- try_pl_7dcf.xsc try_pl_7dcf.c
cl -c -I"C:/_32/pscrpt/inline" -nologo -GF -W3 -MD -Zi -DNDEBUG -Ox
-G
L -fp:precise -DWIN32 -D_CONSOLE -DNO_STRICT -DWIN64 -DCONSERVATIVE -DPERL_TEXTM
ODE_SCRIPTS -DUSE_SITECUSTOMIZE -DPERL_IMPLICIT_CONTEXT -DPERL_IMPLICIT_SYS
-DUS
E_PERLIO -std=c99 -MD -Zi -DNDEBUG -Ox -GL -fp:precise -DVERSION=\"0.00\"
-D
XS_VERSION=\"0.00\" "-IC:\_64\ap1600\lib\CORE" try_pl_7dcf.c
cl : Command line warning D9002 : ignoring unknown option '-std=c99'
try_pl_7dcf.c
try_pl_7dcf.xs(14) : error C2143: syntax error : missing ';' before 'type'
try_pl_7dcf.xs(14) : error C2143: syntax error : missing ';' before 'type'
try_pl_7dcf.xs(14) : error C2143: syntax error : missing ')' before 'type'
try_pl_7dcf.xs(14) : error C2143: syntax error : missing ';' before 'type'
try_pl_7dcf.xs(14) : error C2065: 'ix' : undeclared identifier
try_pl_7dcf.xs(14) : warning C4552: '<' : operator has no effect; expected
operator with side-effect
try_pl_7dcf.xs(14) : error C2059: syntax error : ')'
try_pl_7dcf.xs(14) : error C2143: syntax error : missing ';' before '{'
NMAKE : fatal error U1077: 'cl' : return code '0x2'
Stop.

A problem was encountered while attempting to compile and install your
Inline
C code. The command that failed was:
C:\_64\PLATFO~1\Bin\nmake.exe > out.make 2>&1

The build directory was:
C:\_32\pscrpt\inline\_Inline\build\try_pl_7dcf

To debug the problem, cd to the build directory, and inspect the output
files.

at try.pl line 0.
...propagated at C:/_64/ap1600/site/lib/Inline/C.pm line 772.
INIT failed--call queue aborted.

########################################

Admittedly, that MS compiler is getting a little bit old but my
understanding is that even the most recent MS compilers are not C99
compliant (as MS like to make up their own rules).
FAIK there might also be other non C99 compliant compilers in use.

Both ActiveState and StrawberryPerl provide gcc-based perls for Windows, but
there are still people using MS compilers with perl on Windows. (I'm not
really one of those people - I keep that MS compiler for reference only.)

So I think, for a cookbook especially, it's best to code for the lowest
common denominator unless that's impractical - though I'm sure that use of
non C99 compliant compilers with perl is the exception rather than the rule.

Cheers,
Rob

-----Original Message-----
From: Ron Grunwald via inline

Ron Grunwald via inline

unread,
Dec 9, 2016, 6:15:02 PM12/9/16
to sisy...@optusnet.com.au, inl...@perl.org
Hi Rob,

Thank you for testing the example with your compiler, and also thank you for the wonderful advice. I will remove the index variable declaration from the for loop.

for (int ix=0; ix < N_MP; ix++) {

Cheers,
Ron.

On 9 Dec 2016, at 7:24 am, <sisy...@optusnet.com.au> <sisy...@optusnet.com.au> wrote:

Hi Ron,

The original rendition compiles on Windows ok if the compiler being used is gcc. (I think you said you had tested it on ActivePerl 5.24.0 and found it to be fine.)

However, with my MS compiler (Microsoft (R) C/C++ Optimizing Compiler Version 14.00.40310.41 for AMD64), I got:

Microsoft (R) Program Maintenance Utility   Version 7.00.8882
Copyright (C) Microsoft Corp 1988-2000. All rights reserved.

      cl -c  -I"C:/_32/pscrpt/inline"  -nologo -GF -W3 -MD -Zi -DNDEBUG -Ox -G
L -fp:precise -DWIN32 -D_CONSOLE -DNO_STRICT -DWIN64 -DCONSERVATIVE -DPERL_TEXTM
ODE_SCRIPTS -DUSE_SITECUSTOMIZE -DPERL_IMPLICIT_CONTEXT -DPERL_IMPLICIT_SYS  -DUS
E_PERLIO -std=c99 -MD -Zi -DNDEBUG -Ox -GL -fp:precise    -DVERSION=\"0.00\"  -D
XS_VERSION=\"0.00\"  "-IC:\_64\ap1600\lib\CORE"   try_pl_7dcf.c
cl : Command line warning D9002 : ignoring unknown option '-std=c99'

Ron Grunwald via inline

unread,
Dec 20, 2016, 5:45:02 PM12/20/16
to sisy...@optusnet.com.au, inl...@perl.org
Hi all,

This mailing list is very quiet these days and I think your request will probably fall "through the cracks" unless you submit it as a github "Issue" or a github pull request.

For completeness, I did write up the recipe and submitted it as an issue on github about 3 days ago,


There havn't been any comments made so far, so I'm not sure if its being considered for the C Cookbook.
Also, it looks like no updates were made to Inline::C for the last two years.

Cheers,
Ron.

On 8 Dec 2016, at 3:08 pm, sisy...@optusnet.com.au wrote:

sisy...@optusnet.com.au

unread,
Dec 20, 2016, 6:30:02 PM12/20/16
to Ron Grunwald, inl...@perl.org
From: Ron Grunwald
Sent: Wednesday, December 21, 2016 9:33 AM
To: sisy...@optusnet.com.au
Cc: inl...@perl.org
Subject: Re: Inline::C Cookbook addition

>> This mailing list is very quiet these days and I think your request will
>> probably fall "through the cracks" unless you submit it as a github
>> "Issue" or a github pull request.
>
> For completeness, I did write up the recipe and submitted it as an issue
> on github about 3 days ago,
>
> https://github.com/ingydotnet/inline-c-pm/issues/56
>
> There havn't been any comments made so far, so I'm not sure if its being
> considered for the C Cookbook.
> Also, it looks like no updates were made to Inline::C for the last two
> years.
>
> Cheers,
> Ron

Hi Ron,

Yes, I don't know when your request will be acted upon.

But it's now in a place where it won't get lost - and whenever someone
decides to update Inline::C they should see that request and give it the
consideration it deserves.

Cheers,
Rob

Ron Grunwald via inline

unread,
Dec 22, 2016, 9:45:03 AM12/22/16
to sisy...@optusnet.com.au, inl...@perl.org
Hi Rob,

You've made a very good point - thank you.

I am actually quite interested to update the FORTRAN recipe of the Cookbook also. The FORTRAN 2003 standard introduced interoperability with C features. This should greatly simplify invoking modern FORTRAN program units from Inline::C.

The example I have in mind is writing a FORTRAN MODULE that performs matrix multiplication using the MATMUL intrinsic function. The minimum FORTRAN compiler that would be used is G95, which does offer C interoperability.

Anyway, I'll think about this a bit more.

Cheers,
Ron.

Ron Grunwald via inline

unread,
Feb 5, 2017, 9:45:13 AM2/5/17
to inl...@perl.org
Hi all,

Just providing an update to this thread. I did end up submitting an updated "Cookbook.swim" as a pull request with the help of several contributors. A few days ago, Ingy was kind enough to merge the change with the Inline::C master branch.

The new recipe is titled "Direct Access to Perl variables" and is placed at the end of the section "Meat & Potatoes".

Thank you to everyone who assisted me.

Cheers,
Ron.
0 new messages