QQ登录

只需一步,快速开始

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 795|回复: 8

一个菜问题

[复制链接]
发表于 2005-1-28 18:34:52 | 显示全部楼层 |阅读模式
我学习我制作一个LKMs
/* hello.c*/

#define MODULE

int init_module(void)
{

    prink("hello\n");
    return 0;
}

void cleanup_module(void)
{
    printk("clean hello\n");
}

#gcc -c -O3 hello.c
#insmod hello.o
insmod: error inserting 'hello.o': -1 Invalid module format
为什么阿?
发表于 2005-1-28 19:05:58 | 显示全部楼层
模块不是这样编译的
回复

使用道具 举报

 楼主| 发表于 2005-1-28 19:51:15 | 显示全部楼层
斑竹,说话讲全啊!呵呵!
回复

使用道具 举报

发表于 2005-1-29 02:12:28 | 显示全部楼层
gcc -DMODULE -D__KERNEL__ -c hello.c -o hello.o
加上头文件
#include <linux/kernel.h>
#include <linux/module.h>
在hello.c最后加上
MODULE_LICENSE("GPL");//非必须
另外把/usr/include/linux/version.h里的版本号改成跟内核版本一样,不改也可以,用insmod -f hello.o
回复

使用道具 举报

 楼主| 发表于 2005-1-31 09:40:35 | 显示全部楼层
#define MODULE

#include <linux/kernel.h>
#include <linux/module.h>

int init_module(void)
{
        printk("<1> hello\n");
        return 0;
}

void cleanup_module(void)
{
        printk("<1> clean hello");
}

MODULE_LICENSE("GPL");
------------------------------------------------------------------------------------------
#gcc -c hello.c -o hello.o // 成功
#gcc -DMODULE -c hello.c -o hello.o //
hello.c:1:1: warning: "MODULE" redefined
<command line>:1:1: warning: this is the location of the previous definition
#insmod -f hello.o
insmod: error inserting 'hello.o': -1 Invalid module format
#gcc -DMODULE -D__KERNEL__ -c hello.c -o hello.o
hello.c:1:1: warning: "MODULE" redefined
<command line>:1:1: warning: this is the location of the previous definition
In file included from /usr/include/linux/module.h:10,
                 from hello.c:4:
/usr/include/linux/config.h:5:2: #error Incorrectly using glibc headers for a kernel module
回复

使用道具 举报

发表于 2005-1-31 10:35:39 | 显示全部楼层
模块编程是个非常复杂的话题,不仔细学一遍是不可能会的。

http://www.tldp.org/LDP/lkmpg/
回复

使用道具 举报

发表于 2005-1-31 12:15:28 | 显示全部楼层
gcc -DMODULE -D__KERNEL__ -c hello.c -o hello.o -I/usr/src/linux-xxxx/include
回复

使用道具 举报

 楼主| 发表于 2005-2-1 08:24:57 | 显示全部楼层
还是不行,搞不懂了!
错误一大堆!以下是部分错误

