473,479 Members | 2,060 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Help with __cplusplus

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.
Nov 14 '05 #1
10 1371
Arturo writes:
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.
Nov 14 '05 #2
eg******@yahoo.com.mx (Arturo) wrote:
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
--
Irrwahn Grausewitz (ir*******@freenet.de)
welcome to clc: http://www.ungerhu.com/jxh/clc.welcome.txt
clc faq-list : http://www.faqs.org/faqs/C-faq/faq/
clc OT guide : http://benpfaff.org/writings/clc/off-topic.html
Nov 14 '05 #3
On Wed, 9 Jun 2004, Arturo wrote:
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++.

--
Send e-mail to: darrell at cs dot toronto dot edu
Don't send e-mail to vi************@whitehouse.gov
Nov 14 '05 #4
In <d9**************************@posting.google.com > eg******@yahoo.com.mx (Arturo) writes:
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
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #5
da*****@NOMORESPAMcs.utoronto.ca.com (Darrell Grainger) writes:
On Wed, 9 Jun 2004, Arturo wrote:
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++.


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.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #6
On 9 Jun 2004 18:56:55 GMT, Da*****@cern.ch (Dan Pop) wrote in
comp.lang.c:
In <d9**************************@posting.google.com > eg******@yahoo.com.mx (Arturo) writes:
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.


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

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #7
On Wed, 09 Jun 2004 18:49:08 +0200, Irrwahn Grausewitz
<ir*******@freenet.de> wrote in comp.lang.c:
eg******@yahoo.com.mx (Arturo) wrote:
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.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #8


Arturo wrote:
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.
Nov 14 '05 #9
In <v5********************************@4ax.com> Jack Klein <ja*******@spamcop.net> writes:
On Wed, 09 Jun 2004 18:49:08 +0200, Irrwahn Grausewitz
<ir*******@freenet.de> wrote in comp.lang.c:
eg******@yahoo.com.mx (Arturo) wrote:
>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
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #10
In <a7********************************@4ax.com> Jack Klein <ja*******@spamcop.net> writes:
On 9 Jun 2004 18:56:55 GMT, Da*****@cern.ch (Dan Pop) wrote in
comp.lang.c:
In <d9**************************@posting.google.com > eg******@yahoo.com.mx (Arturo) writes:
>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.


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
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
1166
by: Bill Zhao | last post by:
Hi, I am browsing the source codes and it include the below code in a .h file somebody can tell me what the codes below mean ? Bill Zhao ---------------------------------- #ifdef __cplusplus...
7
1780
by: MuZZy | last post by:
HI I just wonder if someone can help me with this: I have a windows app project developed on VC++ 6.0. I need to adapt it to be able to compile as a console app as well (So it would call main()...
4
2749
by: cpptutor2000 | last post by:
Could some C++ guru help me please? I am trying to build an application using gcc 3.2.3, that has a some classes using functions defined in some C files in the same directory. If inside the C++...
1
8441
by: Oodini | last post by:
Hello, Could anyone explain me the following preprocessor sentences: ---------------------------------------------------------------- #if (defined(__STDC__) && !defined(NO_PROTOTYPE)) ||...
8
2228
by: manochavishal | last post by:
Hi I am writing a Program in which i get input as #C1012,S,A#C1013,S,U I want to get C1012,S,A using strtok and then pass this to function CreateVideo which will further strtok this...
3
2370
by: mjohnson | last post by:
I bought a USB IR reciever and installed the driver. The developer kit gives a DLL with the following information: C++ Builder / Microsoft Visual C++: #ifdef __cplusplus extern "C" { #endif ...
46
17157
by: mattia | last post by:
I've see in some code: #ifdef __cplusplus extern "C" { #endif what does it mean? Thanks
0
7033
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6903
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7027
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6726
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
6861
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5318
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
2987
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
2974
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1291
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.