Understanding #ifdef in .h files
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) { }
#endiftest_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
Hi Harold
Quoted 4 lines · Harold Andr?
Hi,
I try to understand how #ifdef in .h files works.
Quoted 9 lines
I read Greg Kroah-Hartman's Coding style paper
http://www.kroah.com/linux/talks/ols_2002_kernel_codingstyle_talk/html/mgp00031.htmlAnd 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:
You say here you will define the function else where if TEST_FUNCTION is defined
Quoted 10 lines
#ifdef TEST_FUNCTION void test(int *value); #else static inline void test(int *value) { } #endif
test_ifdef.c:#include "test_ifdef.h"
But here you do on and define it any way. You must stick to the rule you created earlier. If you are defining it here then this must also be under the test of ifdef TEST_FUNCTION
Quoted 53 lines
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 at kernelnewbies.org
http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
Signature · 1 line
---P.K.S
Le Fri, 20 Jun 2014 16:36:58 +0530,
Pranay Srivastava <[email protected]> a ?crit :
Quoted 9 lines
You say here you will define the function else where if
TEST_FUNCTIONis definedQuoted 6 lines
#ifdef TEST_FUNCTION void test(int *value); #else static inline void test(int *value) { } #endif
Quoted 12 lines
Quoted 4 lines
test_ifdef.c:#include "test_ifdef.h"But here you do on and define it any way. You must stick to the rule you created earlier. If you are defining it here then this must also be under the test of ifdef
TEST_FUNCTIONQuoted 5 lines
void test(int *value) { *value += 1; }
Thank you Pranay for your answer. I understand this now.
But when i look at the kernel code, i don't see ifdef test around the definition of the function. For example, i look for the function "hiddev_hid_event":
In include/linux/hiddev.h:
#ifdef CONFIG_USB_HIDDEV
...
void hiddev_hid_event(struct hid_device *hid, struct hid_field *field,
struct hid_usage *usage, __s32 value);
...
#else
...
static inline void hiddev_hid_event(struct hid_device *hid, struct hid_field *field, struct hid_usage *usage, __s32 value) { }
...
#endif
In drivers/hid/usbhid/hiddev.c:
/*
* This is where hid.c calls into hiddev to pass an event that occurred
over
* the interrupt pipe
*/
void hiddev_hid_event(struct hid_device *hid, struct hid_field *field,
struct hid_usage *usage, __s32 value)
{
unsigned type = field->report_type;
struct hiddev_usage_ref uref;
uref.report_type =
(type == HID_INPUT_REPORT) ? HID_REPORT_TYPE_INPUT :
((type == HID_OUTPUT_REPORT) ? HID_REPORT_TYPE_OUTPUT :
((type == HID_FEATURE_REPORT) ? HID_REPORT_TYPE_FEATURE :
0)); uref.report_id = field->report->id;
uref.field_index = field->index;
uref.usage_index = (usage - field->usage);
uref.usage_code = usage->hid;
uref.value = value;
hiddev_send_event(hid, &uref);
}EXPORT_SYMBOL_GPL(hiddev_hid_event);
And in drivers/hid/hid-core.c:
...
if (hid->claimed & HID_CLAIMED_INPUT) hidinput_hid_event(hid, field, usage, value); if (hid->claimed & HID_CLAIMED_HIDDEV && interrupt && hid->hiddev_hid_event) hid->hiddev_hid_event(hid, field, usage, value);
...
If i'm right. The function "hiddev_hid_event" is always defined in "hiddev.c" whatever "CONFIG_USB_HIDDEV" is defined or not ? How is it possible ?
Quoted 45 lines · Harold Andr?
Le Fri, 20 Jun 2014 16:36:58 +0530,
Pranay Srivastava <[email protected]> a ?crit :Quoted 9 lines
You say here you will define the function else where if
TEST_FUNCTIONis definedQuoted 6 lines
#ifdef TEST_FUNCTION void test(int *value); #else static inline void test(int *value) { } #endifQuoted 12 lines
Quoted 4 lines
test_ifdef.c:#include "test_ifdef.h"But here you do on and define it any way. You must stick to the rule you created earlier. If you are defining it here then this must also be under the test of ifdef
TEST_FUNCTIONQuoted 5 lines
void test(int *value) { *value += 1; }Thank you Pranay for your answer. I understand this now.
But when i look at the kernel code, i don't see ifdef test around the definition of the function. For example, i look for the function "
hiddev_hid_event":In
include/linux/hiddev.h:
#ifdefCONFIG_USB_HIDDEV
...
void hiddev_hid_event(structhid_device*hid, structhid_field*field,struct hid_usage *usage, __s32 value);...
#else
...
static inline voidhiddev_hid_event(struct hid_device *hid, struct hid_field *field, struct hid_usage *usage, __s32 value){ }
...
#endif
Quoted 4 lines
In
drivers/hid/usbhid/hiddev.c:
/*
* This is wherehid.ccalls into hiddev to pass an event that occurred
over
That's right but the magic doesn't happen here :-)
if you see usbhid/Makefile you'll understand what I mean :-). If not , please do ask.
Quoted 35 lines
* the interrupt pipe
*/
void hiddev_hid_event(structhid_device*hid, structhid_field*field,
structhid_usage*usage,__s32value){ unsigned type = field->report_type; struct hiddev_usage_ref uref; uref.report_type = (type == HID_INPUT_REPORT) ? HID_REPORT_TYPE_INPUT : ((type == HID_OUTPUT_REPORT) ? HID_REPORT_TYPE_OUTPUT : ((type == HID_FEATURE_REPORT) ? HID_REPORT_TYPE_FEATURE : 0)); uref.report_id = field->report->id; uref.field_index = field->index; uref.usage_index = (usage - field->usage); uref.usage_code = usage->hid; uref.value = value; hiddev_send_event(hid, &uref); }
EXPORT_SYMBOL_GPL(hiddev_hid_event);And in
drivers/hid/hid-core.c:
...if (hid->claimed & HID_CLAIMED_INPUT) hidinput_hid_event(hid, field, usage, value); if (hid->claimed & HID_CLAIMED_HIDDEV && interrupt && hid->hiddev_hid_event) hid->hiddev_hid_event(hid, field, usage, value);...
If i'm right. The function "
hiddev_hid_event" is always defined in "hiddev.c" whatever "CONFIG_USB_HIDDEV" is defined or not ? How is it possible ?
Signature · 1 line
---P.K.S
Le Fri, 20 Jun 2014 18:44:35 +0530,
Pranay Srivastava <[email protected]> a ?crit :
Quoted 52 lines
Quoted 45 lines · Harold Andr?
Le Fri, 20 Jun 2014 16:36:58 +0530,
Pranay Srivastava <[email protected]> a ?crit :Quoted 9 lines
You say here you will define the function else where if
TEST_FUNCTIONis definedQuoted 6 lines
#ifdef TEST_FUNCTION void test(int *value); #else static inline void test(int *value) { } #endifQuoted 12 lines
Quoted 4 lines
test_ifdef.c:#include "test_ifdef.h"But here you do on and define it any way. You must stick to the rule you created earlier. If you are defining it here then this must also be under the test of ifdef
TEST_FUNCTIONQuoted 5 lines
void test(int *value) { *value += 1; }Thank you Pranay for your answer. I understand this now.
But when i look at the kernel code, i don't see ifdef test around the definition of the function. For example, i look for the function "
hiddev_hid_event":In
include/linux/hiddev.h:
#ifdefCONFIG_USB_HIDDEV
...
voidhiddev_hid_event(struct hid_device *hid, struct hid_field *field, struct hid_usage *usage, __s32 value);
...
#else
...
static inline voidhiddev_hid_event(struct hid_device *hid, struct hid_field *field, struct hid_usage *usage, __s32 value){ }
...
#endifQuoted 4 lines
In
drivers/hid/usbhid/hiddev.c:
/*
* This is wherehid.ccalls into hiddev to pass an event that
occurred over
Quoted 4 lines
That's right but the magic doesn't happen here :-)
if you see usbhid/Makefile you'll understand what I mean :-). If not , please do ask.
Ah yes !! Ok, i understand !! ;-)
Thank Pranay for sharing your knowledge !