我的OS: RHEL4AS(内核2.6.9)
-----------------------------------------------------------------------------------
from hello.c:4:
/usr/src/linux/include/linux/topology.h: In function `__next_node_with_cpus':
/usr/src/linux/include/linux/topology.h:50: error: incompatible types in assignment
In file included from /usr/src/linux/include/linux/sched.h:31,
                 from /usr/src/linux/include/linux/module.h:10,
                 from hello.c:4:
/usr/src/linux/include/linux/percpu.h:7:24: asm/percpu.h: No such file or directory
In file included from /usr/src/linux/include/linux/module.h:10,
                 from hello.c:4:
/usr/src/linux/include/linux/sched.h: At top level:
/usr/src/linux/include/linux/sched.h:95: error: syntax error before "process_counts"
/usr/src/linux/include/linux/sched.h:95: warning: data definition has no type or storage class
In file included from /usr/src/linux/include/linux/module.h:10,
                 from hello.c:4:
/usr/src/linux/include/linux/sched.h:223: error: syntax error before "pgd_t"
/usr/src/linux/include/linux/sched.h:223: warning: no semicolon at end of struct or union
/usr/src/linux/include/linux/sched.h:236: error: 'brk' redeclared as different kind of symbol
/usr/include/unistd.h:932: error: previous declaration of 'brk' was here
/usr/src/linux/include/linux/sched.h:236: error: 'brk' redeclared as different kind of symbol
/usr/include/unistd.h:932: error: previous declaration of 'brk' was here
/usr/src/linux/include/linux/sched.h:243: error: syntax error before ':' token
/usr/src/linux/include/linux/sched.h:262: error: syntax error before '}' token
/usr/src/linux/include/linux/sched.h:268: error: `_NSIG' undeclared here (not in a function)
/usr/src/linux/include/linux/sched.h:286: error: field `shared_pending' has incomplete type
/usr/src/linux/include/linux/sched.h:377: error: syntax error before "clockid_t"
/usr/src/linux/include/linux/sched.h:377: warning: no semicolon at end of struct or union
/usr/src/linux/include/linux/sched.h:378: warning: data definition has no type or storage class
/usr/src/linux/include/linux/sched.h:391: error: syntax error before '}' token
/usr/src/linux/include/linux/sched.h:551: error: syntax error before "sigset_t"
/usr/src/linux/include/linux/sched.h:551: warning: no semicolon at end of struct or union
/usr/src/linux/include/linux/sched.h:558: error: syntax error before '*' token
/usr/src/linux/include/linux/sched.h:558: warning: data definition has no type or storage class
/usr/src/linux/include/linux/sched.h:569: error: syntax error before "parent_exec_id"
/usr/src/linux/include/linux/sched.h:569: warning: data definition has no type or storage class
/usr/src/linux/include/linux/sched.h:570: error: syntax error before "self_exec_id"
/usr/src/linux/include/linux/sched.h:570: warning: data definition has no type or storage class
/usr/src/linux/include/linux/sched.h:602: error: syntax error before '}' token
/usr/src/linux/include/linux/sched.h: In function `process_group':
/usr/src/linux/include/linux/sched.h:606: error: dereferencing pointer to incomplete type
/usr/src/linux/include/linux/sched.h: At top level:
/usr/src/linux/include/linux/sched.h:675: error: field `thread_info' has incomplete type
In file included from /usr/src/linux/include/linux/module.h:10,
                 from hello.c:4:
/usr/src/linux/include/linux/sched.h:709:25: asm/current.h: No such file or directory
/usr/src/linux/include/linux/sched.h:733: error: syntax error before "sigset_t"
/usr/src/linux/include/linux/sched.h:735: error: syntax error before "sigset_t"
/usr/src/linux/include/linux/sched.h: In function `dequeue_signal_lock':
/usr/src/linux/include/linux/sched.h:740: error: `tsk' undeclared (first use in this function)
/usr/src/linux/include/linux/sched.h:741: error: `mask' undeclared (first use in this function)
/usr/src/linux/include/linux/sched.h:741: error: `info' undeclared (first use in this function)
/usr/src/linux/include/linux/sched.h: At top level:
/usr/src/linux/include/linux/sched.h:748: error: syntax error before "sigset_t"
/usr/src/linux/include/linux/sched.h: In function `on_sig_stack':
/usr/src/linux/include/linux/sched.h:782: error: `current' undeclared (first use in this function)
/usr/src/linux/include/linux/sched.h: In function `sas_ss_flags':
/usr/src/linux/include/linux/sched.h:787: error: `current' undeclared (first use in this function)
/usr/src/linux/include/linux/sched.h: In function `mmdrop':
/usr/src/linux/include/linux/sched.h:815: error: dereferencing pointer to incomplete type
/usr/src/linux/include/linux/sched.h: In function `thread_group_empty':
/usr/src/linux/include/linux/sched.h:896: error: dereferencing pointer to incomplete type
/usr/src/linux/include/linux/sched.h: In function `task_lock':
/usr/src/linux/include/linux/sched.h:914: error: dereferencing pointer to incomplete type
/usr/src/linux/include/linux/sched.h:914: error: dereferencing pointer to incomplete type
/usr/src/linux/include/linux/sched.h:914: error: dereferencing pointer to incomplete type
/usr/src/linux/include/linux/sched.h:914: error: dereferencing pointer to incomplete type
/usr/src/linux/include/linux/sched.h:914: error: dereferencing pointer to incomplete type
/usr/src/linux/include/linux/sched.h:914: error: dereferencing pointer to incomplete type
/usr/src/linux/include/linux/sched.h:914: error: dereferencing pointer to incomplete type
/usr/src/linux/include/linux/sched.h:914: error: dereferencing pointer to incomplete type
/usr/src/linux/include/linux/sched.h:914: error: dereferencing pointer to incomplete type
/usr/src/linux/include/linux/sched.h:914: error: dereferencing pointer to incomplete type
/usr/src/linux/include/linux/sched.h:914: error: dereferencing pointer to incomplete type
/usr/src/linux/include/linux/sched.h:914: error: dereferencing pointer to incomplete type
/usr/src/linux/include/linux/sched.h: In function `task_unlock':
/usr/src/linux/include/linux/sched.h:919: error: dereferencing pointer to incomplete type
/usr/src/linux/include/linux/sched.h:919: error: dereferencing pointer to incomplete type
/usr/src/linux/include/linux/sched.h:919: error: dereferencing pointer to incomplete type
/usr/src/linux/include/linux/sched.h:919: error: dereferencing pointer to incomplete type
/usr/src/linux/include/linux/sched.h:919: error: dereferencing pointer to incomplete type
/usr/src/linux/include/linux/sched.h:919: error: dereferencing pointer to incomplete type
/usr/src/linux/include/linux/sched.h:919: error: dereferencing pointer to incomplete type
/usr/src/linux/include/linux/sched.h:919: error: dereferencing pointer to incomplete type
/usr/src/linux/include/linux/sched.h: In function `set_tsk_thread_flag':
/usr/src/linux/include/linux/sched.h:927: error: dereferencing pointer to incomplete type
/usr/src/linux/include/linux/sched.h: In function `clear_tsk_thread_flag':
/usr/src/linux/include/linux/sched.h:932: error: dereferencing pointer to incomplete type
/usr/src/linux/include/linux/sched.h: In function `test_and_set_tsk_thread_flag':
/usr/src/linux/include/linux/sched.h:937: error: dereferencing pointer to incomplete type
/usr/src/linux/include/linux/sched.h: In function `test_and_clear_tsk_thread_flag':
/usr/src/linux/include/linux/sched.h:942: error: dereferencing pointer to incomplete type
/usr/src/linux/include/linux/sched.h: In function `test_tsk_thread_flag':
/usr/src/linux/include/linux/sched.h:947: error: dereferencing pointer to incomplete type
/usr/src/linux/include/linux/sched.h: In function `set_tsk_need_resched':
/usr/src/linux/include/linux/sched.h:952: error: `TIF_NEED_RESCHED' undeclared (first use in this function)
/usr/src/linux/include/linux/sched.h: In function `clear_tsk_need_resched':
/usr/src/linux/include/linux/sched.h:957: error: `TIF_NEED_RESCHED' undeclared (first use in this function)
/usr/src/linux/include/linux/sched.h: In function `signal_pending':
/usr/src/linux/include/linux/sched.h:962: error: `TIF_SIGPENDING' undeclared (first use in this function)
/usr/src/linux/include/linux/sched.h: In function `need_resched':
/usr/src/linux/include/linux/sched.h:967: error: `TIF_NEED_RESCHED' undeclared (first use in this function)
/usr/src/linux/include/linux/sched.h: In function `arch_pick_mmap_layout':
/usr/src/linux/include/linux/sched.h:1040: error: dereferencing pointer to incomplete type
/usr/src/linux/include/linux/sched.h:1040: error: `PAGE_OFFSET' undeclared (first use in this function)
/usr/src/linux/include/linux/sched.h:1041: error: dereferencing pointer to incomplete type
/usr/src/linux/include/linux/sched.h:1042: error: dereferencing pointer to incomplete type
---------------------------------------------------------------------------------------------
回复

使用道具 举报

发表于 2005-2-1 09:26:00 | 显示全部楼层
对于2.4系列的内核,2.6.x的内核更改了编译内核模块的方法,某些接口的名字也变了,很多旧的教程都是针对2.4系列内核的。

http://www.linuxfans.org/nuke/modules.php?name=Forums&file=viewtopic&t=42871
回复

使用道具 举报

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

本版积分规则

GMT+8, 2024-11-6 16:45 , Processed in 0.045747 second(s), 15 queries .

© 2021 Powered by Discuz! X3.5.

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