Help with __cplusplus

A

Arturo

Hello Netlanders, I'm starting to learn the 'C' language
and reading some code I found this code:

#ifdef __cplusplus
extern "C" {
#endif

Is there someone who could explain me about this code, because
I see that this "__cplusplus" and "extern "C" {" is a kind of
standard to see the difference between C and C++, but I'm not sure
and I want more Information ...

If you know about this stuff or about a reading to consult please
let me know it.
 
D

Darrell Grainger

Hello Netlanders, I'm starting to learn the 'C' language
and reading some code I found this code:

#ifdef __cplusplus
extern "C" {
#endif

Is there someone who could explain me about this code, because
I see that this "__cplusplus" and "extern "C" {" is a kind of
standard to see the difference between C and C++, but I'm not sure
and I want more Information ...

If you know about this stuff or about a reading to consult please
let me know it.

This is a feature of C++. The code you are looking at is designed for a
compiler that works as a C or C++ compiler. If you set the switches for C
the #ifdef is false and the extern is used. If you set the switches for
C++ the #ifdef is true and it uses the extern as required by C++. If you
are interested in more information ask the guys in comp.lang.c++ what the
'extern "C"' is all about or check out a book on C++.
 
O

osmium

Arturo said:
I'm starting to learn the 'C' language
and reading some code I found this code:

#ifdef __cplusplus
extern "C" {
#endif

Is there someone who could explain me about this code, because
I see that this "__cplusplus" and "extern "C" {" is a kind of
standard to see the difference between C and C++, but I'm not sure
and I want more Information ...

If you know about this stuff or about a reading to consult please
let me know it.

It looks like you have encountered the header of a library function and the
vendor that supplies the compiler uses the same library for a C++ compiler.
This is the norm, nowadays. __cplusplus means something to *your* compiler
(it can't mean anything in the C standard because time only seems to flow in
the forward direction and C preceded C++). The syntax extern"C" { is a way
to defeat name mangling, a property that C++ has and C does not.

I could be wrong but my guess is you are taking the wrong approach to
learning; trying to absorb and have everything make sense as you encounter
it. A great many people on these newsgroups implicitly advocate that but I,
personally, have had very poor luck my entire life learning *anything* that
way. Get fairly fluent in writing simple programs first, then go back and
fill in the holes in your knowledge. What some people call a spiral or
helical approach. It may take several revisits before you finally absorb it
all - in the case of C++ you may very well die first.

Looking at headers is not a good way for a novice to learn much of anything
except that computer programming is a complex business.
 
I

Irrwahn Grausewitz

Hello Netlanders, I'm starting to learn the 'C' language
and reading some code I found this code:

#ifdef __cplusplus
extern "C" {
#endif

Is there someone who could explain me about this code, because
I see that this "__cplusplus" and "extern "C" {" is a kind of
standard to see the difference between C and C++, but I'm not sure
and I want more Information ...

[flame-shield: warning, partially OT]

__cplusplus is a macro that a C implementation is *not* allowed to
define in standard conforming mode. It's conventionally defined by
C++ compilers. The extern "C" part tells a C++ compiler, that the
following code requires C calling conventions. (Please note: further
questions about C++ should be directed to comp.lang.c++ to avoid
irritations.)
If you know about this stuff or about a reading to consult please
let me know it.

You definitely want to read _The C Programming Language_ by Kernighan
and Ritchie, second edition (K&R2), accompanied by the comp.lang.c-FAQ
(link in signature). BTW, the FAQ provides some more good book
recommendations in section 18.10.

Good luck!

Regards
 
D

Dan Pop

In said:
Hello Netlanders, I'm starting to learn the 'C' language
and reading some code I found this code:

#ifdef __cplusplus
extern "C" {
#endif

Is there someone who could explain me about this code, because
I see that this "__cplusplus" and "extern "C" {" is a kind of
standard to see the difference between C and C++, but I'm not sure
and I want more Information ...

ALL you need to know in the context of the C language is that a C compiler
is NOT allowed to predefine the __cplusplus macro. Therefore, any time
you see a conditional block starting with #ifdef __cplusplus you can
*safely* ignore it.

If you ever learn C++, such things will make sense when included in a C++
program, but in C programs you can simply treat them as meaningless
comments.

Dan
 
K

Keith Thompson

This is a feature of C++. The code you are looking at is designed for a
compiler that works as a C or C++ compiler. If you set the switches for C
the #ifdef is false and the extern is used. If you set the switches for
C++ the #ifdef is true and it uses the extern as required by C++. If you
are interested in more information ask the guys in comp.lang.c++ what the
'extern "C"' is all about or check out a book on C++.

Not necessarily. The headers could be designed to be used by a C
compiler and by a separate C++ compiler, both of which look for header
files in the same place. (But since system headers tend to contain a
lot of implementation-specific code, it's likely that the two
compilers are closely related, even if they aren't a single program.)
 
J

Jack Klein

Hello Netlanders, I'm starting to learn the 'C' language
and reading some code I found this code:

#ifdef __cplusplus
extern "C" {
#endif

Is there someone who could explain me about this code, because
I see that this "__cplusplus" and "extern "C" {" is a kind of
standard to see the difference between C and C++, but I'm not sure
and I want more Information ...

[flame-shield: warning, partially OT]

__cplusplus is a macro that a C implementation is *not* allowed to
define in standard conforming mode. It's conventionally defined by
C++ compilers. The extern "C" part tells a C++ compiler, that the
following code requires C calling conventions. (Please note: further
questions about C++ should be directed to comp.lang.c++ to avoid
irritations.)

Close, but no cigar. C99 implementations are not allowed to define
__cplusplus. Earlier versions of the standard had no such
prohibitions.
 
J

Jack Klein

ALL you need to know in the context of the C language is that a C compiler
is NOT allowed to predefine the __cplusplus macro. Therefore, any time
you see a conditional block starting with #ifdef __cplusplus you can
*safely* ignore it.

This is true for compilers conforming to the C99 standard that you so
love to malign. It is quite false for C90.
 
N

Neil Kurzman

Arturo said:
Hello Netlanders, I'm starting to learn the 'C' language
and reading some code I found this code:

#ifdef __cplusplus
extern "C" {
#endif

Is there someone who could explain me about this code, because
I see that this "__cplusplus" and "extern "C" {" is a kind of
standard to see the difference between C and C++, but I'm not sure
and I want more Information ...

If you know about this stuff or about a reading to consult please
let me know it.

This specific case tells a C++ compiler that the functions for this
library are C functions.
This compiler calls C and C++ functions differently and needs to be
informed that you are trying to call C functions from C++.
But since this is the C group, It is just trivia.
 
D

Dan Pop

In said:
Hello Netlanders, I'm starting to learn the 'C' language
and reading some code I found this code:

#ifdef __cplusplus
extern "C" {
#endif

Is there someone who could explain me about this code, because
I see that this "__cplusplus" and "extern "C" {" is a kind of
standard to see the difference between C and C++, but I'm not sure
and I want more Information ...

[flame-shield: warning, partially OT]

__cplusplus is a macro that a C implementation is *not* allowed to
define in standard conforming mode. It's conventionally defined by
C++ compilers. The extern "C" part tells a C++ compiler, that the
following code requires C calling conventions. (Please note: further
questions about C++ should be directed to comp.lang.c++ to avoid
irritations.)

Close, but no cigar. C99 implementations are not allowed to define
__cplusplus. Earlier versions of the standard had no such
prohibitions.

For implementations of earlier versions of the standard, it was a (huge)
quality of implementation issues.

Dan
 
D

Dan Pop

In said:
This is true for compilers conforming to the C99 standard that you so
love to malign. It is quite false for C90.

It is quite true for C90 *implementations*, unless you can prove
otherwise.

Dan
 
Joined
Sep 8, 2006
Messages
1
Reaction score
0
I have an additional question about __cplusplus. For detail, I am using GNU g++. But maybe the question isn't GNU g++ specific.

-In what g++ header that __cplusplus is defined? Or I have to define the macro myself in my c++ source (in order to have a proper __cplusplus defined to latterly use whenever needed)? Or it is automatically inserted by g++ at compiling?

I have been working in a project in which C++ program links with other C libs.
I have been using stuff:
#ifdef __cplusplus
extern "C" {
#endif
a long time, but didn't know or ask the above question myself until this confusing thing occurs:

The code used to work this way:
-The C headers always have #ifdef __cplusplus extern "C" { #endif stuff around function prototype declarations
-The C++ sources, though already include the C headers, always duplicate the C function prototype declarations by declaring them again inside extern "C" { ...} (without #ifdef __cplusplus)

Then, I thought that the duplications in C++ sources were redundant and removed them.
First, compiling and linking were ok. But the program went wrong when running. Argument passing was wrong when calling a C funtion. At first, I think __cplusplus declaration was missed, so the extern "C" in C header didn't work. So the above question comes in my mind. Where __cplusplus is defined?

But it seems that it isn't the only question. Next, I tried to also remove #ifdef __cplusplus extern "C" { #endif in C headers. Then the compiler complained about "undefined reference to function ..." while linking, which means, __cplusplus is actually always defined (so the compiler was satisfied in the previous test). But I still question where the macro is defined? And why we need both extern "C" in C header and a duplication of the same thing in C++ source?

Thanks!
 
Joined
May 9, 2008
Messages
1
Reaction score
0
Try this program

#include <stdio.h>

int
main(void) {
#ifdef __cplusplus
printf("C++\n");
#endif
printf("C\n");

return 0;
}

Now compile this in two ways:

gcc -g -Wall cplusplus.c -o c
g++ -g -Wall cplusplus.c -o cpp

And then run the both:
./c
./cpp

You will see that __cplusplus is known to g++ but not to gcc

This is not an answer to your question, but may be a nice example.
 

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,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top