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

Hi,

Every time I consult the perlembed documentation (which happens very rarely) I come across directions like:
cc -o interp interp.c `perl -MExtUtils::Embed -e ccopts -e ldopts`
and I wonder how that command can be modified to work in the windows cmd.exe shell.
My C compiler is gcc, so I try:
C:\_32>gcc -o interp interp.c `perl -MExtUtils::Embed -e ccopts -e ldo +pts` gcc: error: `perl: No such file or directory gcc: error: unrecognized command line option '-MExtUtils::Embed'
Is there a way to structure the command so that it works in the cmd.exe shell ?

My workaround has always been to redirect the ouput of perl -MExtUtils::Embed -e ccopts -e ldopts to a file, removing the newline between the ccopts and ldopts outputs, so that everything is on the one line - and then copy'n'paste that line into the cmd.exe command.
Works fine ... but there must be something better ?

Cheers,
Rob

Replies are listed 'Best First'.
Re: [OT] Windows cmd.exe shell
by holli (Abbot) on Aug 01, 2019 at 04:45 UTC
    This is not pretty.
    @ECHO OFF FOR /F "delims=" %%i IN ('perl -e "print qq(-v)"') DO set perl_argumen +t=%%i perl %perl_argument%
    But it works. See C:\> for /help for the gory details.
    D:\ENV>test.bat This is perl 5, version 26, subversion 1 (v5.26.1) built for MSWin32-x +64-multi-thread


    holli

    You can lead your users to water, but alas, you cannot drown them.
      See C:\> for /help for the gory details

      I had googled up some references to "for /f", but decided I didn't really want to go down the path of invoking a batch script. (My batch programming skills are very close to non-existent.)
      Besides, is it really possible to write a batch script that will do the job of running "gcc -o file.exe file.c args" where args is the concatenation of ccopts() and ldopts(), and cannot be hard coded into the batch script ?

      Of course, invoking a script to perform the task is not such a bad idea - but I think I'll make it a script that's at least readable:
      use strict; use warnings; die "Usage: 'perl compile_it.pl filename'" unless @ARGV == 1; my $args = `perl -MExtUtils::Embed -e "ccopts"`; chomp $args; $args .= `perl -MExtUtils::Embed -e "ldopts"`; chomp $args; system "gcc", "-o", "$ARGV[0].exe", "$ARGV[0].c", $args;
      Thanks holli.

      Cheers,
      Rob
Re: [OT] Windows cmd.exe shell
by soonix (Chancellor) on Aug 01, 2019 at 17:41 UTC
    • newer Windows Systems have PowerShell out-of-the-box. It's not a drop-in replacement for CMD.EXE, and I find its (as I would call it) "similarity-but-difference" to Perl somehow appalling, but should be useful for your case. According to this SO thread you would be able to write
      cc -o interp interp.c $(perl -MExtUtils::Embed -e ccopts -e ldopts)
    • Git for Windows includes a "git BASH", which is a GNU bash based on MSYS
    • And, lastly, this anonymous monk has a point ;-)
      newer Windows Systems have PowerShell out-of-the-box

      Yes, I did see that it was possible to do in PowerShell.
      And I do have PowerShell - though I haven't tested this particular feature of it.

      Git for Windows includes a "git BASH", which is a GNU bash based on MSYS

      I'm also aware of that option.
      I do have MSYS2 and often use its bash shell. This is the shell I'd go to if I wanted to avoid cmd.exe
      However, with native windows perls, I'm usually running the cmd.exe shell - and it will probably stay that way.

      And, lastly, this anonymous monk has a point ;-)

      Indeed !!

      Cheers,
      Rob
Re: [OT] Windows cmd.exe shell - use a Makefile
by bliako (Abbot) on Aug 01, 2019 at 18:46 UTC

    Why not a Makefile? This is the proper way AFAIC.

    Here is a skeleton project I wrote for compiling simple perl-embedding code using GNU Autotools: https://github.com/hadjiprocopis/perl-embed-autotools.

    It uses already existing m4 macro AX_PERL_EXT (in m4/ax_perl_ext.m4) and my own m4 macro AX_PERL_EMBED_EXT (in m4/ax_perl_embed_ext.m4) to provide some more compilation variables.

    sh bootstrap.sh && ./configure && make all will get you started if you are in a unix system. If on a windows system just execute the autotools commands in bootstrap.sh manually or in a batch file - provided you already have GNU Autotools installed, see https://www.gnu.org/software/automake.

    bw, bliako

    (Edit: 40 minutes after posting: git is now good to go)

      Here is a skeleton project I wrote for compiling simple perl-embedding code using GNU Autotools: https://github.com/hadjiprocopis/perl-embed-autotools.

      TIMTOWTDI, as always ;-)
      That looks like a cool little project.
      I'm not so sure that *I* will ever get around to making use of it - but ++ anyway.

      Cheers,
      Rob
Re: [OT] Windows cmd.exe shell (perl.exe)
by Anonymous Monk on Aug 01, 2019 at 07:33 UTC

    Works fine ... but there must be something better ?

    Yup, perl.exe is better, its like you're using it already :)

      Yup, perl.exe is better, its like you're using it already :)

      Yes - I don't think I've ever been faced with a task that is best solved by embedding perl in C code.
      But even if embedding perl in C is unnecessary and/or perverse (and I'm not saying that it's always at least one of those), I still find it to be an interesting feature in its own right.
      In fact, I find it so interesting that once every couple of years I'll spend an hour or two fiddling with it ;-)

      Cheers,
      Rob
        LOL

        perl -MConfig -MExtUtils::Embed -e " system $Config{cc}, qw/ -o interp interp.c /, ccopts(), ldopts() ; "