From Bugzilla Helper: User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0) Description of problem: When I compile simple module device driver programs in C with cc -D__KERNEL__ - DMODULE option, the cc always return erras in line 60, 62, and 91 in module.h at least, then reject to go on; I tried three simple sample programs from books <<Linux Device Driver>>, <<Embedded Linux>>, and <Practical Linux Programming>> respectively, I get the same returning messages. Version-Release number of selected component (if applicable): How reproducible: Always Steps to Reproduce: 1.copy one of the following three set of module program and its makefile into your i386-PC: 1.1 from <<Practical Linux Programming>> 1.1.1 makefile: INCLUDEDIR = /usr/include DEBFLAGS = -O2 CFLAGS = -D__KERNEL__ -DMODULE -Wall $(DEBFLAGS) CFLAGS += -I$(INCLUDEDIR) OBJS = pport.o all: $(OBJS) 1.1.2 pport.c: #include <linux/module.h> #include <linux/kernel.h> #include <linux/fs.h> /* everything... */ int major = 0; struct file_operations fops = { NULL, /* lseek */ NULL, /* read */ NULL, /* write */ NULL, /* readdir */ NULL, /* poll */ NULL, /* ioctl */ NULL, /* mmap */ NULL, /* open */ NULL, /* flush */ NULL, /* release */ NULL, /* fsync */ NULL, /* fasync */ NULL, /* check_media_change */ NULL, /* revalidate */ NULL, /* lock */ }; int init_module(void) { major = register_chrdev(0, "pport", &fops); return 0; } void cleanup_module(void) { unregister_chrdev(major, "pport"); } 1.2 from <<Linux Device Drivers>>@2001 website: examples: faulty.c and Makefile in misc-module folder: 1.2.1 Makefile: # Comment/uncomment the following line to enable/disable debugging #DEBUG = y # Change it here or specify it on the "make" commandline INCLUDEDIR = /usr/include ifeq ($(DEBUG),y) DEBFLAGS = -O -g -DJIT_DEBUG -DJIQ_DEBUG -DALL_DEBUG else DEBFLAGS = -O2 endif CFLAGS = -D__KERNEL__ -DMODULE -Wall $(DEBFLAGS) CFLAGS += -I$(INCLUDEDIR) # first the portable ones, then those that need newer kernel versions # this way everything compilable is built under 1.2 before getting errors OBJS = faulty.o sleepy.o silly.o slave.o slaveD.o slaveH.o import.o \ master.o export.o hello.o \ jit.o jiq.o all: $(OBJS) clean: rm -f *.o *.ver *~ # Don't use -Wall here: the cose is silly by design hello.o: hello.c $(CC) -D__KERNEL__ -c $^ -o $@ # set MODVERSIONS if the kernel uses it VERSUSED = $(shell grep 'define CONFIG_MODVERSIONS' \ $(INCLUDEDIR)/linux/autoconf.h | wc -l | sed 's/ //g') ifeq ($(VERSUSED),1) MODVERSIONS = y endif # and then use it ifdef MODVERSIONS export.o import.o: export.ver endif export.ver: export.c $(CC) -I$(INCLUDEDIR) -E -D__GENKSYMS__ $^ | genksyms > $@ 1.2.3 faulty.c: /* * faulty.c -- a module which generates an oopswhen read * * Tested with 1.2 on the x86 * Tested with 2.0 on the x86, Sparc * */ #ifndef __KERNEL__ # define __KERNEL__ #endif #ifndef MODULE # define MODULE #endif #define __NO_VERSION__ /* don't define kernel_verion in module.h */ #include <linux/module.h> #include <linux/version.h> char kernel_version [] = UTS_RELEASE; #include <linux/kernel.h> /* printk() */ #include <linux/fs.h> /* everything... */ #include <linux/types.h> /* size_t */ #include <asm/segment.h> #include "sysdep.h" /* count_t for portability 2.0/2.1 */ int faulty_major=0; char faulty_buf[1024]; read_write_t faulty_read (struct inode *inode, struct file *filp, char *buf, count_t count) { printk(KERN_DEBUG "read: inode %p, file %p, buf %p, count %li\n", inode, filp, buf, (long)count); memcpy_tofs(buf,faulty_buf,count); return count; } struct pio {int a; char b; long c;}; read_write_t faulty_write (struct inode *inode, struct file *filp, const char *buf, count_t count) { /* put_user(0,(struct pio *)buf);*/ return 0; } struct file_operations faulty_fops = { NULL, /* lseek */ faulty_read, faulty_write, /* nothing more, fill with NULLs */ }; int init_module(void) { int result; /* * Register your major, and accept a dynamic number */ result = register_chrdev(faulty_major, "faulty", &faulty_fops); if (result < 0) return result; if (faulty_major == 0) faulty_major = result; /* dynamic */ /* * allocate the devices -- we can't have them static, as the number * can be specified at load time */ return 0; } void cleanup_module(void) { unregister_chrdev(faulty_major, "faulty"); } 1.3 from <<Embedded Linux>>@2002 website: www.embeddedlinuxinterfacing.com/chapters/7, sample: hello_proc_module.c and cc -D__KERNEL__ -DMODULE ... 2.run command "make" where the copied programs are 3.You will see returned messages like below in section Actual results: Actual Results: stop compiling and return erra message as below: [martinwm@mdoggy minimum:476]$ ls Makefile pport.c [martinwm@mdoggy minimum:477]$ vi pport.c [martinwm@mdoggy minimum:478]$ make cc -D__KERNEL__ -DMODULE -Wall -O2 -I/usr/include -c -o pport.o pport.c In file included from /usr/include/linux/fs.h:23, from pport.c:3: /usr/include/linux/string.h:8:2: warning: #warning Using kernel header in userland! In file included from pport.c:1: /usr/include/linux/module.h:60: parse error before `atomic_t' /usr/include/linux/module.h:60: warning: no semicolon at end of struct or union /usr/include/linux/module.h:60: warning: no semicolon at end of struct or union /usr/include/linux/module.h:62: parse error before `}' /usr/include/linux/module.h:62: warning: type defaults to `int' in declaration of `uc' /usr/include/linux/module.h:62: warning: data definition has no type or storage class /usr/include/linux/module.h:91: parse error before `}' pport.c:7: variable `fops' has initializer but incomplete type pport.c:8: warning: excess elements in struct initializer pport.c:8: warning: (near initialization for `fops') pport.c:9: warning: excess elements in struct initializer pport.c:9: warning: (near initialization for `fops') pport.c:10: warning: excess elements in struct initializer pport.c:10: warning: (near initialization for `fops') pport.c:11: warning: excess elements in struct initializer pport.c:11: warning: (near initialization for `fops') pport.c:12: warning: excess elements in struct initializer pport.c:12: warning: (near initialization for `fops') pport.c:13: warning: excess elements in struct initializer pport.c:13: warning: (near initialization for `fops') pport.c:14: warning: excess elements in struct initializer pport.c:14: warning: (near initialization for `fops') pport.c:15: warning: excess elements in struct initializer pport.c:15: warning: (near initialization for `fops') pport.c:16: warning: excess elements in struct initializer pport.c:16: warning: (near initialization for `fops') pport.c:17: warning: excess elements in struct initializer pport.c:17: warning: (near initialization for `fops') pport.c:18: warning: excess elements in struct initializer pport.c:18: warning: (near initialization for `fops') pport.c:19: warning: excess elements in struct initializer pport.c:19: warning: (near initialization for `fops') pport.c:20: warning: excess elements in struct initializer pport.c:20: warning: (near initialization for `fops') pport.c:21: warning: excess elements in struct initializer pport.c:21: warning: (near initialization for `fops') pport.c:22: warning: excess elements in struct initializer pport.c:22: warning: (near initialization for `fops') pport.c: In function `init_module': pport.c:28: warning: implicit declaration of function `register_chrdev' pport.c: In function `cleanup_module': pport.c:34: warning: implicit declaration of function `unregister_chrdev' make: *** [pport.o] Error 1 [martinwm@mdoggy minimum:479]$ ls Makefile pport.c [martinwm@mdoggy minimum:480]$ Expected Results: pass through the "make" cc compilation and get pport.o. Additional info: Makefiles without option -D__KERNEL__ -DMODULE for a non-module/driver /kernel C program are ok, now.
*** This bug has been marked as a duplicate of 76569 ***