0

I'm new at kernel programming and trying to do a "Hello World" example. I added the following code to init/main.c in start_kernel()

#ifdef HELLO
   printk("Hello World");
#endif

Now to my question. How do i define HELLO in the boot parameters using qemu?

nist
  • 1,706
  • 3
  • 16
  • 24

1 Answers1

1

You need to define HELLO at compile time, (either with -DHELLO as a compiler flag or #define HELLO somewhere), otherwise the compiler never even sees the printk call and no code for it gets emitted.

You can't make the C compiler get re-run at early stage boot time based on the boot parameters, which is what you'd need to do to change HELLO there.

The kernel is no different than any other C program in this respect - preprocessor directives are handled really early on in compilation.

You can setup parameters with this helper macro that are a regular variable that can be set a boot and tested at runtime (not compile time) with a plain old if statement.

Flexo
  • 87,323
  • 22
  • 191
  • 272
  • My question is if there are any way to define HELLO after the code are compiled by using a boot parameter – nist Feb 23 '12 at 17:08
  • 1
    @nist - The kernel isn't special, the pre-processor still happens really early in compilation just like any other C program. You can write normal `if` statements in the kernel just like you can in any other C program though if you want instead of `#ifdef` – Flexo Feb 23 '12 at 17:11
  • Thanks for the info. If I use if statements how could i make my kernel behave diffrently with diffrent bootparameters and how do I add my own boot parameter? – nist Feb 23 '12 at 17:19
  • @nist - http://www.de-brauwer.be/wiki/wikka.php?wakka=HelloParam shows an example of the usual way. I think if you want to do it that early in boot up you'd need to use [__setup](http://lxr.linux.no/linux+*/include/linux/init.h#L231) though. – Flexo Feb 23 '12 at 17:29