[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: using #define preprocessor



TongEng Chiah wrote:
> 
> i read through some kernel codes and notice the use of
> #define
> 
> i was wondering whether i can made use of it in the
> way described below
> 
> i want to write a module called MYMODULE
> in this module i define: #define mymodule
> 
> so in the kernel codes, i want to add in some
> conditions
> 
> #ifdef mymodule
>    blah
>    blah
> #endif
> 
> the codes will only be run if MYMODULE has been loaded
> else, the codes won't run.
> 
> is what i described above possible?
> how can it be done?

It is not possible to do it the way you describe, because
#ifdef..#endif controls *compilation* of code, not
run-time behavior. You instead need something like:

/* In some non-module kernel file: */
int my_module_is_loaded = 0;
/* Export this, and set it to 1 in your module initialization
   code, and back to 0 when it unloads. */

Then, wherever you want to detect the presence of your
module, "if (my_module_is_loaded) { ... }".

There may be a much better way to handle this that I
am not aware of. This solution is kind of ugly, IMO.
Actually it seems kind of bad for the core kernel code
to make decisions based on the presence of absence
of any particular module, but maybe I'm way off
base there.

-- Joe Knapka
-
Kernelnewbies: Help each other learn about the Linux kernel.
Archive:       http://mail.nl.linux.org/
IRC Channel:   irc.openprojects.net / #kernelnewbies
Web Page:      http://www.surriel.com/kernelnewbies.shtml