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

_______________________________________________
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

Reply via email to