LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-07-2008, 02:28 AM   #1
cryincold
Member
 
Registered: Sep 2007
Location: Zhuhai, China
Distribution: Debian, etch
Posts: 40

Rep: Reputation: 15
Question How to let MAKE invoke gcc with "-std=c99"


This is my makefile.

Quote:
ifdef $(KERNELRELEASE)
obj-m:=hello.o
else
KERNELDIR:=/lib/modules/$(shell uname -r)/build
PWD:=$(shell pwd)
default:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
endif
My program was written in c99 style.
make complained that i should turn on c99 flag.
But i only know: "gcc -std=c99 ...."
How 2 modify the makefile to compile in c99 style.

Best regards .
tku in advance .
 
Old 03-07-2008, 07:04 AM   #2
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Can you post the warning or error? Normally you don't use options when compiling modules.
ta0kira
 
Old 03-07-2008, 10:37 AM   #3
nullmind
LQ Newbie
 
Registered: Mar 2008
Posts: 9

Rep: Reputation: 0
I usually get that error when doing:

Code:
for(int i=0; i < foo; i++){
// bar
}
I don't know the kernel make files well enough to give you an answer (the makefile fragment you posted doesn't make sense to me since it seems incomplete), but there most likely is a var like CC_FLAGS which is being passed to CC which is gcc.

Cheers,
Kris
 
Old 03-07-2008, 10:44 AM   #4
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
The makefile is pretty standard for 2.6.x modules. The reason they went to that is so module maintainers wouldn't have to write different makefiles for different kernel releases. The lines causing the warnings or errors are important to post because nearly every C99 warning can be fixed without using C99, especially since modules don't use libc functions.
ta0kira
 
Old 03-07-2008, 02:04 PM   #5
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
Quote:
Originally Posted by nullmind View Post
but there most likely is a var like CC_FLAGS which is being passed to CC which is gcc.
Yes—it’s called EXTRA_CFLAGS (you shouldn’t use CFLAGS directly since that is a global variable which affects compilation of the entire tree).
 
Old 03-07-2008, 08:09 PM   #6
cryincold
Member
 
Registered: Sep 2007
Location: Zhuhai, China
Distribution: Debian, etch
Posts: 40

