QQ登录

只需一步,快速开始

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 1214|回复: 14

我在linux下printk这个函数编译不了,望大侠帮忙

[复制链接]
发表于 2003-4-11 21:22:29 | 显示全部楼层 |阅读模式
我写了一个简单的内核调用程序,就是简单的那种hello world 函数,
但是编译石提示printk该函数未定义,有警告,

用insmod调用时提示gcc的编译版本与我的kernel的版本不同,

请问如何解决,多谢!
发表于 2003-4-13 02:26:54 | 显示全部楼层
post u code , pls
回复

使用道具 举报

 楼主| 发表于 2003-4-16 10:09:13 | 显示全部楼层
我的代码
#include <linux/kernel.h>
#include <linux/module.h>

#if CONFIG_MODVERSIONS == 1
#define MODVERSIONS
#include <linux/modversions.h>
#endif

int init_modules()
{
        printk(" this is demo \n");
       
}

void cleanup_module( )
{
    printk("demo exit kernel \n");       
}

我的makefile为
CC =  gcc
MODCFLAGS:= -Wall -DMODULE -D__KERNEL__ -DLINUX

hello.o : hello.c /usr/include/linux/version.h
        $(CC) $(MODCFLAGS) -c hello.c

我编译时会出现如下warning:
warning: implicit declaration of function 'printk'.
运行 insmod hello.o 时,没有执行printk这个函数,而且会显示我的
gcc的版本是为我的linux大的内核版本的低版本服务的而不是为我的当前的linux的内核版本
回复

使用道具 举报

发表于 2003-4-16 10:49:46 | 显示全部楼层
i modify u code, the basic problem is slab.h need to be included. under is my code.
==============hello.c=================
#include <linux/module.h>
#include <linux/slab.h>

int hello_init(void)
{
        printk("module init\n");
        return 0;
}

void hello_clean(void)
{
        printk("module cleanup\n");
}

module_init(hello_init);
module_exit(hello_clean);

MODULE_LICENSE("GPL");
=============end of hello.c ===========
===========makefile===========
CC = gcc

CFLAGS:= -O2 -Wall -D__KERNEL__ -DMODULE
INCLUDEDIR = /usr/src/linux/include

CFLAGS += -I$(INCLUDEDIR)

TARGET = hello
OBJS = $(TARGET).o
SRC = $(TARGET).c

all: $(TARGET).o

clean:
        rm -f *.o *~ core
回复

使用道具 举报

 楼主| 发表于 2003-4-16 12:16:36 | 显示全部楼层
complie ,appear  these warning , how to resolve these warning?
[code:1]
gcc -O2 -Wall -D__KERNEL__ -DMODULE -I/usr/src/linux/include   -c -o hello.o hello.c
hello.c: In function `hello_init':
hello.c:6: warning: implicit declaration of function `printk'
hello.c: At top level:
hello.c:15: warning: type defaults to `int' in declaration of `module_init'
hello.c:15: warning: parameter names (without types) in function declaration
hello.c:15: warning: data definition has no type or storage class
hello.c:16: warning: type defaults to `int' in declaration of `module_exit'
hello.c:16: warning: parameter names (without types) in function declaration
hello.c:16: warning: data definition has no type or storage class
[/code:1]
回复

使用道具 举报

 楼主| 发表于 2003-4-16 14:13:01 | 显示全部楼层
cheungming,
我按照你的代码编译,出现以下信息
[code:1]
gcc -O2 -Wall -D__KERNEL__ -DMODULE -I/usr/src/linux/include   -c -o hello.o hello.c
hello.c: In function `hello_init':
hello.c:6: warning: implicit declaration of function `printk'
hello.c: At top level:
hello.c:15: warning: type defaults to `int' in declaration of `module_init'
hello.c:15: warning: parameter names (without types) in function declaration
hello.c:15: warning: data definition has no type or storage class
hello.c:16: warning: type defaults to `int' in declaration of `module_exit'
hello.c:16: warning: parameter names (without types) in function declaration
hello.c:16: warning: data definition has no type or storage class
[/code:1]
回复

使用道具 举报

