Exyaristo gia to module

Giorgos Keramidas keramida at ceid.upatras.gr
Thu Apr 17 10:12:01 EEST 2003


On 2003-04-17 08:28, Aleksandros Paleos <apalaios at cs.teiath.gr> wrote:
> Alla peste mou sto praktiko kommati...ti tha prepei na kano gia na
> doulepsei telika to module mou?

Prepei na kaneis ta ejhs:

  a) Na min grafeis C kwdika me auto to stul.

  b) Na min kaneis compile me to xeri, alla na matheis na paizeis me
     Makefiles.

Perissotera sxolia parakato:

  : #define MODULE
  : #include <linux/module.h>
  :
  : int init_module(void)
  :                 {
  :                 printk("<1>Hello world\n");return 0;
  :                 }
  : void cleanup_module(void)
  :                 {
  :                 printk("<1>Goodbye cruel world\n");
  :                 }
  :

auto olo mporei na graftei ki etsi:

  : #define MODULE
  : #include <linux/module.h>
  :
  : int init_module(void)
  : {
  :         printk("<1>Hello kernel world.\n");
  :         return 0;
  : }
  :
  : int
  : cleanup_module(void)
  : {
  :         printk("<1>Goodbye kernel world.\n");
  :         return 0;
  : }

Pistepse me, den einai katholou ashmanto to styl pou grafeis.  Gia tou
logou to alithes, ti einai pio eukolo na diabaseis kai na katalabeis ti
kanei?  Auto...

  : #include <getopt.h>
  :
  : int vflag=0;int main(int argc,char*argv[]){int c;while((c=/*
  : */getopt(argc,argv,"s:v"))!=-1)switch(c){case's':blah(optarg
  : );break;case'v':vflag=1;break;}if(optind<argc){argc-=optind;
  : argv+=optind;}if(argc>1)usage();if(argc==1)blah(argv[0]);/*1
  : void usage(void){printf("!!!\n");return 0;}*/return(foo());}

h auto? ...

  : #include <getopt.h>
  : #include <stdio.h>
  : #include <stdlib.h>
  :
  : int vflag = 0;                          /* Verbose if set. */
  :
  : static void usage(void);
  :
  : extern int blah(const char *);
  : extern int foo(void);
  :
  : int main(int argc, char *argv[])
  : {
  :         int c;
  :
  :         while ((c = getopt(argc, argv, "")) != -1)
  :                 switch (c) {
  :                 case 's':
  :                         blah(optarg);
  :                         break;
  :                 case 'v':
  :                         vflag = 1;
  :                         break;
  :                 }
  :         /* Ignore args parsed by getopt already.  */
  :         if (optind < argc) {
  :                 argc -= optind;
  :                 argv += optind;
  :         }
  :         if (argc > 1)
  :                 usage();
  :         if (argc == 1)
  :                 blah(argv[0]);
  :
  :         if (foo() != 0)
  :                 return (EXIT_FAILURE);
  :         else
  :                 return (EXIT_SUCCESS);
  : }
  :
  : #if 0
  : void usage(void)
  : {
  :         fprintf(stderr, "usage: prog [-s foo] [-v]\n");
  :         exit(EXIT_FAILURE);
  : }
  : #endif

Einai kapos uperboliko to paradeigma, alla an thes na deis POSO pio
periergos mporei na ginei o kwdikas se C, psakse gia ta paradeigmata
tou IOCCC (International Obfuscated C Code Contest).

> sti synexeia kano gcc -c hello.c

Giati kaneis compile me to xeri?

Ftiakse ena Makefile na kanei th douleia gia sena.  Tha sou proteina
kati san auto:

	$ cat Makefile

	CFLAGS=		-Wall -W -Wall -Wbad-function-cast -Wcast-align \
	  -Wcast-qual -Wchar-subscripts -Winline -Wmissing-prototypes \
	  -Wnested-externs -Wpointer-arith -Wredundant-decls -Wshadow \
	  -Wstrict-prototypes -Wwrite-strings

	CFLAGS_KMOD=	-D__KERNEL__ -DMODULE -O

	ifeq ($(CC),)
	CC=	gcc
	endif

	MOD_NAME=	hello

	all:	$(MOD_NAME).o

	$(MOD_NAME).o:	$(MOD_NAME).c
		$(CC) $(CFLAGS) $(CFLAGS_KMOD) -c $(MOD_NAME).c


Etsi tha einai polu aplo na kaneis compile, grafontas:

	$ make

> insmod ./hello.o
> pairno to parakato minima lathous....ti prepei na kano?
>
> ./hello.o: kernel-module version mismatch
>         ./hello.o was compiled for kernel version 2.4.19
>         while this kernel is version 2.4.19-4GB.

Kaneis compile me lathos headers.  Sou exei pei hdh kapoios pos na to
ftiakseis auto.  Diabase ta prohgoumena mail.

> to uname deixnei
> linux:/home/alaios/Documents/leitourgika # uname -r
> 2.4.19-4GB
>
> Gia ta alla 2 pou rotises opos blepeis einai directories
> linux:/usr/include # file linux
> linux: directory
> linux:/usr/include # file asm
> asm: directory

Kai den prepei na einai.  Kai sou eipe se mia apanthsh pos na to
ftiakseis.  Opote, diorthose to.

- Giorgos




More information about the Linux-greek-users mailing list