I am new to C and Linux. I am trying to compile the below code but it gives some fatal error while compiling. Any help on fixing this appreciated.
Here is the 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 is %llu clock cycles",(end - start));
}
I am trying to compile it this way:
gcc -c -O2 -W -Wall -isystem /lib/modules/'uname -r'/build/include -D_KERNEL_ -DMODULE measurecpu.c
I get this error:
measurecpu.c:1:32: fatal error: linux/module.h: No such file or directory
#include <linux/module.h>
^
compilation terminated.