开发者

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)
{
    ...开发者_如何学C
}

How's the value of CONFIG_IP_MULTIPLE_TABLES populated?


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


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


Using a #define. For instance:

#define CONFIG_IP_MULTIPLE_TABLES


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)


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