2009年2月12日 星期四

「引」如何编写makefile

http://zhidao.baidu.com/question/81844701.html

http://wiki.netbsd.se/index.php/Basic_Unix_programming#Using_BSD_Make
去看看,也许有帮助
回答者: tdaixn - 举人 五级 1-10 14:24
你先用gcc把它给编译出来。然后再想用makefile

gcc最一般的用法就是:
gcc -o 要生成的可执行文件名 源代码文件名
如:gcc -o hello.x hello.c
如果一些头文件要指明的话,可以这样:
gcc -o hello.x -I头文件所在的文件夹 -l一些库名 hello.c
最通常,我们用到一些数学库。gcc -o hello.x -lm hello.c

makefile的话,你可以基于上述的语句进行修改:建议你看点资料,或一些典型的例子。但是注意的是规则那一行,得用Tab键打头。
hello.x : hello.o
gcc -o hello.x hello.o (这一行,得用Tab打头)
hello.o : hello.c 头文件
gcc -c hello.o hello.c -I头文件所在目录 -lm (这一行,得用Tab打头)
回答者: wacs5 - 首席运营官 十二级 1-14 10:18
1 # To build modules outside of the kernel tree, we run "make"
2 # in the kernel source tree; the Makefile these then includes this
3 # Makefile once again.
4 # This conditional selects whether we are being included from the
5 # kernel Makefile or not.
6 ifeq ($(KERNELRELEASE),)
7
8 # Assume the source tree is where the running kernel was built
9 # You should set KERNELDIR in the environment if it's elsewhere
10 KERNELDIR ?= /lib/modules/$(shell uname -r)/build
11 # The current directory is passed to sub-makes as argument
12 PWD := $(shell pwd)
13
14 modules:
15 $(MAKE) -C $(KERNELDIR) M=$(PWD) modules
16
17 modules_install:
18 $(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install
19
20 clean:
21 rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions
22
23 .PHONY: modules modules_install clean
24
25 else
26 # called from kernel build system: just declare what our modules are
27 obj-m := hello.o
28 endif

在lwn上可以找到这个例子,你可以把以上两个文件放在你的某个目录下,然后执行make,也许你不一定能成功,因为linux kernel 2.6要求你编译模块之前,必须先在内核源代码目录下执行make,换言之,你必须先配置过内核,执行过make,然后才能make你自己的模块.原因我就不细说了,你按着她要求的这么去做就行了.

另外注意命令前面必须是tab
什么是命令?
类似于这种
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules

沒有留言:

張貼留言