Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: External (extra) files when using Inline::CPP

by syphilis (Archbishop)
on Apr 16, 2024 at 11:13 UTC ( [id://11158889]=note: print w/replies, xml ) Need Help??


in reply to External (extra) files when using Inline::CPP

Here's one way that works for me:
Firstly compile the cpp file into xkcd.o:
g++ -c xkcd.cpp
Then create a static library (named, say, libxkcd.a) from the generated xkcd.o:
ar -rc libxkcd.a xkcd.o
Then before the "use Inline CPP;" line in test.pl I inserted:
use Inline CPP => Config => BUILD_NOISY => 1, INC => '-ID:/w/xkcd', LIBS => '-LD:/w/xkcd -lxkcd', ;
The "BUILD_NOISY" directive just allows you to see the compilation during the initial run.
The 'INC' and 'LIBS' directives allow xkcd.h and libxkcd.a to be located, as needed during the build.
Obviously, for me both of those files were located in the D:/w/xkcd directory. For me, the script then outputs:
Perfect random number: 4 Perfect random number: 4 Perfect random number: 4
I think it might be important that there's an empty line or 2 at the bottom of all three test.pl, xkcd.h and xkcd.cpp files.

Cheers,
Rob

Replies are listed 'Best First'.
Re^2: External (extra) files when using Inline::CPP
by cavac (Parson) on Apr 16, 2024 at 12:01 UTC

    Perfect, thank you!

    I ended up with doing some extra magic in BEGIN, to automate those compile steps (.cpp and .h files in same directory):

    #!/usr/bin/env perl use v5.38; use strict; use warnings; use Carp; use Cwd; use Array::Contains; my $dir; my $verbose; BEGIN { $dir = getcwd; print "Working directory: $dir\n"; $verbose = 0; if(contains('-v', \@ARGV)) { $verbose = 1; } # ---- Poor mans Makefile --- if(contains('--clean', \@ARGV)) { print "Cleaning up\n"; my $cmd = 'rm -rf _Inline xkcd.o libxkcd.a'; print "$cmd\n" if($verbose); `$cmd`; exit(0); } print "Compiling library...\n"; foreach my $cmd ('g++ -c xkcd.cpp', 'ar -rc libxkcd.a xkcd.o') { print "$cmd\n" if $verbose; `$cmd`; } # ---- Poor mans Makefile --- }; use Inline 'CPP'; use Inline CPP => Config => BUILD_NOISY => $verbose, INC => '-I' . $dir, LIBS => '-L' . $dir . ' -lxkcd', ; for(1..3) { print "XKCD Dice roll: ", cast_die(), "\n"; } __END__ __CPP__ #include "xkcd.h" int cast_die() { int rolled = xkcd_dice_roll(); return rolled; }

    Hmm, i also tested without the empty lines at the bottom of the files and it worked fine. But i dimly remember having the same (or similar) issue to yours on another project.

    PerlMonks XP is useless? Not anymore: XPD - Do more with your PerlMonks XP
      I ended up with doing some extra magic in BEGIN, to automate those compile steps

      AIUI, if you were to instead turn the project into a module (with Makefile.PL and XS file, etc.) you wouldn't need to create a library, nor would you even need to precompile the .cpp file.
      That can all be taken care of automatically by the Makefile.PL, courtesy of the OBJECT=>'$(O_FILES)' option being passed to WriteMakefile() .

      However, AFAIK, that capability has not been ported to the Inline::C and Inline::CPP build procedures.
      I'm not sure why that has not yet been attended to. Maybe it's difficult, or maybe it's just that no-one has got around to doing it.

      Cheers,
      Rob

        If i release something, then yes, i will use proper modules with a Makefile.PL. But i really wanted an easy way to run local tests and one-off tools that doesn't require going through the steps.

        Inline is so much nicer for that can of stuff than using h2xs, because it does all the that for you on the fly, making it easier to experiment with interfaces and stuff.

        PerlMonks XP is useless? Not anymore: XPD - Do more with your PerlMonks XP
Re^2: External (extra) files when using Inline::CPP
by marioroy (Prior) on Apr 16, 2024 at 21:03 UTC

    The MCE Sandbox project places the C and header files inside the src folder. There is just the one *.c file, everything else placed in *.h files.

    .Inline/ Where Inline::C is configured to cache C object file +s. bin/ algorithm3.pl Practical sieve based on Algorithm3 from Xuedong Luo + [1]. src/ algorithm3.c Inline::C code for algorithm3.pl. bits.h Utility functions for byte array. output.h Fast printing of primes to a file descriptor. sandbox.h Header file, includes bits.h, output.h, sprintull.h. sprintull.h Fast base10 to string conversion. typemap Type-map file for Inline::C.

    I pass -I${base_dir}/src for Inline C, inside algorithm3.pl

    BEGIN { $ENV{PERL_INLINE_DIRECTORY} = "${base_dir}/.Inline"; mkdir "${base_dir}/.Inline" unless -d "${base_dir}/.Inline"; } use Inline 'C' => Config => CCFLAGSEX => "-I${base_dir}/src -O3 -fomit-frame-pointer", TYPEMAPS => "${base_dir}/src/typemap", clean_after_build => 0; use Inline 'C' => "${base_dir}/src/algorithm3.c";

    Later editing a header file requires removing the cache .Inline folder. Inline C does not know to re-compile due to not checking the *.h time stamps.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11158889]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2024-06-25 10:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.