免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 6677 | 回复: 19
打印 上一主题 下一主题

[FreeBSD] 这样的Makefile怎么写? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-03-02 12:00 |只看该作者 |倒序浏览
要写一个内核模块,文件组织是这样的:
有一个目录dir,该目录下有两个子目录dir1、dir2
其中目录dir中有文件a.c、b.c、c.c及对应的头文件
子目录dir1中有a1.c、b1.c等,其中a1.c和b1.c要依赖a.c、b.c提供的接口
子目录dir2中有也有一些c文件。
这样的vMakefile怎么编写呢?请大虾们赐教。

论坛徽章:
0
2 [报告]
发表于 2007-03-02 13:15 |只看该作者
参看 /usr/share/mk/ 下

论坛徽章:
0
3 [报告]
发表于 2007-03-02 13:15 |只看该作者
原帖由 ktrudger 于 2007-3-2 12:00 发表
要写一个内核模块,文件组织是这样的:
有一个目录dir,该目录下有两个子目录dir1、dir2
其中目录dir中有文件a.c、b.c、c.c及对应的头文件
子目录dir1中有a1.c、b1.c等,其中a1.c和b1.c要依赖a.c、b.c提供的接 ...


子目录疑似用-c,“依赖”在链接的时候考虑。

论坛徽章:
0
4 [报告]
发表于 2007-03-02 13:16 |只看该作者
原帖由 mingyanguo 于 2007-3-2 13:15 发表
参看 /usr/share/mk/ 下


嗯,现成的轮子。

论坛徽章:
0
5 [报告]
发表于 2007-03-02 13:23 |只看该作者
原帖由 雨丝风片 于 2007-3-2 13:16 发表


嗯,现成的轮子。

好复杂

论坛徽章:
0
6 [报告]
发表于 2007-03-02 13:28 |只看该作者
#        from: @(#)bsd.subdir.mk        5.9 (Berkeley) 2/1/91
# $FreeBSD: src/share/mk/bsd.subdir.mk,v 1.30.2.5 2002/07/22 14:21:51 ru Exp $
#
# The include file <bsd.subdir.mk> contains the default targets
# for building subdirectories.
#
# For all of the directories listed in the variable SUBDIRS, the
# specified directory will be visited and the target made. There is
# also a default target which allows the command "make subdir" where
# subdir is any directory listed in the variable SUBDIRS.
#
#
# +++ variables +++
#
# DISTRIBUTION        Name of distribution. [bin]
#
# SUBDIR        A list of subdirectories that should be built as well.
#                Each of the targets will execute the same target in the
#                subdirectories.
#
# +++ targets +++
#
#        distribute:
#                 This is a variant of install, which will
#                 put the stuff into the right "distribution".
#
#        afterinstall, all, all-man, beforeinstall, checkdpadd,
#        clean, cleandepend, cleandir, depend, install, lint, maninstall,
#        obj, objlink, realinstall, regress, tags
#

.include <bsd.init.mk>

DISTRIBUTION?=        bin
.if !target(distribute)
distribute:
.for dist in ${DISTRIBUTION}
        cd ${.CURDIR}; \
            ${MAKE} install -DNO_SUBDIR DESTDIR=${DISTDIR}/${dist} SHARED=copies
.endfor
.endif

_SUBDIR: .USE
.if defined(SUBDIR) && !empty(SUBDIR) && !defined(NO_SUBDIR)
        @for entry in ${SUBDIR}; do \
                if test -d ${.CURDIR}/$${entry}.${MACHINE_ARCH}; then \
                        ${ECHODIR} "===> ${DIRPRFX}$${entry}.${MACHINE_ARCH}"; \
                        edir=$${entry}.${MACHINE_ARCH}; \
                        cd ${.CURDIR}/$${edir}; \
                else \
                        ${ECHODIR} "===> ${DIRPRFX}$$entry"; \
                        edir=$${entry}; \
                        cd ${.CURDIR}/$${edir}; \
                fi; \
                ${MAKE} ${.TARGET:realinstall=install} \
                    DIRPRFX=${DIRPRFX}$$edir/; \
        done
.endif

${SUBDIR}::
        @if test -d ${.TARGET}.${MACHINE_ARCH}; then \
                cd ${.CURDIR}/${.TARGET}.${MACHINE_ARCH}; \
        else \
                cd ${.CURDIR}/${.TARGET}; \
        fi; \
        ${MAKE} all


.for __target in all all-man checkdpadd clean cleandepend cleandir \
    depend distribute lint maninstall \
    obj objlink realinstall regress tags
