iso c++ standard on c linkage

Y

Your Uncle

I'm given to understand that your standard is specific regarding linking to
c. Furthermore I'm going to speculate that there exists a c++ version of
Keith Thompson, brilliant, encyclopedic and able to confuse by giving me the
exact answer, all 42 volumes. I just want to know wrap a C function so that
I don't stomp all over it when I bring it down the hall. cheers, furunculus
 
J

Jack Klein

I'm given to understand that your standard is specific regarding linking to
c. Furthermore I'm going to speculate that there exists a c++ version of
Keith Thompson, brilliant, encyclopedic and able to confuse by giving me the
exact answer, all 42 volumes. I just want to know wrap a C function so that
I don't stomp all over it when I bring it down the hall. cheers, furunculus

The C++ language allows linkage specifications, of which it
specifically defines exactly one, extern "C". The intent is that such
functions should be compiled in such a way that they may be linked
with code compiled with a compatible C compiler, or perhaps call
system functions designed to be called from C. Note that the standard
neither requires nor guarantees that extern "C" will provide
compatibility with any particular C compiler or system call.

What extern "C" specifically does not do is make the C++ compiler
compile C code. The code is still treated by the compiler as C++
code. Only the linkage is changed.

That means that you can't compile C code with a C++ compiler and
extern "C" if it contains any of the many imcompatibilities between C
and C++.

This function cannot be compiler by a C++ compiler, regardless of
whether you try to give it C linkage:

void func()
{
int new; // reserved word used as variable name
double static_cast; // ditto
void *vp = 0;
int *ip = vp; // can't assign void* to object *
}

If you want to write code that can compile as C and also as C++ with C
linkage, you must restrict yourself to the common subset of C and C++.
In at least some cases, this results in code that is neither good C
nor good C++.
 
Y

Your Uncle

Jack Klein said:
The C++ language allows linkage specifications, of which it
specifically defines exactly one, extern "C". The intent is that such
functions should be compiled in such a way that they may be linked
with code compiled with a compatible C compiler, or perhaps call
system functions designed to be called from C. Note that the standard
neither requires nor guarantees that extern "C" will provide
compatibility with any particular C compiler or system call.
So a commodore 64 with Vectron's compiler is not required to have it. I
believe that if extern "C" is going to work somewhere, then it would be on
my popular OS and implementation.
What extern "C" specifically does not do is make the C++ compiler
compile C code. The code is still treated by the compiler as C++
code. Only the linkage is changed.

That means that you can't compile C code with a C++ compiler and
extern "C" if it contains any of the many imcompatibilities between C
and C++.

This function cannot be compiler by a C++ compiler, regardless of
whether you try to give it C linkage:

void func()
{
int new; // reserved word used as variable name
double static_cast; // ditto
void *vp = 0;
int *ip = vp; // can't assign void* to object *
}

If you want to write code that can compile as C and also as C++ with C
linkage, you must restrict yourself to the common subset of C and C++.
In at least some cases, this results in code that is neither good C
nor good C++.
Rats. Elsewhere it was suggested that I compile the C part and the c++ part
separately and then link them as c++. Is that a pipe dream? cheers, f
 
I

Ian Collins

Your said:
Rats. Elsewhere it was suggested that I compile the C part and the c++ part
separately and then link them as c++. Is that a pipe dream? cheers, f
Compile the C part as C and the C++ part as C++. Link with the C++
compiler.

If any functions in the C++ part have to be called from C, make them
extern "C".

Clear?
 
Y

Your Uncle

Ian Collins said:
Compile the C part as C and the C++ part as C++. Link with the C++
compiler.

If any functions in the C++ part have to be called from C, make them
extern "C".

Clear?
Alles klar. Now I've got to fiddle with my implementation. They don't
really include this part in K&R. ciao, f
 
I

Ian Collins

Your said:
Alles klar. Now I've got to fiddle with my implementation. They don't
really include this part in K&R. ciao, f
Which is hardly surprising what you consider how far K&R predates C++!

Remember, every time you link a C++ application against your system
libraries, you are doing exactly what I said above.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top