Missing extern

J

jaswinder

Hello friends

We know that we can use the extern 'C' {} to use C code in C++.

I want instead to have extern 'C++' {} so that I can use C++ code in C.

But the compiler errors.

Any ideas. THANKS.
 
I

Ian Collins

Hello friends

We know that we can use the extern 'C' {} to use C code in C++.

I want instead to have extern 'C++' {} so that I can use C++ code in C.

But the compiler errors.

There's no such construct as extern 'C++' (or extern 'C' in C++, it's
extern "C").
 
O

osmium

jaswinder said:
We know that we can use the extern 'C' {} to use C code in C++.

I want instead to have extern 'C++' {} so that I can use C++ code in
C.

But the compiler errors.

What do you propose the compiler do when it encounters the undeclared
identifier "class"?
 
A

Anand Hariharan

Hello friends

We know that we can use the extern 'C' {} to use C code in C++.

I want instead to have extern 'C++' {} so that I can use C++ code in C.

But the compiler errors.

Any ideas. THANKS.

This is really a C++ question.

Pay careful attention to Ian's response -- in particular, note that he
said you have to use double quotes around C, not single quote (yes,
such 'small' details are important).

extern "C" actually allows C and C++ code to mix by telling the linker
that the declarations within that block should have C linkage. If you
can convince the linker that your C++ code can have C linkage, then
you should be able to achieve what you want. Of course, this means
you cannot have features exclusive to C++ such as templates,
exceptions, classes, function overloading etc. in your C++ function
declarations within the extern "C" block.

hope this helps,
- Anand
 
J

Jorgen Grahn

You can't get there from here.

To mix C and C++, you have to define an interface in terms of C functions,
types, and variables only and then implement the interface in C++.

And there are good examples in the C++ FAQ Lite -- for example, how to
use a simple C++ class from C with not too much trouble (just a bit of
inevitable wrapping).

/Jörgen
 
S

Seebs

Hello friends
We know that we can use the extern 'C' {} to use C code in C++.
Sorta.

I want instead to have extern 'C++' {} so that I can use C++ code in C.

You can't.
But the compiler errors.

No, you do.
Any ideas. THANKS.

If you really want to mix C and C++, your option (singular) is to use
a C++ compiler and wrap the C code in extern C. Or, use a C++ compiler
for the C++ code, but declare an interface with extern C so that you
can access it from C. Probably.

Basically, though, it turns out this is a C++ question, because the C
answer is "there are no provisions for using stuff in other languages."

(More generally, "extern C" doesn't even really let you use C code
in C++, it lets you use other things that are written using the C
linkage in C++, which could be anything from C to Fortran, or even
C++ complete with aggressive use of C++-specific features.)

-s
 

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

No members online now.

Forum statistics

Threads
473,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top