${__target}: _SUBDIR
.endfor

.for __target in files includes
.for __stage in build install
${__stage}${__target}:
.if make(${__stage}${__target})
${__stage}${__target}: _SUBDIR
.endif
.endfor
${__target}:
        cd ${.CURDIR}; ${MAKE} build${__target}; ${MAKE} install${__target}
.endfor

.if !target(install)
.if !target(beforeinstall)
beforeinstall:
.endif
.if !target(afterinstall)
afterinstall:
.endif
install: beforeinstall realinstall afterinstall
.ORDER: beforeinstall realinstall afterinstall
.endif

论坛徽章:
0
7 [报告]
发表于 2007-03-02 13:32 |只看该作者
原帖由 ktrudger 于 2007-3-2 13:23 发表

好复杂


现成的轮子是让你用的,不是让你照着再做一个的。。。

论坛徽章:
0
8 [报告]
发表于 2007-03-02 18:04 |只看该作者
知道的朋友能不能说详细点,在主目录的Makefile中加入:
SUBDIR += dir2
SUBDIR += dir3
.include <bsd.subdir.mk>
这样对不对?
另外,
不知道子目录的Makefile如何写?

论坛徽章:
0
9 [报告]
发表于 2007-03-02 18:23 |只看该作者
/usr/src/
下面有很多使用这些makefile的例子。

论坛徽章:
2
亥猪
日期:2014-03-19 16:36:35午马
日期:2014-11-23 23:48:46
10 [报告]
发表于 2007-03-03 02:14 |只看该作者
http://wiki.netbsd.se/index.php/Basic_Unix_programming#Using_BSD_Make


Using BSD Make
Makefiles are nice, but typing the same lines all the time can get very annoying, even if you use SUFFIXES.

BSD's Make is a very nice Make, which comes pre-packed with some files which can make your life a lot easier and your Makefiles more elegant, but it is not compatible with GNU make. Some things are even incompatible between the Makes of different versions of BSD. Now we got that out of the way, let's see an example:

  1. PROG=   test
  2. SRCS=   test_a.c test_b.c
  3. # We have written no manpage yet, so tell Make not to try and
  4. # build it from nroff sources.  If you do have a manpage, you
  5. #  usually won't need this line since the default name
  6. #  of the manpage is ${PROG}.1 .
  7. MAN=

  8. .include <bsd.prog.mk>
复制代码

That's all there's to it! Put this in the same directory as the `test' program's sources and you're good to go.

If you're on a non-BSD system, chances are that the normal `make' program will choke on this file. In that case, the BSD Make might be installed as `pmake', or `bmake'. On Mac OS X, BSD make is called `bsdmake', the default 'make' is GNU Make. If you can't find make on your particular system, ask your administrator about it.

The bsd.prog.mk file (in /usr/share/mk) does all the work of building the program, taking care of dependencies etc. This file also makes available a plethora of targets, like `all', `clean', `distclean' and even `install'. A good BSD Make implementation will even call `lint' on your source files to ensure your code is nice and clean.

If you wish to add flags to the C compiler, the clean way to do it is like this:

  1. CFLAGS+= -I/usr/X11R6/include
复制代码

For the linker, this is done by

  1. LDADD=        -lX11
复制代码

If you're adding libraries or include paths, be sure to make lint know about them:

  1. LINTFLAGS+=        -lX11 -I/usr/X11R6/include
复制代码

If you're creating a library, the Makefile looks slightly different:

  1. LIB=    mylib
  2. SRCS=   mylib_a.c mylib_b.c

  3. .include <bsd.lib.mk>
复制代码

A library doesn't have a manpage by default. You can force one to be built by supplying a MAN line, of course.

As you can see, the BSD Make system is extremely elegant for large projects. For simple projects also, but only if you have one program per directory. The system does not handle multiple programs in one directory at all. Of course, in large projects, using directories for each program is a must to keep the project structured, so this shouldn't be a major problem.

The main directory of a project should contain this Makefile:

  1. SUBDIR=         test mylib

  2. .include <bsd.subdir.mk>
复制代码

Additionally, bsd.prog.mk and bsd.lib.mk always include the file ../Makefile.inc, so you can keep global settings (like DEBUG switches etc) in a Makefile.inc file at toplevel.

For more information, usually there is a /usr/share/mk/bsd.README file which explains BSD Make more completely than this little document. See also BSD Make.

[ 本帖最后由 gvim 于 2007-3-3 02:17 编辑 ]
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP