Mailing List Archive

Re: C++ in patch.1o
Dean Roehrich writes:
>
> Andy, Ilya, Andreas,
> I've sent a few MM5.06 patches to the makemaker list today that I made
> while working with C++. With Ilya's c_plus_plus-0.1 patch, MM5.06, xsubpp
> 1.922, I can call C++ constructors, destructors, and methods with dynamic
> objects from Perl. I did this with 5.001m.
>
> So... Ilya: I'd like to see your patch fitted to 5.001n. I'd like to see
> this stuff, along with my MM5.06 patches, (xsubpp 1.922 is in patch.1n) go
> into patch.1o--and on to 5.002.
>
> It was pretty much a snap to interface C++ and Perl. I see no reason to
> hold back on this. So Andy, if you were waiting for second opinions--now
> you have it :)
>
> Dean
>

Thanks, even if my main work does not give a lot of fruits recently,
it is very pleasant to see the hobby rewarding.

I sent a newer patch to Andy privately. If you are interested, it
is below.

Btw, here is how I feel about h2xs now: my patches to xsubpp-2.0a
allow literal copies of C declarations for structs (with typedefs and
comments) to be used in .xs files. I feel that h2xs should have
options to extract the struct/union/class declarations into .xs file
(optionally recursively - maybe to a different file to get reed of
noise), and an option for extraction of function declarations (I have
(recursive) extraction of typedefs in one of my Perl files, and
extraction of externs in another, so prototyping should be
easy). After this getting Direct-To-Perl (name modelled after
Direct-To-SOM: compiling C++ into CORBA) should be pretty easy.

Ilya

*** writemain.SH~ Wed Feb 08 16:44:20 1995
--- writemain.SH Tue Oct 10 17:02:48 1995
***************
*** 51,56 ****
--- 51,76 ----