发表于 2003-4-16 21:41:53 | 显示全部楼层
if i commnet that '#include <linux/slab.h>' then i have same error with u. but if i have that, i can compile the kernel.
which version of u gcc and kernel.
my kernel is 2.4.20, my gcc -v info are as followed
----------------
Reading specs from /usr/lib/gcc-lib/i686-pc-linux-gnu/3.2.1/specs
Configured with: /var/tmp/portage/gcc-3.2.1-r6/work/gcc-3.2.1/configure --prefix=/usr --bindir=/usr/i686-pc-linux-gnu/gcc-bin/3.2 --includedir=/usr/lib/gcc-lib/i686-pc-linux-gnu/3.2.1/include --datadir=/usr/share/gcc-data/i686-pc-linux-gnu/3.2 --mandir=/usr/share/gcc-data/i686-pc-linux-gnu/3.2/man --infodir=/usr/share/gcc-data/i686-pc-linux-gnu/3.2/info --enable-shared --host=i686-pc-linux-gnu --target=i686-pc-linux-gnu --with-system-zlib --enable-languages=c,c++,ada,f77,objc,java --enable-threads=posix --enable-long-long --disable-checking --enable-cstdio=stdio --enable-clocale=generic --enable-__cxa_atexit --enable-version-specific-runtime-libs --with-gxx-include-dir=/usr/lib/gcc-lib/i686-pc-linux-gnu/3.2.1/include/g++-v3 --with-local-prefix=/usr/local --enable-shared --enable-nls --without-included-gettext
Thread model: posix
gcc version 3.2.1 20021207 (Gentoo Linux 3.2.1-20021207)
回复

使用道具 举报

 楼主| 发表于 2003-4-17 09:21:02 | 显示全部楼层
cheungming,
my use redhat9.0 ,my kernel is 2.4.20-8, my gcc -v info are as followed
[code]
Reading specs from /usr/lib/gcc-lib/i386-redhat-linux/3.2.2/specs
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --enable-shared --enable-threads=posix --disable-checking --with-system-zlib --enable-__cxa_atexit --host=i386-redhat-linux
Thread model: posix
gcc version 3.2.2 20030222 (Red Hat Linux 3.2.2-5)
[code/]
what i do? help !!
回复

使用道具 举报

发表于 2003-4-17 09:29:38 | 显示全部楼层
where u install u linux kenrel? do u have /usr/src/linux? default installation only have /usr/src/linux-2.4

make sure u kernel header include directory point to right one.
回复

使用道具 举报

 楼主| 发表于 2003-4-17 10:06:20 | 显示全部楼层
cheungming,
redhat9.0. i  install   redhat9.0 is default install . i have /usr/src/linux2.4.20-8,/usr/src/linux-2.4  link to /usr/src/linux2.4.20-8.
what can i do ? help !!!
回复

使用道具 举报

发表于 2003-4-17 10:09:25 | 显示全部楼层
so u do not have /usr/src/linux, right?
make a symbol link as ln -s /usr/src/linux-2.4.20-8 /usr/src/linux
then try again.
回复

使用道具 举报

 楼主| 发表于 2003-4-17 10:53:59 | 显示全部楼层
cheungming,

thank you!
compile is no warning and error .
but when  i "insmod  hello.o" , no print  on the screen,why?
u code have "  printk("module init\n"); ",  help !!!
回复

使用道具 举报

发表于 2003-4-17 21:35:37 | 显示全部楼层
which loglevel u use? can u use dmesg to print all message.
dmesg -n x can set different level x.
man dmesg for help.

kernel module is not so easy to build, u had better read that ldd book carefully.
回复

使用道具 举报

 楼主| 发表于 2003-4-22 16:25:26 | 显示全部楼层
cheungming,
thanks! i find  the print  is in  the /var/messages.
how to make print on the screen?
回复

使用道具 举报

发表于 2003-4-22 22:00:46 | 显示全部楼层
read 'man dmesg' carefully to see how to set log level. u can use 'dmesg -n7' to show all message.
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

GMT+8, 2024-11-16 02:56 , Processed in 0.077969 second(s), 16 queries .

© 2021 Powered by Discuz! X3.5.

快速回复 返回顶部 返回列表