delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2001/01/16/00:52:30

Message-Id: <3.0.1.32.20010116134309.006c0464@wingate>
X-Sender: n_abing#ns DOT roxas-online DOT net DOT ph AT wingate
X-Mailer: Windows Eudora Pro Version 3.0.1 (32)
Date: Tue, 16 Jan 2001 13:43:09 +0800
To: djgpp AT delorie DOT com
From: "Nimrod A. Abing" <n_abing AT ns DOT roxas-online DOT net DOT ph>
Subject: Re: Calling C++ functions from C (or using Bison with C++)
In-Reply-To: <3A62A587.AC673A86@falconsoft.be>
References: <3 DOT 0 DOT 1 DOT 32 DOT 20010112084816 DOT 006bc314 AT wingate>
<3 DOT 0 DOT 1 DOT 32 DOT 20010112084816 DOT 006bc314 AT wingate>
<3 DOT 0 DOT 1 DOT 32 DOT 20010115131743 DOT 006b86ec AT wingate>
Mime-Version: 1.0
Reply-To: djgpp AT delorie DOT com
Errors-To: nobody AT delorie DOT com
X-Mailing-List: djgpp AT delorie DOT com
X-Unsubscribes-To: listserv AT delorie DOT com

At 08:23 AM 01/15/2001 +0100, you wrote:
>"Nimrod A. Abing" wrote:
>> gets turned into a C program by the Bison program. Tim Van Holder's
>> suggestion of using ``extern'' language declarations will not (and does
>> not) compile because C doesn't recognize the extern "C" construct. Although
>Ah, is THAT why it doesn't work? That's easy then: surround them with
>#ifdef __cplusplus.

Oh' beautiful day!!! Thanks Tim. It works now as you suggested. I got
confused with the extern "C" part because I thought that it was a compiler
thing when I remembered (after a refresher on C++) Stroustrup calling it a
"linkage specification". I've had to do more of the same with the header
files so I can mix C and C++ class decls/defns. Heheh, I'm rewriting most
of the C routines in C++ just because it's so much fun.

>Oh, and don't count on pointer-to-functions supporting virtual
>functions;
>IIRC they're stored as offsets to the relevant entry in the virtual
>table,
>not as pointers to actual functions.

I agree, it would break the assumption that the pointer to a virtual member
function used in a C program points the same address all the time. What if
it gets swapped out on disk? All sorts of nastiness there.

>I'm not even sure you can be sure a C++ function without 'extern "C"'
>will
>have the same calling convention as a C function, so
>pointers-to-functions
>are not guaranteed to work (I think).

Which is why Stroustrup suggests using extern "C" whenever there is the
need to call C++ functions and use C++ typedefs and stuff in C. You need to
wrap them in the extern "C" { ... }.

[ test.h ]
. . .

extern "C" {
void c_sort(void*, void*, int(*cmp)(void*, void*)); C Linkage
int c_cmpfn(void*, void*);
}

void cxx_sort(void*, void*, int(*cmp)(void*, void*)); // C++ Linkage
int cxx_cmpfn(void*, void*);

. . .
[ test.h]

[ test.cc ]
. . .

c_sort(array1, array2, c_cmpfn); // Ok.
c_sort(array1, array2, cxx_cmpfn); // May work with some compilers.

. . .
[test.cc]

The second call may or may not work because there is no guarantee for some
compilers that C and C++ calling conventions are the same. It seems to work
in gcc, but then again this is not portable and we don't like that now, do we?

Thanks again for your suggestions Tim. BTW, are you still working on DLX?

nimrod_a_abing
--------------

+========================================+
|  Home page: www.geocities.com/n_abing  |
+========================================+

"Tinimbang ka ngunit kulang."
If you understand that phrase, i-email mo'ko. ;-)

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019