[Can I ]"Embed the contents of the source file in the executable".
Yes, though doing so is a bit of a faff and you need to copy&paste and build a multiline string in C.
The MULTLINE() macro below does that, but you need to add \n at the end any line where that is important (eg. the shebang line). For most other lines you don't need to bother, as Perl happily compiles whole programs as one line:
#include <EXTERN.h>
#include <perl.h>
EXTERN_C void xs_init (pTHX);
EXTERN_C void boot_DynaLoader (pTHX_ CV* cv);
EXTERN_C void boot_Win32CORE (pTHX_ CV* cv);
EXTERN_C void
xs_init(pTHX)
{
char *file = __FILE__;
dXSUB_SYS;
/* DynaLoader is a special case */
newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
newXS("Win32CORE::bootstrap", boot_Win32CORE, file);
}
static PerlInterpreter *my_perl; /*** The Perl interpreter ***/
#define MULTILINE(...) #__VA_ARGS__
static char code[] = MULTILINE(
#! perl -slw\n
use strict;
print "Hello from $^O $]\n";
);
int main(int argc, char **argv, char **env) {
char *embedding[] = { "", "-e", "0" };
PERL_SYS_INIT3(&argc,&argv,&env);
my_perl = perl_alloc();
perl_construct(my_perl);
PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
perl_parse(my_perl, NULL, 3, embedding, NULL);
perl_run(my_perl);
// Execute embedded program
eval_pv(code, TRUE);
perl_destruct(my_perl);
perl_free(my_perl);
PERL_SYS_TERM();
return 0;
}
C:\test>cl /W3 -DWIN32 hello.c -I"C:\Perl64\lib\CORE" C:\Perl64\lib\C
+ORE\perl510.lib
Microsoft (R) C/C++ Optimizing Compiler Version 15.00.21022.08 for x64
Copyright (C) Microsoft Corporation. All rights reserved.
hello.c
Microsoft (R) Incremental Linker Version 9.00.21022.08
Copyright (C) Microsoft Corporation. All rights reserved.
/out:hello.exe
hello.obj
C:\Perl64\lib\CORE\perl510.lib
C:\test>.\hello.exe
Hello from MSWin32 5.010001
With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
|