[comp.lang.c++] C --> C++

jima@hplsla.HP.COM (Jim Adcock) (04/15/89)

Say you want to write your routines in C++, but for
compatibility with old-world programmers your routines
need to be callable from C.  Is there a clean way to do this?
IE a way that is not dependant on the latest and
greatest set of gyrations to encode C++ calling
conventions into C-compatible names?  Can you
do a extern "C" like construct that goes the other
way? 

ekrell@hector.UUCP (Eduardo Krell) (04/16/89)

In article <6590096@hplsla.HP.COM> jima@hplsla.HP.COM (Jim Adcock) writes:
>Can you
>do a extern "C" like construct that goes the other
>way? 

You should have added "in a portable way"...
Since C++ doesn't have a standard naming convention for generating
unique names for the loader, each compiler does its own name mangling.
The result is that you even can't link together .o's generated by
different C++ compilers (or even different versions of the same
compiler, if there was a change in the name mangling scheme between
the two versions).

    
Eduardo Krell                   AT&T Bell Laboratories, Murray Hill, NJ

UUCP: {att,decvax,ucbvax}!ulysses!ekrell  Internet: ekrell@ulysses.att.com

jss@hector.UUCP (Jerry Schwarz) (04/16/89)

In article <6590096@hplsla.HP.COM> jima@hplsla.HP.COM (Jim Adcock) asks:
>Say you want to write your routines in C++, but for
>compatibility with old-world programmers your routines
>need to be callable from C.  Is there a clean way to do this?
>IE a way that is not dependant on the latest and
>greatest set of gyrations to encode C++ calling
>conventions into C-compatible names?  Can you
>do a extern "C" like construct that goes the other
>way? 

extern "C" actually goes both ways.

        extern "C" int f() { ... }

does exactly what you want.  (This is currently working in the
internal version of cfront that will eventually become 2.0)

Jerry Schwarz
AT&T Bell Labs, Murray Hill

pj@hrc63.co.uk (Mr P Johnson "Baddow") (05/02/89)

In article <11437@ulysses.homer.nj.att.com>, ekrell@hector.UUCP (Eduardo Krell) writes:
> In article <6590096@hplsla.HP.COM> jima@hplsla.HP.COM (Jim Adcock) writes:
> >Can you
> >do a extern "C" like construct that goes the other
> >way? 
> 
> You should have added "in a portable way"...
> Since C++ doesn't have a standard naming convention for generating
> unique names for the loader, each compiler does its own name mangling.

Yes, but I should be able to tell the compiler "dont mangle this function
name".  A #pragma or something.  It would still be callable from C++ (via
the `extern "C"' mechanism).

Speaking as someone who has tried to do this, I think it is a major gap in
the langauge.

Paul.

ark@alice.UUCP (Andrew Koenig) (05/04/89)

In article <578@hrc63.co.uk>, pj@hrc63.co.uk (Mr P Johnson "Baddow") writes:

> Yes, but I should be able to tell the compiler "dont mangle this function
> name".  A #pragma or something.  It would still be callable from C++ (via
> the `extern "C"' mechanism).

The following works in AT&T C++ 2.0:

	extern "C" void f() { /* stuff */ }
-- 
				--Andrew Koenig
				  ark@europa.att.com

hansen@pegasus.ATT.COM (Tony L. Hansen) (05/04/89)

<> >Can you
<> >do a extern "C" like construct that goes the other
<> >way?
<>
<> You should have added "in a portable way"...
<> Since C++ doesn't have a standard naming convention for generating
<> unique names for the loader, each compiler does its own name mangling.
<
< Yes, but I should be able to tell the compiler "dont mangle this function
< name".  A #pragma or something.  It would still be callable from C++ (via
< the `extern "C"' mechanism).
<
< Speaking as someone who has tried to do this, I think it is a major gap in
< the langauge.

The extern "language" linkage specification works both ways. Not only can
one specify that a function has been written in C, one can also specify that
a C++ function is to be linked just as a C function would be linked. For
example,

	#include <stdio.h>

	extern "C" void foo() { printf("foo\n"); }

	void bar() { printf("bar\n"); }

	main()
	{
		foo();
		bar();
	}

the function "bar" will be known externally as the mangled name (such as
"bar__Fv" or "bar__Nv"), but the function "foo" will be known externally as
the unmangled name "foo". Both cfront 2.0 and Zortech's next C++ compiler
work this way.

By the way, the next version of Zortech's C++ compiler will also support the

	extern "Pascal"

linkage specification, in addition to the extern "C" and "C++" linkage
specifications.

					Tony Hansen
				att!pegasus!hansen, attmail!tony
				    hansen@pegasus.att.com