tags:

views:

122

answers:

7
#ifdef CONFIG_IP_MULTIPLE_TABLES
struct fib_table * fib_hash_init(int id)
#else
struct fib_table * _ _init fib_hash_init(int id)
{
    ...
}

How's the value of CONFIG_IP_MULTIPLE_TABLES populated?

+1  A: 

Using a #define. For instance:

#define CONFIG_IP_MULTIPLE_TABLES
nc3b
But the above code is used to check whether the kernel support some feature, say,we can't predictably hardcode the `#define`,right?
httpinterpret
Not necessarily in this file. You can add it in another file (that maybe `#includes` this file) or give it as an argument in the command line, in your `makefile` etc.
nc3b
+2  A: 

It can be either in a #define statement that's been seen previously, or passed in to the compiler (typically using the -D option).

Paul Hankin
+11  A: 

You can do it in one of at least two ways:

#define CONFIG_IP_MULTIPLE_TABLES

With many compilers, define it on the compile command line:

cc ... -DCONFIG_IP_MULTIPLE_TABLES
WhirlWind
+1  A: 

I believe this is what you are asking:

#define CONFIG_IP_MULTIPLE_TABLES
#ifdef CONFIG_IP_MULTIPLE_TABLES
struct fib_table * fib_hash_init(int id)
#else
struct fib_table * _ _init fib_hash_init(int id)
{
    ...
}

This will evaluate into

struct fib_table * fib_hash_init(int id)
Zarel
So `struct fib_table * _ _init fib_hash_init(int id)` is useless here?
httpinterpret
@httpinterpret: It is disregarded if `CONFIG_IP_MULTIPLE_TABLES` has not been defined. For instance, say this file is included by another file. In this other file you may or may not `#define CONFIG_IP_MULTIPLE_TABLES` and this will lead to a different declaration of fib_table
nico
+1  A: 

When you compile in C the C Pre Processor is run first (CPP) which is a simple macro language. Properties or Bindings are passed in this preprocessor usually with the -D argument.

If you passed a -D for CONFIG_IP_MULTIPLE_TABLES it would be defined. and he ifdef would happen.

Adam Gent
+1  A: 

CONFIG_IP_MULTIPLE_TABLES is a parameter passed to the Linux kernel while compiling. If you are referring to the same, it is set while you set up the parameters before compiling the kernel in the config file. If this is set to Y, the compiler option to gcc defines the macro. If set to N, it is not defined.

In the case of Y, struct fib_table * fib_hash_init(int id) is compiled and in the case of N struct fib_table * _ _init fib_hash_init(int id) is compiled. This is because #ifdef and #else are pre processor directives and they are processed before the compiler proper starts looking at the code.

vpit3833
A: 

The macros starting with CONFIG_are called configuration options, they're populated when you do make *config. At this point, you select the various options you want to be integrated in the kernel. When you're done selecting the options you want, a .config file is produced containing the various options you've selected.

Then an header is created include/linux/autoconf.h which contains the definition of the macros corresponding to the configuration option. This header is included on the command line for each file compiled.

Each configuration option is described in a Kconfig file, there's usually one Kconfig file per directory. There's a kconfig.txt in the documentation describing the language.

LB