Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Inline::C with multiple *.c

by gri6507 (Deacon)
on Aug 24, 2012 at 00:08 UTC ( [id://989413]=perlquestion: print w/replies, xml ) Need Help??

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

Fellow Monks,

There has to be a better way to specify multiple C source files for use with Inline::C module. In my case, the C source files, header files, and the library are in the same directory as the Perl script. Here's what I am currently forced to do:

my $relSourcePath; my $inlineCFile1; my $inlineCFile2; my $inlineBuildDir; my $libDir; my $incDir; BEGIN { # this file is in the same directory as the rest of the sources $relSourcePath = File::Spec->rel2abs(dirname($0)); die "\nInstall path must not have spaces.\n\n" if $relSourcePath = +~ /\s/; # Need to build the path to the Inline C file here, so that we cou +ld # reference it below my @dirs = (); push @dirs, $relSourcePath; $inlineCFile1 = File::Spec->catfile(@dirs, "file_a.c"); $inlineCFile2 = File::Spec->catfile(@dirs, "file_b.c"); push @dirs, ".Inline"; $inlineBuildDir = File::Spec->catdir(@dirs); pop @dirs; $libDir = File::Spec->catdir(@dirs); $incDir = File::Spec->catdir(@dirs); } use Inline ( C => Config => LIBS => "-L$libDir -lldv32", INC => "-I$incDir", DIRECTORY => $inlineBuildDir, ); use Inline C => "$inlineCFile1"; use Inline C => "$inlineCFile2";
Is there a better way?

Replies are listed 'Best First'.
Re: Inline::C with multiple *.c
by syphilis (Archbishop) on Aug 24, 2012 at 01:12 UTC
    No problem for me with relative paths to 2 C files in the same directory:
    C:\>type a.c void foo() { printf("Hello from foo\n"); } C:\>type b.c void bar() { printf("Hello from bar\n"); } C:\>type try.pl use warnings; use Inline C => "./a.c"; use Inline C => "./b.c"; foo(); bar(); C:\>perl try.pl Hello from foo Hello from bar C:\>
    It may well be that relative paths need to begin with "./".

    For LIBS and INC I think you do need to specify an absolute path which, for the cwd, should just be File::Spec->rel2abs('.')
    And, yes, that would need to be done in a BEGIN block.

    To me, that doesn't seem so bad - I guess it could always be improved.

    Cheers,
    Rob
Re: Inline::C with multiple *.c
by Anonymous Monk on Aug 24, 2012 at 08:13 UTC

    Is there a better way?

    Yes, chdir

    #!/usr/bin/perl -- use Path::Class; use constant THISFILE => "".file( __FILE__ )->absolute; use constant THISDIR => "".file( THISFILE )->dir; BEGIN { chdir THISDIR or die $!; } use Inline ( C => Config => LIBS => "-Llib/relative -lrelative", INC => "-Iinc/relative", BUILD_NOISY => 1, CLEAN_AFTER_BUILD => 0, DIRECTORY => 'RELATIVE', # INVALID VALUE?! IT MUST EXIST ); use Inline C => file('a.c')->absolute, qw' NAME RelativeA '; use Inline C => file('b.c')->absolute, qw' NAME RelativeB '; use strict; use warnings; ...

    See also swd, Toolkit, Syntax::Collector...

      Thanks, your syntax does seem much cleaner and more intuitive.

      However, I just stumbled across another odd problem. I just changed my b.c so that it calls a function from a.c. Since a.c is built first, it generates a DLL (I'm doing this on Windows under Strawberry Perl). Next, when building b.c, it has to be linked against the first DLL. But I can't specify that in the config section above because it's common to all Inline::C files and clearly that DLL does not yet exist when compiling/linking a.c.

      Any suggestions?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://989413]
Front-paged by bulk88
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (6)
As of 2024-04-24 09:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found