sed '/Do not delete this line--writemain depends on it/q' miniperlmain.c
+
+
+ if test X"$args" != "X" ; then
+ for ext in $args ; do
+ : $ext will either be 'Name' or 'Name1/Name2' etc
+ : convert ext into cname and mname
+ mname=`echo $ext | sed 's!/!::!g'`
+ cname=`echo $mname | sed 's!:!_!g'`
+
+ echo "EXTERN_C void boot_${cname} _((CV* cv));"
+ done
+ fi
+
+ cat << 'EOP'
+
+ static void
+ xs_init()
+ {
+ EOP
+
if test X"$args" != "X" ; then
echo " char *file = __FILE__;"
ai=''
***************
*** 62,68 ****
mname=`echo $ext | sed 's!/!::!g'`
cname=`echo $mname | sed 's!:!_!g'`

! echo " { extern void boot_${cname} _((CV* cv));"
if test "$ext" = "DynaLoader"; then
: Must NOT install 'DynaLoader::boot_DynaLoader' as 'bootstrap'!
: boot_DynaLoader is called directly in DynaLoader.pm
--- 82,88 ----
mname=`echo $ext | sed 's!/!::!g'`
cname=`echo $mname | sed 's!:!_!g'`

! echo " {"
if test "$ext" = "DynaLoader"; then
: Must NOT install 'DynaLoader::boot_DynaLoader' as 'bootstrap'!
: boot_DynaLoader is called directly in DynaLoader.pm
*** miniperlmain.c~ Thu Sep 28 00:00:28 1995
--- miniperlmain.c Tue Oct 10 16:55:34 1995
***************
*** 2,18 ****
--- 2,33 ----
* "The Road goes ever on and on, down from the door where it began."
*/

+ #ifdef __cplusplus
+ extern "C" {
+ #endif
+
#include "EXTERN.h"
#include "perl.h"

+ #ifdef __cplusplus
+ }
+ # define EXTERN_C extern "C"
+ #else
+ # define EXTERN_C extern
+ #endif
+
static void xs_init _((void));
static PerlInterpreter *my_perl;

int
+ #ifndef CAN_PROTOTYPE
main(argc, argv, env)
int argc;
char **argv;
char **env;
+ #else /* def(CAN_PROTOTYPE) */
+ main(int argc, char **argv, char **env)
+ #endif /* def(CAN_PROTOTYPE) */
{
int exitstatus;

***************
*** 46,53 ****

/* Register any extra external extensions */

static void
xs_init()
{
- /* Do not delete this line--writemain depends on it */
}
--- 61,69 ----

/* Register any extra external extensions */

+ /* Do not delete this line--writemain depends on it */
+
static void
xs_init()
{
}
*** lib/ExtUtils/xsubpp~ Thu Jun 22 07:25:32 1995
--- lib/ExtUtils/xsubpp Tue Oct 10 18:53:10 1995
***************
*** 312,318 ****

sub Q {
my($text) = @_;
! $text =~ tr/#//d;
$text =~ s/\[\[/{/g;
$text =~ s/\]\]/}/g;
$text;
--- 312,318 ----

sub Q {
my($text) = @_;
! $text =~ s/^\#//gm;
$text =~ s/\[\[/{/g;
$text =~ s/\]\]/}/g;
$text;
***************
*** 783,788 ****
--- 783,791 ----
# print initialization routine
print qq/extern "C"\n/ if $cplusplus;
print Q<<"EOF";
+ ##ifdef __cplusplus
+ #extern "C"
+ ##endif
#XS(boot_$Module_cname)
#[[
# dXSARGS;
*** h2xs.SH~ Thu Sep 28 00:00:18 1995
--- h2xs.SH Tue Oct 10 18:34:02 1995
***************
*** 294,302 ****
--- 294,308 ----
warn "Writing $ext$modpname/$modfname.xs\n";

print XS <<"END";
+ #ifdef __cplusplus
+ extern "C" {
+ #endif
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
+ #ifdef __cplusplus
+ }
+ #endif

END
if( $path_h ){
*** minimod.PL.orig Tue Oct 31 08:12:28 1995
--- minimod.PL Thu Nov 02 22:57:48 1995
***************
*** 53,65 ****
my($pname);
my($dl) = canon('/','DynaLoader');
print $head;
print " char *file = __FILE__;\n";
foreach $_ (@exts){
my($pname) = canon('/', $_);
my($mname, $cname, $ccode);
($mname = $pname) =~ s!/!::!g;
($cname = $pname) =~ s!/!__!g;
! print "\t{ extern void boot_${cname} _((CV* cv));\n";
if ($pname eq $dl){
# Must NOT install 'DynaLoader::boot_DynaLoader' as 'bootstrap'!
# boot_DynaLoader is called directly in DynaLoader.pm
--- 53,77 ----
my($pname);
my($dl) = canon('/','DynaLoader');
print $head;
+
+ foreach $_ (@exts){
+ my($pname) = canon('/', $_);
+ my($mname, $cname);
+ ($mname = $pname) =~ s!/!::!g;
+ ($cname = $pname) =~ s!/!__!g;
+ print "EXTERN_C void boot_${cname} _((CV* cv));\n";
+ }
+
+ my ($tail1,$tail2) = ( $tail =~ /\A(.*\n)(\s*\}.*)\Z/s );
+ print $tail1;
+
print " char *file = __FILE__;\n";
foreach $_ (@exts){
my($pname) = canon('/', $_);
my($mname, $cname, $ccode);
($mname = $pname) =~ s!/!::!g;
($cname = $pname) =~ s!/!__!g;
! print "\t{\n";
if ($pname eq $dl){
# Must NOT install 'DynaLoader::boot_DynaLoader' as 'bootstrap'!
# boot_DynaLoader is called directly in DynaLoader.pm
***************
*** 72,78 ****
}
print "\t}\n";
}
! print $tail;
}

sub canon{
--- 84,90 ----
}
print "\t}\n";
}
! print $tail2;
}

sub canon{
Re: C++ in patch.1o [ In reply to ]
> I think we had this discussion before, but I don't think it was ever
> resolved. So I'll ask the question I asked before - Is __cplusplus
> defined in ALL C++ compilers?

"__cplusplus" was introduced when ANSI C expropriated symbols not
starting with "__". Before that, the few compilers I've checked used
"c_plusplus".


Regards,

Hallvard
Re: C++ in patch.1o [ In reply to ]
>I think we had this discussion before, but I don't think it was ever
>resolved. So I'll ask the question I asked before - Is __cplusplus
>defined in ALL C++ compilers? or will you rely on MakeMaker to define it?

The C++ standard draft (of april 28, 1995) states that this macro
should be defined for all C++ implementations:

This is a quote from:
http://www.ph.tn.tudelft.nl/People/klamer/wp/cpp.html

16.8 Predefined macro names [cpp.predefined]

1 The following macro names shall be defined by the implementation:

__LINE__
The line number of the current source line (a decimal constant).

__FILE__
The presumed name of the source file (a character string literal).

__DATE__
The date of translation of the source file (a character string lit­
eral of the form "Mmm dd yyyy", where the names of the months are
the same as those generated by the asctime function, and the first
character of dd is a space character if the value is less than 10).
If the date of translation is not available, an implementation-
defined valid date shall be supplied.

__TIME__
The time of translation of the source file (a character string lit­
eral of the form "hh:mm:ss" as in the time generated by the asctime
function). If the time of translation is not available, an imple­
mentation-defined valid time shall be supplied.

__STDC__
Whether __STDC__ is defined and if so, what its value is, are imple­
mentation-defined.

__cplusplus
The name __cplusplus is defined (to an unspecified value) when com­
piling a C++ translation unit.

which I found by starting my Web-browser at

http://www.cygnus.com/misc/wp/index.html

--Gisle
Re: C++ in patch.1o [ In reply to ]
>> "__cplusplus" was introduced when ANSI C expropriated symbols not
>> starting with "__". Before that, the few compilers I've checked used
>> "c_plusplus".
>
> I take it that means that there are C++ compilers out there which do
> not define "__cplusplus". Yes?

Yes. Whether they'd compile an average C++ program written for modern
compilers is another matter. C++ has evolved a lot.

> Is this something that MakeMaker/Configure should deal with?

I don't know where to deal with it... I sure wouldn't try to guess which
C++ compiler people will use, when I install perl. I jumped into this
thread too late, but couldn't you put this somewhere?
#if defined(c_plusplus) && !defined(__cplusplus)
/* This museum piece never heard of __cplusplus */
#define __cplusplus c_plusplus
#endif


Regards,

Hallvard
Re: C++ in patch.1o [ In reply to ]
Paul Marquess writes:
>
> From: Gisle Aas <aas@oslonett.no>
> > The C++ standard draft (of april 28, 1995) states that this macro
> > should be defined for all C++ implementations:
>
> What about C++ compilers that existed before april 28, 1995?
>

Draft exists for many years, though not the same ;-).

Ilya
Re: C++ in patch.1o [ In reply to ]
Hallvard B Furuseth writes:
>
> >> "__cplusplus" was introduced when ANSI C expropriated symbols not
> >> starting with "__". Before that, the few compilers I've checked used
> >> "c_plusplus".
> >
> > I take it that means that there are C++ compilers out there which do
> > not define "__cplusplus". Yes?
>
> Yes. Whether they'd compile an average C++ program written for modern
> compilers is another matter. C++ has evolved a lot.
>

While there are reasons perl should compile on _any_ available C
compiler, I see no reason why the same should be applicable to C++.

> > Is this something that MakeMaker/Configure should deal with?
>
> I don't know where to deal with it... I sure wouldn't try to guess which
> C++ compiler people will use, when I install perl. I jumped into this
> thread too late, but couldn't you put this somewhere?
> #if defined(c_plusplus) && !defined(__cplusplus)
> /* This museum piece never heard of __cplusplus */
> #define __cplusplus c_plusplus
> #endif
>

If this will solve some problems, sure... In fact, I think it is much
more reasonable to put the extern "c" into perl.h and XSUB.h. This
will clean up the .c files.

Declarations for ...__init are necessary in perlmain.c, though.

Ilya
Re: C++ in patch.1o [ In reply to ]
Hallvard B Furuseth writes:
> I don't know where to deal with it... I sure wouldn't try to guess which
> C++ compiler people will use, when I install perl. I jumped into this
> thread too late, but couldn't you put this somewhere?
> #if defined(c_plusplus) && !defined(__cplusplus)
> /* This museum piece never heard of __cplusplus */
> #define __cplusplus c_plusplus
> #endif
>

Since the question how to invoke C++ compiler is not solved so far,
one may always add -D__cplusplus to the command line of a broken
compiler. I do not think we should go overboard about this issue.

Ilya
Re: C++ in patch.1o [ In reply to ]
From: Ilya Zakharevich <ilya@math.ohio-state.edu>
>
> Dean Roehrich writes:
> >
> > Andy, Ilya, Andreas,
> > I've sent a few MM5.06 patches to the makemaker list today that I made
> > while working with C++. With Ilya's c_plus_plus-0.1 patch, MM5.06, xsubpp
> > 1.922, I can call C++ constructors, destructors, and methods with dynamic
> > objects from Perl. I did this with 5.001m.
> >
> > So... Ilya: I'd like to see your patch fitted to 5.001n. I'd like to see
> > this stuff, along with my MM5.06 patches, (xsubpp 1.922 is in patch.1n) go
> > into patch.1o--and on to 5.002.
> >
> > It was pretty much a snap to interface C++ and Perl. I see no reason to
> > hold back on this. So Andy, if you were waiting for second opinions--now
> > you have it :)
> >
> > Dean
> >
>
> Thanks, even if my main work does not give a lot of fruits recently,
> it is very pleasant to see the hobby rewarding.
>
> I sent a newer patch to Andy privately. If you are interested, it
> is below.
>
> Btw, here is how I feel about h2xs now: my patches to xsubpp-2.0a
> allow literal copies of C declarations for structs (with typedefs and
> comments) to be used in .xs files. I feel that h2xs should have
> options to extract the struct/union/class declarations into .xs file
> (optionally recursively - maybe to a different file to get reed of
> noise), and an option for extraction of function declarations (I have
> (recursive) extraction of typedefs in one of my Perl files, and
> extraction of externs in another, so prototyping should be
> easy). After this getting Direct-To-Perl (name modelled after
> Direct-To-SOM: compiling C++ into CORBA) should be pretty easy.
>
> Ilya
>

I think we had this discussion before, but I don't think it was ever
resolved. So I'll ask the question I asked before - Is __cplusplus
defined in ALL C++ compilers? or will you rely on MakeMaker to define it?

Paul

p.s. I do intend to get back to xsubpp 2.0 soon.
Re: C++ in patch.1o [ In reply to ]
From: Hallvard B Furuseth <h.b.furuseth@usit.uio.no>
>
> > I think we had this discussion before, but I don't think it was ever
> > resolved. So I'll ask the question I asked before - Is __cplusplus
> > defined in ALL C++ compilers?
>
> "__cplusplus" was introduced when ANSI C expropriated symbols not
> starting with "__". Before that, the few compilers I've checked used
> "c_plusplus".

I take it that means that there are C++ compilers out there which do
not define "__cplusplus". Yes?

Is this something that MakeMaker/Configure should deal with?

Paul
Re: C++ in patch.1o [ In reply to ]
From: Gisle Aas <aas@oslonett.no>
>
> >I think we had this discussion before, but I don't think it was ever
> >resolved. So I'll ask the question I asked before - Is __cplusplus
> >defined in ALL C++ compilers? or will you rely on MakeMaker to define it?
>
> The C++ standard draft (of april 28, 1995) states that this macro
> should be defined for all C++ implementations:

What about C++ compilers that existed before april 28, 1995?

Paul