Original Poster
Rep: Reputation: 15
Yes, as nullmind(#3) mentioned, my code includes

Quote:
for(int i=0; i < foo; i++){
// bar
}
Compiler complained:
Quote:
‘for’ loop initial declaration used outside C99 mode
I tried to add
Quote:
CFLAGS+=" -std=c99 "
or
Quote:
EXTRA_CFLAGS+=" -std=c99 "
None of them had any effects.

Frustrated..........
 
Old 03-08-2008, 12:14 PM   #7
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
Quote:
Originally Posted by cryincold View Post
None of them had any effects.
Can you give us the entire Makefile. Also, give us the output of
Code:
make V=1
 
Old 03-08-2008, 09:19 PM   #8
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Code:
int i;
for(i=0; i < foo; i++){
// bar
}
ta0kira

PS You need to export a variable such as EXTRA_CFLAGS as far as I know.

Last edited by ta0kira; 03-08-2008 at 09:20 PM.
 
Old 03-08-2008, 09:33 PM   #9
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
Quote:
Originally Posted by ta0kira View Post
PS You need to export a variable such as EXTRA_CFLAGS as far as I know.
Not if it’s a Makefile variable. You might do something like this for your Makfile:
Code:
ifndef KERNELDIR
        KERNELDIR  := /lib/modules/$(shell uname -r)/build
endif

obj-m := hello.o

EXTRA_CFLAGS := -std=c99

all:
	$(MAKE) -C $(KERNELDIR) M=$(PWD)

clean:
	$(MAKE) -C $(KERNELDIR) M=$(PWD) clean
 
Old 03-08-2008, 09:51 PM   #10
cryincold
Member
 
Registered: Sep 2007
Location: Zhuhai, China
Distribution: Debian, etch
Posts: 40

Original Poster
Rep: Reputation: 15
This is my Code(trivial)...
Quote:
#include <linux/init.h>
#include <linux/module.h>
#include <linux/vermagic.h>

MODULE_LICENSE("Dual BSD/GPL");

static __init int hello_init(void)
{
//int n = 0;
printk(KERN_ALERT"Hello,world\n");
//for(n=0; n<10; n++)
for( int n=0; n<10; n++)
;

return 0;
}

static void hello_exit(void)
{
printk(KERN_ALERT"Bye bye\n");
}

module_init(hello_init);
module_exit(hello_exit);

Last edited by cryincold; 03-08-2008 at 10:03 PM.
 
Old 03-08-2008, 10:08 PM   #11
cryincold
Member
 
Registered: Sep 2007
Location: Zhuhai, China
Distribution: Debian, etch
Posts: 40

Original Poster
Rep: Reputation: 15
Hi,
it still doesn't work. Though i added export EXTRA_CFLAGS += -std=c99

i tried the makefile osor(#9) showed above. Failed to work.

In my optimistic opinion:
Maybe in the near future, with version updating, this problem will be fixed automatically.
I'm not planning to spent any more time on this annoying issue any more.
That's not a very serious problem, i think, not worthy too much time.
Tku all very much.
Anymore, any advice that would help is still welcome. ^_^

Here is my makefile:
Quote:
#export EXTRA_CFLAGS+=" -std=c99 "
#export CFLAGS+=" -std=c99 "
ifdef $(KERNELRELEASE)
export EXTRA_CFLAGS := -std=c99
obj-m:=hello.o
else
KERNELDIR:=/lib/modules/$(shell uname -r)/build
PWD:=$(shell pwd)
default:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
endif
make output:
Quote:
$make
make -C /lib/modules/2.6.19/build M=/home/cryincold/tmp modules
make[1]: Entering directory `/usr/src/linux-2.6.19'
CC [M] /home/cryincold/tmp/hello.o
/home/cryincold/tmp/hello.c: In function ‘hello_init’:
/home/cryincold/tmp/hello.c:12: error: ‘for’ loop initial declaration used outside C99 mode
make[2]: *** [/home/cryincold/tmp/hello.o] Error 1
make[1]: *** [_module_/home/cryincold/tmp] Error 2
make[1]: Leaving directory `/usr/src/linux-2.6.19'
as i command: make v=1
Quote:
make v=1
$make v=1
make -C /lib/modules/2.6.18.5/build M=/home/cryincold/tmp modules
make[1]: Entering directory `/usr/src/linux-2.6.18.5'
CC [M] /home/cryincold/tmp/hello.o
/home/cryincold/tmp/hello.c: In function ‘hello_init’:
/home/cryincold/tmp/hello.c:12: error: ‘for’ loop initial declaration used outside C99 mode
make[2]: *** [/home/cryincold/tmp/hello.o] Error 1
make[1]: *** [_module_/home/cryincold/tmp] Error 2
make[1]: Leaving directory `/usr/src/linux-2.6.18.5'
make: *** [default] Error 2

Last edited by cryincold; 03-09-2008 at 12:07 AM.
 
Old 03-08-2008, 10:49 PM   #12
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Yes, please see my other post. You need to declare int i; outside of the for structure. That's what is causing the error and it's such a minor thing that you're better off changing it than trying to force C99 to work. Additionally, other things might stop working because the change in standard changes the macros that are defined, which sometimes changes the structure of the code.
ta0kira

PS From the make infopage:
Code:
To pass down, or "export", a variable, `make' adds the variable
and its value to the environment for running each command. The
sub-`make', in turn, uses the environment to initialize its table
of variable values. ...
Except by explicit request, `make' exports a variable only if it
is either defined in the environment initially or set on the
command line, and if its name consists only of letters, numbers,
and underscores. ...
If you want to export specific variables to a sub-`make', use the
`export' directive ...
 
Old 03-09-2008, 01:49 PM   #13
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
If you do
Code:
make V=1
(that’s a capital ‘V’) it will output the intermediate steps taken by the Makefile.

I tried a slightly modified version of the Makefile I provided in post 9 (I changed c99 to gnu99 since the kernel makes use of GNU C features) with the sourcecode you provided. It works as expected (I highlighted the command-line option “std=gnu99” in red, but you might have to scroll to see it):
Code:
$ cat hello.c
#include <linux/module.h>

MODULE_LICENSE("Dual BSD/GPL");

static __init int hello_init(void)
{
	//int n = 0;
	printk(KERN_ALERT"Hello,world\n");
	//for(n=0; n<10; n++)
	for( int n=0; n<10; n++)
		;

	return 0;
}

static void hello_exit(void)
{
	printk(KERN_ALERT"Bye bye\n");
}

module_init(hello_init);
module_exit(hello_exit);
$ cat Makefile
ifndef KERNELDIR
        KERNELDIR  := /lib/modules/$(shell uname -r)/build
endif

obj-m := hello.o

EXTRA_CFLAGS := -std=gnu99

all:
	$(MAKE) -C $(KERNELDIR) M=$(PWD)

clean:
	$(MAKE) -C $(KERNELDIR) M=$(PWD) clean
$ make V=1
make -C /lib/modules/2.6.22-14-generic/build M=/home/osor/src/hello
make[1]: Entering directory `/usr/src/linux-headers-2.6.22-14-generic'
test -e include/linux/autoconf.h -a -e include/config/auto.conf || (		\
	echo;								\
	echo "  ERROR: Kernel configuration is invalid.";		\
	echo "         include/linux/autoconf.h or include/config/auto.conf are missing.";	\
	echo "         Run 'make oldconfig && make prepare' on kernel src to fix it.";	\
	echo;								\
	/bin/false)
mkdir -p /home/osor/src/hello/.tmp_versions
rm -f /home/osor/src/hello/.tmp_versions/*
make -f scripts/Makefile.build obj=/home/osor/src/hello
   rm -f /home/osor/src/hello/built-in.o; ar rcs /home/osor/src/hello/built-in.o
  gcc -m32 -Wp,-MD,/home/osor/src/hello/.hello.o.d  -nostdinc -isystem /usr/lib/gcc/i486-linux-gnu/4.1.3/include -D__KERNEL__ -Iinclude  -include include/linux/autoconf.h -Wall -Wundef -Wstrict-prototypes -Wno-trigraphs -fno-strict-aliasing -fno-common -O2 -pipe -msoft-float -mregparm=3 -freg-struct-return -mpreferred-stack-boundary=2  -march=i586 -mtune=generic -ffreestanding -maccumulate-outgoing-args   -Iinclude/asm-i386/mach-default -fomit-frame-pointer -g  -fno-stack-protector -Wdeclaration-after-statement -Wno-pointer-sign -std=gnu99  -DMODULE -D"KBUILD_STR(s)=#s" -D"KBUILD_BASENAME=KBUILD_STR(hello)"  -D"KBUILD_MODNAME=KBUILD_STR(hello)" -c -o /home/osor/src/hello/.tmp_hello.o /home/osor/src/hello/hello.c
  Building modules, stage 2.
make -f /usr/src/linux-headers-2.6.22-14-generic/scripts/Makefile.modpost
  scripts/mod/modpost -m -a -i /usr/src/linux-headers-2.6.22-14-generic/Module.symvers -I /home/osor/src/hello/Module.symvers -o /home/osor/src/hello/Module.symvers -w
  gcc -m32 -Wp,-MD,/home/osor/src/hello/.hello.mod.o.d  -nostdinc -isystem /usr/lib/gcc/i486-linux-gnu/4.1.3/include -D__KERNEL__ -Iinclude  -include include/linux/autoconf.h -Wall -Wundef -Wstrict-prototypes -Wno-trigraphs -fno-strict-aliasing -fno-common -O2 -pipe -msoft-float -mregparm=3 -freg-struct-return -mpreferred-stack-boundary=2  -march=i586 -mtune=generic -ffreestanding -maccumulate-outgoing-args   -Iinclude/asm-i386/mach-default -fomit-frame-pointer -g  -fno-stack-protector -Wdeclaration-after-statement -Wno-pointer-sign    -D"KBUILD_STR(s)=#s" -D"KBUILD_BASENAME=KBUILD_STR(hello.mod)"  -D"KBUILD_MODNAME=KBUILD_STR(hello)" -DMODULE -c -o /home/osor/src/hello/hello.mod.o /home/osor/src/hello/hello.mod.c
  ld -m elf_i386 -m elf_i386 -r -o /home/osor/src/hello/hello.ko /home/osor/src/hello/hello.o /home/osor/src/hello/hello.mod.o
make[1]: Leaving directory `/usr/src/linux-headers-2.6.22-14-generic'
And it’s not likely a version problem, since I tried on 2.6.22 and 2.6.14. I think you (cryincold) have made a typo somewhere.
 
Old 03-09-2008, 08:40 PM   #14
cryincold
Member
 
Registered: Sep 2007
Location: Zhuhai, China
Distribution: Debian, etch
Posts: 40

Original Poster
Rep: Reputation: 15
Tku ta0kira,
Tku osor.

Unfortunately, it still doesn't work on my machine.
Ok, Just ignore it. i have no patience with this weird, troublesome but not very important problem now.

Tku all to devote your time to help me.

with best regards. :-)

Quote:
cryincold@wieldy:~/tmp$ cat hello.c
#include <linux/init.h>
#include <linux/module.h>
#include <linux/vermagic.h>

MODULE_LICENSE("Dual BSD/GPL");

static __init int hello_init(void)
{
// int n = 0;
printk(KERN_ALERT"Hello,world\n");
// for(n=0; n<10; n++)
for( int n=0; n<10; n++)
;

return 0;
}

static void hello_exit(void)
{
printk(KERN_ALERT"Bye bye\n");
}

module_init(hello_init);
module_exit(hello_exit);
cryincold@wieldy:~/tmp$ cat makefile
ifndef KERNELDIR
KERNELDIR := /lib/modules/$(shell uname -r)/build
endif

obj-m := hello.o

EXTRA_CFLAGS := -std=gnu99

all:
$(MAKE) -C $(KERNELDIR) M=$(PWD)

clean:
$(MAKE) -C $(KERNELDIR) M=$(PWD) clean
cryincold@wieldy:~/tmp$ make V=1
make -C /lib/modules/2.6.18.5/build M=/home/cryincold/tmp
make[1]: Entering directory `/usr/src/linux-2.6.18.5'
test -e include/linux/autoconf.h -a -e include/config/auto.conf || ( \
echo; \
echo " ERROR: Kernel configuration is invalid."; \
echo " include/linux/autoconf.h or include/config/auto.conf are missing."; \
echo " Run 'make oldconfig && make prepare' on kernel src to fix it."; \
echo; \
/bin/false)
mkdir -p /home/cryincold/tmp/.tmp_versions
rm -f /home/cryincold/tmp/.tmp_versions/*
make -f scripts/Makefile.build obj=/home/cryincold/tmp
gcc -m32 -Wp,-MD,/home/cryincold/tmp/.hello.o.d -nostdinc -isystem /usr/lib/gcc/i486-linux-gnu/4.1.2/include -D__KERNEL__ -Iinclude -include include/linux/autoconf.h -Wall -Wundef -Wstrict-prototypes -Wno-trigraphs -fno-strict-aliasing -fno-common -Os -pipe -msoft-float -mpreferred-stack-boundary=2 -march=i486 -mregparm=3 -ffreestanding -Iinclude/asm-i386/mach-default -fomit-frame-pointer -fno-stack-protector -Wdeclaration-after-statement -Wno-pointer-sign -DMODULE -D"KBUILD_STR(s)=#s" -D"KBUILD_BASENAME=KBUILD_STR(hello)" -D"KBUILD_MODNAME=KBUILD_STR(hello)" -c -o /home/cryincold/tmp/.tmp_hello.o /home/cryincold/tmp/hello.c
/home/cryincold/tmp/hello.c: In function ‘hello_init’:
/home/cryincold/tmp/hello.c:12: error: ‘for’ loop initial declaration used outside C99 mode
make[2]: *** [/home/cryincold/tmp/hello.o] Error 1
make[1]: *** [_module_/home/cryincold/tmp] Error 2
make[1]: Leaving directory `/usr/src/linux-2.6.18.5'
make: *** [all] Error 2
every time i post my code, all spaces and tab characters at the beginning of the lines disappear.
 
Old 03-09-2008, 09:05 PM   #15
cryincold
Member
 
Registered: Sep 2007
Location: Zhuhai, China
Distribution: Debian, etch
Posts: 40

Original Poster
Rep: Reputation: 15
Hi ~~~

It has been fixed !!! ^_^

i missed the key word: export

it should be :
Quote:
export EXTRA_CFLAGS := -std=gnu99
Hehe ....
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
doubt with "make check" of gcc in chapter 6 lfs_rocks Linux From Scratch 1 02-13-2008 12:15 AM
problem running "make bootstrap" on GCC Zaneyard Linux From Scratch 1 01-28-2008 09:28 PM
how can I invoke "awk" from shell to do floating point math? Joseph Schiller Programming 8 01-12-2006 05:00 AM
Constant errors during "make" or "make install" with SUSE 10.1 Alpha 4 TehFlyingDutchman Linux - Software 3 12-30-2005 06:25 PM
cgi-bin: "attempt to invoke directory as script" hamish Linux - Software 0 12-09-2004 12:45 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:34 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration