Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

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

by syphilis (Archbishop)
on Apr 17, 2024 at 12:39 UTC ( [id://11158910]=note: print w/replies, xml ) Need Help??


in reply to Re^4: External (extra) files when using Inline::CPP
in thread External (extra) files when using Inline::CPP

Maybe it's a capability within Inline::Module?

Quite likely, assuming that it's using Inline solely for the generation of the XS file, and not the generation of the Makefile.PL.
For generation of XS files I use InlineX::C2XS/InlineX::CPP2XS - both of which also have the capability of generating a Makefile.PL with the OBJECT=>'$(O_FILES)' argument to WriteMakefile()).

The problem with Inline::C and Inline::CPP is that, for the OBJECT=>'$(O_FILES)' approach to work, the .c or .cpp file(s) (xkcd.cpp, in this case) needs to be in the build directory.
However, one doesn't know in advance the location of the directory in which the build will actually take place.

There's only a couple of simple hacks to C.pm and CPP.pm needed to enable the passing of the the OBJECT=>'$(O_FILES)' argument to the Makefile.PL that Inline generates in the build directory.
And I've found that those rough hacks work fine so long as xkcd.cpp and xkcd.h have been placed in the build directory before test.pl is executed.

The rough hack I used:
In Inline/C.pm locate and replace
croak "'$key' is not a valid config option for $class\n";
with:
croak "'$key' is not a valid config option for $class\n" unless $key eq 'OBJECT';
In Inline/CPP.pm apply this patch:
--- CPP.pm_orig 2019-04-20 00:50:28.000000000 +1000 +++ CPP.pm 2024-04-17 21:16:22.142182800 +1000 @@ -114,6 +114,9 @@ while (@config_options) { my ($key, $value) = (shift @config_options, shift @config_options +); $key = uc $key; + if ($key eq 'OBJECT') { + $o->{ILSM}{MAKEFILE}{OBJECT} = $value; + } if ($key eq 'NAMESPACE') { _handle_namespace_cfg_option($o, $value); }
I slightly alter test.pl to be:
use Inline CPP => Config => BUILD_NOISY => 1 CLEAN_AFTER_BUILD => 0, OBJECT => '$(O_FILES)', ; use Inline CPP; for(1..3) { print "Perfect random number: ", cast_die(), "\n"; } __END__ __CPP__ #include "xkcd.h" int cast_die() { int rolled=xkcd_dice_roll(); return rolled; }
Then I run perl test.pl, which fails because of an undefined reference to xkcd_dice_roll()
Then remove all files from the build directory (./_Inline/build/test_pl_b6be).
Then copy xkcd.h and xkcd.cpp to the build directory (./_Inline/build/test_pl_b6be).
Then re-run perl test.pl - which succeeds this time.
On inspecting the build directory (./_Inline/build/test_pl_b6be), I can see that xkcd.cpp has been compiled into xkcd.o, and there is no library.
I don't recommend doing it this way - it's more just a proof of concept that this approach could be made to work.

Cheers,
Rob

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2024-06-18 05:20 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.