httpinterpret
httpinterpret

Reputation: 6709

How are macros evaluated in C?

#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?

Upvotes: 2

Views: 448

Answers (7)

LB40
LB40

Reputation: 12331

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.

Upvotes: 0

WhirlWind
WhirlWind

Reputation: 14112

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

Upvotes: 12

vpit3833
vpit3833

Reputation: 7951

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.

Upvotes: 1

Adam Gent
Adam Gent

Reputation: 49085

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.

Upvotes: 1

Zarel
Zarel

Reputation: 2201

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)

Upvotes: 1

user97370
user97370

Reputation:

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

Upvotes: 2

nc3b
nc3b

Reputation: 16230

Using a #define. For instance:

#define CONFIG_IP_MULTIPLE_TABLES

Upvotes: 1

Related Questions