您的位置:首页 > 其它

source文件和makefile文件编写

2014-11-11 21:27 489 查看

http://blog.csdn.net/kaylc/article/details/6263296

一. makefile (没有扩展名,它名字就叫makefile),内容如下:

  !INCLUDE $(NTMAKEENV)/makefile.def

  WDM程序使用的所有makefile都这样写,我们只需写一个,编译时把它拷贝到工作目录下就行了

  二. sources文件就需要我们根据不同的场合修改了,不过基本模板如下:

  TARGETNAME=驱动程序名 //(不含扩展名)

  TARGETPATH=obj // 固定不变

  TARGETTYPE=DRIVER // 固定不变(表明,连接成*.sys文件)

  DRIVERTYPE=WDM // 为 Win32 Driver Model 驱动

  INCLUDES=$(BASEDIR)/inc/ddk;$(BASEDIR)/inc // 源程序可能使用的DDK头文件所在的目录,多个目录用";" 隔开,多个文件用 '空格' 隔开

  其中"$(BASEDIR)"指DDK当前的安装目录,【windows通过添加系统环境变量即可】例如当前DDK安装在D:上,则$(BASEDIR) 就是 "D:/DDK",所以上面的INCLUDES可以翻译成D:/DDK/inc/ddk; D:/DDK/inc

  三. 注意:

  1. 编译时必须保证 makefile,sources和源程序在同一目录下

  2. 编写sources文件时,其中的"="两边不能有空格

  3. 工程的工作目录的绝对路径中不能出现空格,而且表面上看来DDK好像是完成的编译,实际上它什么都没做!

官方说明:

The sources file

This is where build gets most of its directives. Here are some of the more important ones — and note that the first four are required:

TARGETNAME: This required directive specifies the name of the intended binary. For example, you might give TARGETNAME=MyDriver (build will append .sys to the driver’s name).

TARGETTYPE: This required directive dictates the type of binary to be built. Most common are:

TARGETYPE=DRIVER for a plain-vanilla driver (a .sys file).

TARGETTYPE=EXPORT_DRIVER for a driver that exports entry points as a DLL does. (The resulting binary is a .sys file.) A kernel DLL is such a creature. See Tim Roberts’ article about kernel-mode DLLs for more information about this option.

TARGETTYPE=DRIVER_LIBRARY (or TARGETTYPE=LIBRARY) for a .lib binary that will be link-edited into an executable. This feature is typically used when there are two or more source directories (perhaps because each is owned by a different developer). The sources
file in one directory could have TARGETTYPE=DRIVER (for a .sys file); the sources in the second (or third, etc.) directory would give TARGETTYPE=DRIVER_LIBRARY. A complete sources file has an example.

TARGETTYPE=PROGRAM for a user-space program (an .exe file), for example, a program to call the driver.

TARGETTYPE=DYNLINK for a user-space DLL, which an .exe file might use.

In case you’re wondering how names like ntoskrnl.exe and hal.dll are generated for kernel modules, the answer is that build has special-case logic. Don’t try to create kernel modules of your own with an extension of .exe or .dll.

TARGETPATH: This required directive says where to put the binary.

Note: The specific form TARGETPATH=lib$(BUILD_ALT_DIR) is often employed, and the resulting directory path is one beneath the one where the sources file is located; for a WinXP checked-build target, that path would be libchk_wxp_x86/i386. The path name comes
from concatenating “lib” with the evaluated environmental variable BUILD_ALT_DIR (see environmental variables) and with “/i386,” which designates the platform type. If you’re using this form to produce a .lib file, you will specify the file’s path in TARGETPATH
differently from the way you will specify the path for that same file in building an executable incorporating the .lib file (see TARGETLIBS).

SOURCES: This required directive names the source files. For example,

SOURCES= /

BozoStuff.cpp /

miniport.c /

passthru.c /

passthru.rc /

protocol.c /

UtilRtns.c

Notice the mixture of .c and .cpp files: The compiler’s default action is to apply C or C++ language rules according to file type.

Note: If a directive is to have more than one entry, you may specify all on a single line, or you may put them on several lines, but then you must end each line with a backslash and no following characters, not even blanks. This restriction applies to directives
in sources files and in dirs files.

A further note: In SOURCES, names can take the form filename.ext, ./ filename.ext and ../ filename.ext, but not ../../ filename.ext and not a fully qualified name like c:/dir/ filename.ext. That is another quirk of build.

INCLUDES: This specifies the directories for any header files (other than the target OS’s standard directories). For example,

INCLUDES= /

./inc /

C:/JA.pgm/src/C/Drivers/JADriver/inc

