使用模块编译的方式编译多个.c源文件

来源:互联网 发布:七匣子充值淘宝网 编辑:程序博客网 时间:2024/05/16 13:44

编译多个源文件的模块的步骤:

1、先去编译多个.c文件
2、有个makefile
3、模块之间有机结合

一、用gcc编译多个.c文件
1.c
#include <stdio.h>
int main()
{
    int a;
    int b;
    a = 2;
    b = 7;
    printf("2+7 = %d\n",add(a,b));
    printf("2*7 = %d\n",mul(a,b));
    return 0;
}
2.c
int add(int x,int y)
{ return x+y;}
3.c
int mul(int x,int y)
{   
    return x*y;
}
编译过程
gcc 1.c 2.c 3.c -o x
同时可以使用makefile来进行编译,一共有两种makefile
一种是:
#this is simple example
x.o:1.c 2.c 3.c
 gcc 1.c 2.c 3.c -o x.o
另外一种是:
#this is simple example
OBJS = x
CC  = gcc
CFLAGS = -Wall -O -g
x : 1.c 2.c 3.c
 $(CC) $^ -o $@
编译时使用make命令,之后使用./x来查看运行结果

二、使用makefile的方式编译模块
#include <linux/init.h>                               
#include <linux/module.h>                               

MODULE_LICENSE("GPL");
        
static char *name = "weipeng jing";             
static int age = 30;   

module_param(age, int, S_IRUGO);   //传参的宏                            
module_param(name, charp, S_IRUGO);  //charp是这个宏特定点字符类型                          
                               
static int hello_init(void)                               
{                               
 printk(KERN_EMERG" Name:%s\n",name);                       
 printk(KERN_EMERG" Age:%d\n",age);                              
 return 0;                               
}                               
static void hello_exit(void)                               
{                               
 printk(KERN_INFO" Module Exit\n ");                           
}
                               
module_init(hello_init); //加载模块编译的入口                              
module_exit(hello_exit);  //卸载模块编译的入口
使用到的makefile
ifneq ($(KERNELRELEASE),)

obj-m := param.o

else
 
KDIR := /lib/modules/2.6.18-53.el5xen/build
all:
 make -C $(KDIR) M=$(PWD) modules
clean:
 rm -f *.ko *.o *.mod.o *.mod.c *.symvers

endif
编译步骤:
1、使用make命令进行编译
2、使用insmod param.ko进行加载模块,传参也是从这里传,例insmod param.ko age=20
3、使用rmmod param 进行卸载模块

三、使用模块编译的方式编译多个.c源文件
hello.c                                                           
#include <linux/module.h>
#include <linux/init.h>

MODULE_LICENSE("GPL");
MODULE_AUTHOR("weipeng jing");
MODULE_DESCRIPTION("Hello World Module");
MODULE_ALIAS("a simplest module");
int res1,res2;
//module_param(res1, int, S_IRUGO);
//module_param(res2, int, S_IRUGO);

extern int add_integar(int a,int b);//可以在linux环境下的/proc/kallsyms目录下所有的公共函数
extern int sub_integar(int a,int b);//中寻找同名函数

static int __init hello_init()
{
    int res1 = add_integar(1,2);
    printk(KERN_EMERG" RES:%d\n",res1);
    return 0;
}

static void __exit hello_exit()
{
    int res2 = sub_integar(2,1);
    printk(KERN_EMERG" RES:%d\n",res2);
}

module_init(hello_init);
module_exit(hello_exit);

calculate.c ----> 目的在于将add_integar()、sub_integar()放在目标目录中
#include <linux/init.h>                               
#include <linux/module.h>
                              
MODULE_LICENSE("GPL");                               
                               
int add_integar(int a,int b)                               
{                               
 return a+b;                            
}
                              
int sub_integar(int a,int b)                               
{                               
 return a-b;                            
}                           

static int __init sym_init()
{
    return 0;
}

static void __exit sym_exit()
{

}

module_init(sym_init);
module_exit(sym_exit);

EXPORT_SYMBOL(add_integar);
EXPORT_SYMBOL(sub_integar);

makefile文件
ifneq ($(KERNELRELEASE),)

obj-m := hello.o calculate.o

else
 
KDIR := /lib/modules/2.6.18-53.el5xen/build
all:
 make -C $(KDIR) M=$(PWD) modules
clean:
 rm -f *.ko *.o *.mod.o *.mod.c *.symvers

endif

加载时:先加载calculate.ko 然后再加载hello.ko

 

原创粉丝点击