i new c , linux. trying compile below code gives fatal error while compiling. on fixing appreciated.
here code measurecpu.c
:
#include <linux/module.h> #include <linux/kernel.h> #include <linux/init.h> #include <linux/hardirq.h> #include <linux/preempt.h> #include <linux/sched.h> #include<stdio.h> int main() { uint64_t start, end; int i=0; asm volatile ("cpuid \ n \ t" "rdtsc \ n \ t" "mov %%edx, %0 \ n \ t" "mov %%eax, %1 \ n \ t": "=r" (cycles_high), "=r" (cycles_low):: "%rax", "%rbx", "%rcx", "%rdx"); for(i=0; i<200000;i++) {} asm volatile ("rdtscp \ n \ t" "mov %%edx, %0 \ n \ t" "mov %%eax, %1 \ n \ t" "cpuid \ n \ t": "=r" (cycles_high1), "=r" (cycles_low1):: "%rax", "%rbx", "%rcx", "%rdx"); start = ( ((uint64_t)cycles_high << 32) | cycles_low ); end = ( ((uint64_t)cycles_high1 << 32) | cycles_low1 ); printk(kern_info " \ n function execution time %llu clock cycles",(end - start)); }
i trying compile way:
gcc -c -o2 -w -wall -isystem /lib/modules/'uname -r'/build/include -d_kernel_ -dmodule measurecpu.c
i error:
measurecpu.c:1:32: fatal error: linux/module.h: no such file or directory #include <linux/module.h> ^ compilation terminated.
i trying compile way gcc -c -o2 -w -wall -isystem /lib/modules/'uname -r'/build/include -d_kernel_ -dmodule measurecpu.c
usually way compile kernel module use kernel build system - ie use make
instead of gcc
directly. need create makefile
, specify object, line obj-m := measurecpu.o
in case. after in same directory, issue make
command, yield kernel object file measurecpu.ko
# if kernelrelease defined, we've been invoked # kernel build system , can use language. ifneq ($(kernelrelease),) obj-m := measurecpu.o # otherwise called directly command # line; invoke kernel build system. else kerneldir ?= /lib/modules/$(shell uname -r)/build pwd := $(shell pwd) default: $(make) -c $(kerneldir) m=$(pwd) modules clean: $(make) -c $(kerneldir) m=$(pwd) modules clean endif
note kernel module not user space program, cannot just run it. need tell kernel kernel module via insmod
, , check results via dmesg
.
Comments
Post a Comment