Note: Unlike SOURCES, INCLUDES allows forms like ../../Dir and fully qualified directory paths. Do not, however, specify paths with imbedded blanks. Instead, use the 8.3 name (you can discover the 8.3 names of directories and files below a directory by issuing
the command dir /x <directory> in a command window).

C_DEFINES: A directive to affect compilation. Its value is passed to the C compiler (and to the MIDL compiler). See compiler parameters for some possibilities.

USER_C_FLAGS: Another directive to affect compilation. Its value is passed to the C/C++ compiler. For example,

USER_C_FLAGS=$(USER_C_FLAGS) /FAsc

would pick up any value defined in the environmental variable USER_C_FLAGS in the command window and would append /FAsc to that value. See compiler parameters for other possibilities.

MSC_OPTIMIZATION: This overrides the default compiler optimization of build. A checked build will have /Od /Oi, to prevent optimization.

MSC_WARNING_LEVEL: This controls the compiler’s warning level. The default is /W3, but it is better practice to set it to /W3 /Wx so that warnings are considered errors.

TARGETLIBS: This names one or more libraries needed by link-edit to resolve references (APIs, sections of code, variables). Many OS libraries like ntoskrnl.lib are automatically searched, but you may need to specify some. If you were building an NDIS intermediate
driver, for example, you would specify TARGETLIBS=$(DDK_LIB_PATH) /ndis.lib. And you would of course name any libraries of your own.

Note: TARGETLIBS does not have the quite same special handling that TARGETPATH does. If you created a .lib file with a path of TARGETPATH=lib$(BUILD_ALT_DIR), in the sources file for the .sys file you would designate the .lib file’s path more fully, by lib$(BUILD_ALT_DIR)/*/
(asterisk means implicit platform type).

To make the point above clear, let’s suppose you have this for the .lib file:

TARGETNAME=DriverPart2

TARGETTYPE=DRIVER_LIBRARY # output is a .lib file

TARGETPATH=lib$(BUILD_ALT_DIR) # where to put the .lib file.

For the .sys file, you would have:

TARGETNAME=MyDriver

TARGETTYPE=DRIVER # output is a .sys file

TARGETLIBS=lib$(BUILD_ALT_DIR)/*/DriverPart2 # where to find the .lib file.

TARGETPATH=lib$(BUILD_ALT_DIR) # where to put the .sys file.

LINKER_FLAGS: This is information supplied to the linker as a parameter. If you want the linker to produce a map, for example, specify "-MAP" as the value of this parameter.

Browser information: You specify this if you intend to use the Visual Studio browser to find definitions of APIs, symbols and variables (see browsable defintions); in a large project, especially in one to which you are new, this capability is a very handy one.
You need two directives to get browser information: the first to make build create it, and the second to indicate the name of the browser database file.

BROWSER_INFO=1

BROWSERFILE=$(TARGETNAME).bsc -n

You can use conditional logic. For example:

!if !defined(DDK_TARGET_OS) || "$(DDK_TARGET_OS)"=="Win2K"

# The driver is for Win2K. Build with NDIS 4.0

C_DEFINES=$(C_DEFINES) -DNDIS40_MINIPORT=1

C_DEFINES=$(C_DEFINES) -DNDIS40=1

!else

# The driver is for WinXP or Win2003, so build# with NDIS 5.1.

C_DEFINES=$(C_DEFINES) -DNDIS51_MINIPORT=1

C_DEFINES=$(C_DEFINES) -DNDIS51=1

!endif

Putting it all together, you might have this in a complete sources file:

TARGETNAME=MyPassthru

TARGETYPE=DRIVER

TARGETPATH=lib$(BUILD_ALT_DIR)

TARGETLIBS=$(DDK_LIB_PATH)/ndis.lib

SOURCES= /

BozoStuff.cpp /

miniport.c /

passthru.c /

passthru.rc /

protocol.c /

UtilRtns.c

INCLUDES= /

./inc /

C:/JA.pgm/src/C/Drivers/JADriver/inc

USER_C_FLAGS=$(USER_C_FLAGS) /FAsc

!if !defined(DDK_TARGET_OS) || "$(DDK_TARGET_OS)"=="Win2K"

C_DEFINES=$(C_DEFINES) -DNDIS40_MINIPORT=1

C_DEFINES=$(C_DEFINES) -DNDIS40=1

!else

C_DEFINES=$(C_DEFINES) -DNDIS51_MINIPORT=1

C_DEFINES=$(C_DEFINES) -DNDIS51=1

!endif

BROWSER_INFO=1

BROWSERFILE=$(TARGETNAME).bsc -n

You can find a full list of build directives in the DDK documentation under the heading “Build Utility Macros.”

Note: After you make a change in a sources file, it is usually necessary to rebuild completely to pick up the changes.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: