- 论坛徽章:
- 2
|
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:
- PROG= test
- SRCS= test_a.c test_b.c
- # We have written no manpage yet, so tell Make not to try and
- # build it from nroff sources. If you do have a manpage, you
- # usually won't need this line since the default name
- # of the manpage is ${PROG}.1 .
- MAN=
- .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:
- CFLAGS+= -I/usr/X11R6/include
复制代码
For the linker, this is done by
If you're adding libraries or include paths, be sure to make lint know about them:
- LINTFLAGS+= -lX11 -I/usr/X11R6/include
复制代码
If you're creating a library, the Makefile looks slightly different:
- LIB= mylib
- SRCS= mylib_a.c mylib_b.c
- .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:
- SUBDIR= test mylib
- .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 编辑 ] |
|