LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   #if defined(__cplusplus) in Kernel (https://www.linuxquestions.org/questions/linux-newbie-8/if-defined-__cplusplus-in-kernel-801691/)

ravishekhar.82 04-13-2010 02:21 AM

#if defined(__cplusplus) in Kernel
 
Hi,

In places there are statement like ...
#if defined(__cplusplus)
//do something.

For example, in include/linux/stddef.h ,

Code:

#if defined(__cplusplus)
#define NULL 0
#else
#define NULL ((void *)0)
#endif

What this statement is doing here and what is the significance of having it. Please explain.

Aquarius_Girl 04-13-2010 02:24 AM

See if the below link helps you !
http://developers.sun.com/solaris/ar...l_linkage.html

grail 04-13-2010 02:37 AM

So I am curious ravi as to how many times and in how many different ways you are going to ask how #define works in C++???

Also, do you not have access to any search engines? Simply pasting in __cplusplus in google has over 200K hits and if you are
really as capable as you have said with C I cannot for the life of me workout why you are unable to get / understand this information.

ravishekhar.82 04-13-2010 03:37 AM

Quote:

So I am curious ravi as to how many times and in how many different ways you are going to ask how #define works in C++???

Hi,

I know what is the meaning of the statement and #define works in C or C++.

I think I have to clarify my question bit more.

Quote:

What this statement is doing here and what is the significance of having it. Please explain
Basically as we all know Kernel code is compiled with gcc not and not having support for C++, then why C++ preprocessor thing is present here.

My guess is that there must be some strong reason for it. I wanted to know that ???

And I again what to say that I am learning internals of kernel and trying to find out why thing are done the way it is.
I approached the way to learn by directly looking into code itself.

grail 04-13-2010 07:19 AM

Quote:

Basically as we all know Kernel code is compiled with gcc not and not having support for C++
This is an inaccurate statement. There is nothing to say that a C++ program cannot include headers that are also used
in the compilation of the kernel.
However, what we are saying is that if you choose to take portions of particular files, as you have been doing, that not all
pieces are immediately transferable.

Point in case, if you took only part of the code you have highlighted:
Quote:

#define NULL ((void *)0)
And place this in your C++ code it may more than likely fail as C++ does not support this construct in this way,
hence why the reference to:
Quote:

#if defined(__cplusplus)
So now this particular set of code is portable to both C and C++

Quote:

And I again what to say that I am learning internals of kernel and trying to find out why thing are done the way it is.
I approached the way to learn by directly looking into code itself.
And again I would say, you need to go and do a course (online or otherwise) as you have really asked very similar
questions about the exact same topic several times.


All times are GMT -5. The time now is 05:46 AM.