Mohamad
Mohamad

Reputation: 37

How can I solve the error "initializer element is not constant" in C in Linux Kernel

I have written the following code in C in Linux in order to take the transpose of matrix whose dimensions are taken as command line argument. But when I am trying to make(compile) this code ,I am getting the following error:

error mesege

would some one tell me how to solve this error please ??

Thanks in advance

#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/stat.h>
#include <linux/proc_fs.h>
#include <linux/slab.h>

/*---------------------------------------------------------*/
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Taking the transpoze of matrix");
MODULE_AUTHOR("Magenta");

int Row = 1 ;
int Column = 2 ;

module_param(Row, int, 0000) ;
module_param(Column, int, 0000) ;

int *MemoryBlock = kmalloc(Column * Row * sizeof(int), GFP_KERNEL) ;

/*if(MemoryBlock == NULL)
{
  printk("Error!! memory not allocated") ;
  exit(1) ; //Exit Failure . 
}*/

static int __init hello_5_init(void)
{ 
 return 0 ;
}
static void __exit hello_5_exit(void)
{ 
}

module_init(hello_5_init);
module_exit(hello_5_exit);

Upvotes: 0

Views: 329

Answers (1)

dbush
dbush

Reputation: 223862

You're attempting to run code outside of a function. That is not allowed.

Move the allocation to the init function:

int *MemoryBlock = NULL;
...
static int __init hello_5_init(void)
{ 
 MemoryBlock  = kmalloc(Column * Row * sizeof(int), GFP_KERNEL) ;
 return 0 ;
}

Upvotes: 1

Related Questions