HELP: Clang options needed in command to compile device driver into linux module.

Hello,

What option should be added in the clang command to compile linux device driver(from .c to .bc)?
Typically we need a makefile to compile a module. But I just want a simple command line.

I tried "clang -O3 -emit-llvm hello.c -c -o hello.bc " but errors occurred saying
fatal error: ‘linux/init.h’ file not found
#include <linux/init.h>
^
1 error generated
Then I was taught to add -D or -I option so that clang can find the linux kernel head file. But I still dont know
how exactly the command like…All I want to know is a clang command line that can compile a helloworldmodule.c into helloworldmodule.bc.

Please give me some suggestion.
Any help would be appreciated!

15102925731 wrote:

Hello£¬

What option should be added in the clang command to compile linux device
driver£¨from .c to .bc£©?
Typically we need a makefile to compile a module. But I just want a
simple command line.

Make, using the information in the Makefile, runs gcc or clang with a long command line. That command line tells gcc/clang how to do things, like where to find linux/init.h or how certain macros are to be defined.

It doesn't matter whether you run clang from the makefile or not, but you will need the correct command line for it to work. What command line does make run gcc with?

Nick

Hi, Nick
I think that’s the question. I just don’t know what’s the command line that can tell clang to find the linux head file like a makefile will do. Please help me!

15102925731 wrote:

Hi, Nick
I think that's the question. I just don't know what's the command line
that can tell clang to find the linux head file like a makefile will do.
Please help me!

In the linux kernel, "make V=1" will compile verbosely, printing out each command it runs. Each .o file produced will have its own command-line (many of them will be the same, but you have to make sure you use the right one).

Nick

Wow that’s to much output information.

Maybe I can put it in this way. Could you tell me the clang option that can include the linux kernel headers???
Something like
"clang -O3 -isystem /lib/modules/uname -r/build/include -emit-llvm hellomodule.c -c -o hellomodule.bc"
Thank you!

BTW, the Makefile is as folllow,

obj-m := hellomodule.o
KERNELDIR := /lib/modules/uname -r/build
PWD := $(shell pwd)
modules:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules

Can I use an option of clang to specify the KERNELDIR ,etc?
Thank you!

^^^^^^^^^^^^^^

  Check the Makefile under KERNELDIR directory.

Regards,
chenwj

Sorry, but I didn’t find a Makefile under KERNELDIR.
Why is it necessary to check that makefile anyway???

As to the Makefile that I posted before, if I can changed that file to build the .c into .bc rather then .o, that would also solve my problem!
Thank you!

Sorry, but I didn't find a Makefile under KERNELDIR.
Why is it necessary to check that makefile anyway???

  Well, `make -C $DIR` means it will change to that directory and start
to make. That's why I sugguest you looking for Makefile under KERNELDIR.
Where do you download the kernel source?

Regards,
chenwj