Note that there are some explanatory texts on larger screens.

plurals
  1. POPerl: how can I put all my inline C code into a separate file?
    primarykey
    data
    text
    <p>This problem is so simple I can feel the RTFM's coming. However, I've been looking at the docs (<a href="http://search.cpan.org/perldoc?Inline" rel="nofollow noreferrer">Inline</a>, <a href="http://search.cpan.org/perldoc?Inline::C" rel="nofollow noreferrer">Inline-C</a>, <a href="http://search.cpan.org/perldoc?Inline::C-Cookbook" rel="nofollow noreferrer">Inline-C-Cookbook</a> ) all morning and I can't figure out how to solve this problem.</p> <p>I want to use inline C, but I don't want to have C code in the same file as my perl code. </p> <p>(Emacs doesn't like having two languages in one file. In principle this is a matter of convenience, but in practice I'm having to edit my C in one file then copy-paste it into my perl script.)</p> <p>Here is working perl:</p> <pre><code>#!/usr/bin/perl use Inline C =&gt; DATA; use strict; use warnings; use List::Util qw(sum); use feature qw(say); my @array = (1..10); say "native perl: ", sum(@array), ", Inline C: ", sum1(\@array); __END__ __C__ double sum1(AV* array) { int i; double sum = 0.0; for (i=0; i&lt;=av_len(array); i++) { SV** elem = av_fetch(array, i, 0); if (elem != NULL) sum += SvNV(*elem); } return sum; } </code></pre> <p>(thanks to <a href="https://stackoverflow.com/questions/1503763/how-can-i-pass-an-array-to-a-c-function-in-perl-xs">mobrule</a> for getting me this far.)</p> <p>I want to move all of the C code (or as much as possible) into a separate header file. </p> <p>What I can do is put <code>sum1</code> into a header, and do this:</p> <pre><code># same perl as above except now say sum2 instead of sum1 __END__ __C__ #include "sum.h" double sum2(AV* array) { sum1(array); } </code></pre> <p>This is good enough as I no longer have to edit the C in perl-mode, but I wonder if there isn't a more elegant solution to this problem?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload