kernelnewbies.kernelnewbies.org archive mirror
 help / color / mirror / Atom feed
From: harold.andre@gmx.fr (Harold André)
To: kernelnewbies@lists.kernelnewbies.org
Subject: Understanding #ifdef in .h files
Date: Fri, 20 Jun 2014 12:58:56 +0200	[thread overview]
Message-ID: <20140620125856.7b022baa@debian-desktop> (raw)

Hi,

I try to understand how #ifdef in .h files works.

I read Greg Kroah-Hartman's Coding style paper
http://www.kroah.com/linux/talks/ols_2002_kernel_codingstyle_talk/html/mgp00031.html

And as he says, i try to do a simple example but it does not work. I
try with a small piece of C outside the kernel. I have 3 files.


test_ifdef.h:

	#ifdef TEST_FUNCTION
	void test(int *value);
	#else
	static inline void test(int *value) { }
	#endif

test_ifdef.c:

	#include "test_ifdef.h"

	void test(int *value)
	{
	    *value += 1;
	}

main.c:

	#include <stdio.h>
	#include "test_ifdef.h"

	int main(int argc, char *argv[])
	{
	    int i = 3;
	
	    printf("i = %d\n", i);

	    test(&i);

	    printf("i = %d\n", i);

	    return 0;
	}


And when i compile:

$ gcc -Wall -g main.c test_ifdef.c -o test_ifdef -DTEST_FUNCTION
$ ./test_ifdef 
i = 3
i = 4
$ gcc -Wall -g main.c test_ifdef.c -o test_ifdef
test_ifdef.c:14:6: error: redefinition of ?test?
 void test(int *value)
      ^
In file included from test_ifdef.c:12:0:
test_ifdef.h:17:20: note: previous definition of ?test? was here
 static inline void test(int *value) { }
                    ^
$

I understand why it does not compile. But:
- How it can work in the kernel code ?
- Is-it possible to do this in code outside the kernel ?

Thank you.

Harold

             reply	other threads:[~2014-06-20 10:58 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-06-20 10:58 Harold André [this message]
2014-06-20 11:06 ` Understanding #ifdef in .h files Pranay Srivastava
2014-06-20 12:59   ` Harold André
2014-06-20 13:14     ` Pranay Srivastava
2014-06-20 13:58       ` Harold André

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20140620125856.7b022baa@debian-desktop \
    --to=harold.andre@gmx.fr \
    --cc=kernelnewbies@lists.kernelnewbies.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).