[comp.lang.c++] C++ masquerading as C ... How?

hardin@hpindda.HP.COM (John Hardin) (01/06/90)

Pardon the ignorance of a beginner, but can someone tell me how to make a
separately compiled function written in C++ look like a normal C function
to a program written in C?  Is this possible, or must the C program call
the external function by the encoded name produced by C++?

I inferred that this was possible from a line in B. Stroustrup's article
"Type-safe Linkage for C++":

    This leaves the problem of how to call a C function or a C++
    function "masquerading" as a C function.

What I'm asking is how one makes a C++ function masquerade as a C function.

Thanks in advance for any help.

John Hardin
Hewlett Packard, Information Networks Division
hardin%hpindgi@hplabs.hp.com
----------------------------

shap@delrey.sgi.com (Jonathan Shapiro) (01/08/90)

In article <6170011@hpindda.HP.COM> hardin@hpindda.HP.COM (John Hardin) writes:
> Can someone tell me how to make a separately compiled function
> written in C++ look like a normal C function to a program written in
> C?
>
>John Hardin
>Hewlett Packard, Information Networks Division
>hardin%hpindgi@hplabs.hp.com
>----------------------------

There are a couple of strategies.

1. You can declare the function using extern "C":

    extern "C" int froboz(int);

    int froboz(int i)
    {
	    return i;
    }

This has the disadvantage that C++ code that uses it won't get
typesafe linkage.

2. You can declare the function with a slightly different name for c:

    extern "C" int c_froboz(int);

    int froboz(int i)  // c++ version
    {
	    return i;
    }

    int c_froboz(int i) { return froboz(i); }  // C version

Advantage is typesafe linkage for C, but disadvantage is the name is
different.

Hope it helps

Jonathan S. Shapiro
Silicon Graphics, Inc.

hardin@hpindda.HP.COM (John Hardin) (01/09/90)

In answer to my question about how to make a C++ function masquerade
as C, I received several replies all saying that I should use the
'extern "C"' construct.  Though I was familiar with the use of this
construct to tell C++ to use C linkage when linking with functions
writen in C, I was not aware that it should be used to tell C++
to generate C function names to the linker for locally defined
procedures.

For those who asked me to post what I found out, I offer this
simple example (which I used to prove to myself that it works):

-----------------------> test.c <-----------------------

#include <stdio.h>

main ()
  {
  printf ("The length of 'abcd' is %d.\n", foo ("abcd"));
  }

-----------------------> test2.C <-----------------------

#include <string.h>

extern "C" int foo (char * str)
  {
  return (strlen (str));
  }

----------------------------------------------------------

I created the executable 'test' as follows:

    CC -c test2.C
    cc -c test.c
    CC -o test test.o test2.o

Indeed this did link properly and produced the output

    The length of 'abcd' is 4.

To those who were kind enough to set me straight, thank you!

John Hardin
hardin%hpindgh@hplabs.hp.com
----------

jimad@microsoft.UUCP (JAMES ADCOCK) (01/10/90)

Although not stated anywhere explicetly, the extern "C" construct works both
ways -- it can be used to tell a C++ program to look for a C function
of that name, but it can also be used to tell a C++ program to generate
a function of that name -- IE turn off name mangling for this one function:

#include <stdio.h>

extern "C" int foo() { return 1234; }

int foo2() { return 1234; }

main()
{
    printf("%d\n", foo() );
    printf("%d\n", foo2() );
}

On my machine, in linker land foo is known as "_foo" [ as would be a 
function written in K&R C ], whereas foo2 is known by the mangled name
"_foo__Fv"