文档库 最新最全的文档下载
当前位置:文档库 › CCS下makefile的编写

CCS下makefile的编写

CCS下makefile的编写
CCS下makefile的编写

CCS makefile文件编写

前期预备知识

Alternation and Grouping

1 | 用法PUBLIC|PRIVA TE :表示匹配单词为PUBLIC或者PRIV ATE

2 空格以用法PUBLIC (void|DWORD) :

3 ^ 表示一行的开始处,[ \t] 表跟着多个空格或tabs.

例子:^PUBLIC[ \t]+(void|int|long|DWORD) It matches, at the beginning of a line, the word "PUBLIC", followed by one or more spaces or tabs, followed by any one of the following words: "void", "int", "long" or "DWORD".

Beginning and Ending of Line

^PUBLIC:一行开头是PUBLIC。

\)$:一行结尾是圆括号。

^\)$ 这一行就只有一个圆括号。

Character Classes

[AEIOUY aeiouy] :matches any vowel, whether upper or lower case.

[0-9]will match any character between 0 and 9.

^ 如果不用在开头字母上,放中间表示it just adds the up-caret to that class. Y ou may use it as a shorthand method of saying "match any characters except for the following: rather than specifying a large character class. For example, [^$.|(){}*+?^] matches anything except the eleven characters following the up-caret. Escape Sequences 转义字符

Escape Meaning

\n Newline ( or , depending on how it is defined for the document)

\t Tab

\b [Ctrl]-[H] (backspace)

\r Carriage return

\f form feed

\nnn Octal value between 0 and 0377.

\xnn Hexadecimal digit between 0x00 and 0xFF

\m The literal character m.

Note: When using escape sequences in C language source code, each backslash must also be escaped with another backslash. The resulting pattern specification is often unaesthetic.

Iteration Qualifiers

the * matches any number of occurrences, the + matches one or more, and the ? matches zero or one.

特例:

\t* The example above will match any number of consecutive tabs, including none. By itself, it is not very useful to match none of something. As part of a larger regular expression, it could be quite useful. For our purposes here, the following might be preferable:

\t+ This example matches one or more consecutive tabs. The tab is represented as \t, and the plus sign says "one or more of the previous." To match whitespace, we need to match spaces too. We don't know what order the spaces and tabs will come in, and we don't know how many there will be. These are signs that we need a character class.

[ \t]+ The example above uses a character class containing a space and a tab. The + sign following it means that this Regular Expression will match any combination of spaces and tabs, so long as there is at least one space or tab.If you wanted to match the whitespace within a function call, where you knew there might be one space or tab, or there might be none, your expression could look like this:

\([ \t]? This example searches for a left parenthesis followed by zero or one spaces or tabs. Since the left parenthesis is a meta-character it is necessary to escape or quote it with a preceding backslash. The \t we have used before to represent a tab character. The question mark says "zero or one of the preceding."

Matching a Character

The basic unit of a regular expression is matching a single character. Y ou can match a single character in one of three ways:

Literally,

Ambiguously, or

With a Character Class.

Y ou may match a character literally by using the character itself or the appropriate escape sequence. If matching a character literally is too limiting, you may match ambiguously, by using the dot ( . ) metacharacter. If a literal character is too narrow a match and the dot is too broad a match, a character class can be used for anything in

between.

Placing the Cursor

The escape sequence that specifies cursor position is \c. An example of its use follows:

singletons\[\c.*\]

This example places the cursor at the beginning of whatever text is contained between the left and right square brackets. The square brackets are metacharacters and are therefore escaped. This cursor positioning facilitates editing the contents of the square brackets.

Reference Groups and Replacement Strings

Regex Examples

Regular Expressions Tips

Regular Expression Syntax

Character Description

\ Marks the next character as special. To define a special character in a regular expression, precede the special character with the backslash character.

Example: The regular expression /n/ matches the character n. The regular expression /\n/ matches a linefeed or newline character.

^ Matches the beginning of input or line. In this implementation, this character cannot be defined in character set.

$ Matches the end of input or line. In this implementation this character cannot be defined in a character set.

* Matches the preceding character zero or more times. In this implementation cannot be defined if only one character is specified in the regular expression. That means that /zo*/ matches z and zoo, but /z*/ will match nothing because only one character has been specified.

+ Matches the preceding character one or more times.

? Matches the preceding character zero or one time. In this implementation cannot be defined if only one character is specified in the regular expression.

. Matches any single character except '\n'.

(pattern) Matches the pattern and remembers the match. The matched substring can be retrieved by using '\0'-'\9' in the regular expression, where '0'-'9' are the numbers of the pattern.

Example: Regular expression '(re).*\0s+ion' will match 'regular expression'. First, the pattern ?re)?matches the first two letters of 憆egular expression?and the pattern is remembered with index 0. The pattern '*' matches 'gular exp' in 'regular expression'. The pattern 慭0?retrieves the pattern that has been remembered with index 0, and this 're' matches the second occurrence of 're' in 'regular expression'. Finally, the pattern 's+ion' matches 'ssion'.

x|y Matches either the character 'x' or 'y'. Y ou can combine more than two characters. Example: 'x|y|z'.

{n} Means the preceding character will match exactly n times (nonnegative).

{n,} M eans the preceding character will match at least n times (nonnegative).

{n,m} Means the preceding character will match at least n times and at most m times (both nonnegative).

[xyz] A character set. Matches any one of the enclosed characters.

[^xyz] A non-matching character set. Matches any character that is not in the set.

\b Matches a word boundary, that is, the boundary between any character excluding space characters (" \f\n\r\t\v") and non-space characters.

\B Matches a non-word boundary. Matches any boundary between space characters or between non-space characters.

\d Matches any digit /0-9/

\D Matches any non-digit.

\e Marks the start position of text to extract; used in conjunction with \E.

\E Marks the end position of text to extract; used in conjunction with \e. If the \E is not supplied, the end of line ($) will be used as the end position.

\f Matches a formfeed.

\n Matches a newline character.

\r Matches a carridge return character.

\s Matches any white space character.

\S Matches any non-white space character.

\t Matches a tab character.

\v Matches any vertical tab character.

\w Matches any word character including underscore. [A-Za-z0-9_]

\W Matches any non-word character (any character that does not match \w).

\num Where num is a value between 0 and 9. Matches remembered pattern. (See description of pattern.)

/n/ Where n is a value between 1 and 255. Matches supplied in n ASCII code. Examples

"FIND\e.+" M atches "FIND.+" but only returns the characters after "FIND"

"Find\e.+\ETheRest" Matches "Find.+TheRest" but only returns the characters between "Find" and "TheRest".

"^SOURCE=\e.+$" Extracts all source files after the SOURCE=macro definition. "^.+\.obj\s*:\e.+$" Extracts all the source dependencies in a makefile.

"^\w+s*=\e.+$" Extracts all macro definitions.

Searching for control characters (binary/hex data)

Control characters can be used in both search and replacement patterns. Any byte value can be specified as part of a pattern. Perform the following steps to search for or replace binary or hex values.

1. Go to Edit 郌ind (or Find/Replace, Find in Files, Replace in Files).

2. Turn on Regular Expressions in the appropriate dialog.

3. Then use the \x notation to specify hexadecimal values.

Examples:

To search for the form feed character, use this string:

\x0c

(That's backslash x zero c.)

Make sure there are two digits following the x.

To search for two consecutive bytes by hexadecimal value, string them together like this:

\x0c\x0d

If you are replacing EOL characters, the following information will help to understand how CodeWright views and deals with End of Line characters.

Generally, using '\n' for matching or replacing line ends is the suggested method in CodeWright (the '\' must be doubled when used in code). However, a special case is made for searching for and replacing hexadecimal 0D and 0A characters. Since these

characters make up ends of lines, some rules had to be established to avoid disrupting partial line-end sequences when searching for and replacing them. When searching for 0D and 0A hex characters, keep the following in mind:

?穃x0a will match a solo (without a preceding 0x0D) 0x0A but not the pair.

?穃x0d will match a solo (without a following 0x0A) 0x0D but not the pair.

?\n will match a solo 0x0A or a 0x0D,0x0A pair.

?Replacing "\x0d" with "\n" would be good for converting a Macintosh file (opened with autodetect file type off).

?Replacing "\n" with "\n" is a good way to convert Unix files to MS-DOS files or vice-versa, if the buffer flag for Unix EOLs is set appropriately in Options 郋ditor|郘anguage郞ptions.

See Hex Mode for more information on hex editing.

Special Characters

Character Meaning

. Matches any single character, except a newline

* Matches any number of occurrences (even zero) of the expression that precedes it.

+ Matches one or more occurrences of the preceding expression.

? Matches zero or one occurrence of the preceding expression.

[ and ] Defines the beginning and end of a character class.

( and ) Defines the beginning and end of a group of expressions. Groups expressions into larger units and dictates precedence. Each group is also a Reference Group that may be pasted into a replacement string.

| Alternation. Allows matching the expression on the left or on the right of the operator.

$ Matches the end of a line.

^ Two meanings: Matches the beginning of a line. Complement operator when the first character in a character class.

\ Used for escaping metacharacters and non-printing characters.

\c The position in the pattern at which the cursor is placed at the end of a successful search.

CodeSizeTune - Using External Makefiles

Y ou can use an external makefile with CodeSizeTune. However, to be successfully used, you must make modifications to the makefile so that the PBC_CMDFILE macro is added to the build options.

For example, if the gmake command to build the program is:

gmake -makefile.mak TARG=c6x

When running CodeSizeTune, the command line would be:

gmake -makefile.mak TARG=c6x PBC_CMDFILE=-@C:\TEMP\PBCV ariantOpts

Using Regular Expressions

A regular expression defines a specific text pattern that can be used to be locate and retrieve specific sections of text. For example, nat.* is a regular expression which searches for all occurrences of "nat" followed by one or more characters (e.g. nathan, nate, natalie, etc.). There are many more regular expression characters, however, these are the most commonly used ones.

Regular Expressions By Example

Adding Regular Expressions to Makefile Import

In this exercise, you will learn how to specify the source files you wish to import within the makefile importing utility. The makefile utility will scan the makefile you are importing, looking for the source files you specify. In this exercise, you will configure the makefile utility to scan for source files and include files. Specification is done using regular expressions as in the previous exercise.

1.Add the default regular expression "^SOURCE\e.+$" (without the

quotes) to the Added Expression section by clicking the Add button.

In this instance, "^SOURCE\e.+$" indicates that CCS will search the makefile for all lines containing source file information (e.g.

SOURCE=). The "^" and "$" characters indicate that these

lines must have no additional or extraneous text.

2.In the Regular Expression to Add, type "^INCLUDE\e.+$" (without the

quotes). Press Add. In this instance, "^INCLUDE\e.+$" indicates that CCS will search the makefile for all lines containing header file information (e.g. INCLUDE=

indicate that these lines must have no additional or extraneous

text.

1.Don’t press Finish yet!In the next exercise you will learn how

to use the macro tool.

Using Macros

Macros allow you to apply advanced search-and-replace techniques to a file. For example, below is a macro definition located in a makefile:

WHICH_OBJ_DIR \

"$(INTDIR)\application.obj" \

"$(INTDIR)\function1.obj" \

"$(INTDIR)\function2.obj" \

If we define WHICH_OBJ_DIR to equal "C:\winnt" (without quotes) within the makefile import tool, the generator will add the following text into the generated project file (.pjt):

"C:\winnt\application.obj" \

"C:\winnt\function1.obj" \

"C:\winnt\function2.obj" \

1.In the Macro Definition to Add field, enter: WHICH_OBJ_DIR

2.In the Macro value field, enter: C:\winnt

3.Click the Add button to add the macro. Your window should look like

the one below.

2.

Note: You must examine your own makefile to identify any macros defined within it. Each software development tool defines its own set of macros.

4.Click Finish to save.

Makefile规则

目录 1.简介 3 1.1.准备工作 3 1.2.Makefile介绍 3 1.3.规则简介 4 1.4.make工作原理 4 1.5.使用变量 5 1.6.简化命令 6 1.7.另一种风格 6 1.8.清理 7 2.Makefile 7 2.1.makefile名字 7 2.2.包含 8 2.3.‘MAKEFILE’变量 8 2.4.怎么重新生成makefile 8 2.5.重载makefile 9 3.规则 9 3.1.例子 9 3.2.规则的语法 9 3.3.通配符 10 3.3.1.通配符的缺陷 10 3.3.2.wildcard函数 11 3.4.目录搜索 11 3.4.1.‘VPATH’ 11 3.4.2.选择性搜索 12 3.4.3.使用自动变量 12 3.4.4.目录搜索和隐含规则 12 3.5.PHONY目标 13 3.6.FORCE目标 14 3.7.空目标 14 3.8.内建的特别目标 14 3.9.一个规则多个目标 15 3.10.一个目标多条规则 15 3.11.静态模式规则 16 3.11.1.语法 16 3.11.2.静态模式规则和隐式规则 17 3.12.双冒号规则 17 3.13.自动生成依赖关系 17 4.编写命令 18 4.1.回显 18 4.2.执行 19 4.3.并行执行 19 4.4.错误 19 4.5.中断make 20 4.6.递归使用 20 4.6.1.‘MAKE’变量 20 4.6.2.传递变量到子make 21 5.命令行参数 21 6.参考 25

6.1.指令 25 6.2.函数 26 6.3.自动变量 27 6.4.特别变量 29 GNU Make使用 Make 程式最初设计是为了维护C程式文件防止不必要的重新编译。在使用命令行编译器的时候,修改了一个工程中的头文件,怎么确保包含这个头文件的所有文件都得到编译?目前10机的版本生成是使用批处理程式,编译那些文件依赖于程式的维护者,在模块之间相互引用头文件的情况下,要将所有需要重新编译的文件找出来是一件痛苦的事情;在找到这些文件之后,修改批处理进行编译。实际上这些工作能让make程式来自动完成,make工具对于维护一些具有相互依赖关系的文件特别有用,他对文件和命令的联系(在文件改动时调用来更新其他文件的程式)提供一套编码方法。Make工具的基本概念类似于Proglog语言,你告诉make需要做什么,提供一些规则,make来完成剩下的工作。 1.简介 make工作自动确定工程的哪部分需要重新编译,执行命令去编译他们。虽然make多用于C程式,然而只要提供命令行的编译器,你能将其用于所有语言。实际上,make工具的应用范围不仅于编程,你能描述任和一些文件改动需要自动更新另一些文件的任务来使用他。 1.1.准备工作 如果要使用make,你必须写一个叫做“makefile”的文件,这个文件描述工程中文件之间的关系,提供更新每个文件的命令。典型的工程是这样的:可执行文件靠目标文件来更新,目标文件靠编译源文件来更新。 Makefile写好之后,每次更改了源文件后,只要执行make就足够了,所有必要的重新编译将执行。Make程式利用makefile中的数据库和文件的最后修改时间来确定那个文件需要更新;对于需要更新的文件,make执行数据库中记录的命令。 能提供命令行参数给make来控制那个文件需要重新编译。 1.2.Makefile介绍 Makefile文件告诉make做什么,多数情况是怎样编译和链接一个程式。 这里有一个简单的makefile,描述怎么编译链接由8个C文件和3个头文件组成的一个编辑器: edit : main.o kbd.o command.o display.o insert.o serach.o files.o utils.o cc ?o edit main.o kbd.o command.o display.o insert.o search.o files.o utils.o main.o : main.c defs.h cc ?c main.c kdb.o : kbd.c defs.h command.h cc ?c kbd.c command.o : command.c defs.h command.h cc -c command.c display.o : display.c defs.h buffer.h cc -c display.c insert.o : insert.c defs.hbuffer.h cc -c insert.c search.o : search.c defs.hbuffer.h cc -c search.c files.o : files.c defs.h buffer.h command.h cc -c files.c utils.o : utils.c defs.h cc -c utils.c

跟我一起写Makefile

跟我一起写Makefile 陈皓 1 概述 什么是makefile?或许很多Winodws的程序员都不知道这个东西,因为那些Windows的IDE都为你做了这个工作,但我觉得要作一个好的和professional的程序员,makefile还是要懂。这就好像现在有这么多的HTML的编辑器,但如果你想成为一个专业人士,你还是要了解HTML的标识的含义。特别在Unix下的软件编译,你就不能不自己写makefile了,会不会写makefile,从一个侧面说明了一个人是否具备完成大型工程的能力。 因为,makefile关系到了整个工程的编译规则。一个工程中的源文件不计数,其按类型、功能、模块分别放在若干个目录中,makefile定义了一系列的规则来指定,哪些文件需要先编译,哪些文件需要后编译,哪些文件需要重新编译,甚至于进行更复杂的功能操作,因为makefile就像一个Shell脚本一样,其中也可以执行操作系统的命令。 makefile带来的好处就是——“自动化编译”,一旦写好,只需要一个make命令,整个工程完全自动编译,极大的提高了软件开发的效率。make是一个命令工具,是一个解释makefile中指令的命令工具,一般来说,大多数的IDE都有这个命令,比如:Delphi的make,Visual C++的nmake,Linux下GNU的make。可见,makefile都成为了一种在工程方面的编译方法。 现在讲述如何写makefile的文章比较少,这是我想写这篇文章的原因。当然,不同产商的make各不相同,也有不同的语法,但其本质都是在“文件依赖性”上做文章,这里,我仅对GNU的make进行讲述,我的环境是RedHat Linux 8.0,make的版本是3.80。必竟,这个make是应用最为广泛的,也是用得最多的。而且其还是最遵循于IEEE 1003.2-1992 标准的(POSIX.2)。 在这篇文档中,将以C/C++的源码作为我们基础,所以必然涉及一些关于C/C++的编译的知识,相关于这方面的内容,还请各位查看相关的编译器的文档。这里所默认的编译器是UNIX下的GCC和CC。 2 关于程序的编译和链接 在此,我想多说关于程序编译的一些规范和方法,一般来说,无论是C、C++、还是pas,首先要把源文件编译成中间代码文件,在Windows下也就是.obj 文件,UNIX下是.o 文件,即Object File,这个动作叫做编译(compile)。然后再把大量的Object File合成执行文件,这个动作叫作链接(link)。 编译时,编译器需要的是语法的正确,函数与变量的声明的正确。对于后者,通常是你需要告诉编译器头文件的所在位置(头文件中应该只是声明,而定义应该放在C/C++文件中),只要所有的语法正确,编译器就可以编译出中间目标文件。一般来说,每个源文件都应该对应于一个中间目标文件(O文件或是OBJ 文件)。 链接时,主要是链接函数和全局变量,所以,我们可以使用这些中间目标文件(O文件或是OBJ文件)来链接我们的应用程序。链接器并不管函数所在的源文件,只管函数的中间目标文件(Object File),在大多数时候,由于源文件太多,编译生成的中间目标文件太多,而在链接时需要明显地指出中间目标文件名,这对于编译很不方便,所以,我们要给中间目标文件打个包,在Windows下这种包叫“库文件”(Library File),也就是.lib 文件,在UNIX下,是Archive File,也就是.a 文件。 总结一下,源文件首先会生成中间目标文件,再由中间目标文件生成执行文件。在编译时,编译器只检测程序语法,和函数、变量是否被声明。如果函数未被声明,编译器会给出一个警告,但可以生成Object File。而在链接程序时,链接器会在所有的Object File中找寻函数的实现,如果找不到,那到就会报链接错

手动建立makefile简单实例解析

手动建立makefile简单实例解析 假设我们有一个程序由5个文件组成,源代码如下:/*main.c*/ #include "mytool1.h" #include "mytool2.h" int main() { mytool1_print("hello mytool1!"); mytool2_print("hello mytool2!"); return 0; } /*mytool1.c*/ #include "mytool1.h" #include void mytool1_print(char *print_str) { printf("This is mytool1 print : %s ",print_str); } /*mytool1.h*/ #ifndef _MYTOOL_1_H #define _MYTOOL_1_H void mytool1_print(char *print_str); #endif /*mytool2.c*/ #include "mytool2.h" #include void mytool2_print(char *print_str) { printf("This is mytool2 print : %s ",print_str); }

/*mytool2.h*/ #ifndef _MYTOOL_2_H #define _MYTOOL_2_H void mytool2_print(char *print_str); #endif 首先了解一下make和Makefile。GNU make是一个工程管理器,它可以管理较多的文件。我所使用的RedHat 9.0的make版本为GNU Make version 3.79.1。使用make的最大好处就是实现了“自动化编译”。如果有一个上百个文件的代码构成的项目,其中一个或者几个文件进行了修改,make就能够自动识别更新了的文件代码,不需要输入冗长的命令行就可以完成最后的编译工作。make执行时,自动寻找Makefile(makefile)文件,然后执行编译工作。所以我们需要编写Makefile文件,这样可以提高实际项目的工作效率。 在一个Makefile中通常包含下面内容: 1、需要由make工具创建的目标体(target),通常是目标文件或可执行文件。 2、要创建的目标体所依赖的文件(dependency_file)。 3、创建每个目标体时需要运行的命令(command)。 格式如下: target:dependency_files command target:规则的目标。通常是程序中间或者最后需要生成的文件名,可以是.o文件、也可以是最后的可执行程序的文件名。另外,目标也可以是一个make执行的动作的名称,如目标“clean”,这样的目标称为“伪目标”。 dependency_files:规则的依赖。生成规则目标所需要的文件名列表。通常一个目标依赖于一个或者多个文件。 command:规则的命令行。是make程序所有执行的动作(任意的shell命令或者可在shell下执行的程序)。一个规则可以有多个命令行,每一条命令占一行。注意:每一个命令行必须以[Tab]字符开始,[Tab]字符告诉make此行是一个命令行。make按照命令完成相应的动作。这也是书写Makefile中容易产生,而且比较隐蔽的错误。命令就是在任何一个目标的依赖文件发生变化后重建目标的动作描述。一个目标可以没有依赖而只有动作(指定的命令)。比如Makefile中的目标“clean”,此目标没有依赖,只有命令。它所指定的命令用来删除make过程产生的中间文件(清理工作)。 在Makefile中“规则”就是描述在什么情况下、如何重建规则的目标文件,通常规则

Makefile下编写Helloworld的例子

什么是makefile?或许很多Windows的程序员都不知道这个东西,因为那些Windows的IDE都为你做了这个工作,但我觉得 要作一个好的和professional的程序员,makefile还是要懂。这就好像现在有这么多的HTML的编辑器,但如果你想成为一个专 业人士,你还是要了解HTML的标识的含义。特别在Unix下的软件编译,你就不能不自己写makefile了,会不会写makefile, 从一个侧面说明了一个人是否具备完成大型工程的能力。 因为,makefile关系到了整个工程的编译规则。一个工程中的源文件不计数,其按类型、功能、模块分别放在若干个目录中, makefile定义了一系列的规则来指定,哪些文件需要先编译,哪些文件需要后编译,哪些文件需要重新编译,甚至于进行更复 杂的功能操作,因为makefile就像一个Shell脚本一样,其中也可以执行操作系统的命令。 makefile带来的好处就是——“自动化编译”,一旦写好,只需要一个make 命令,整个工程完全自动编译,极大的提高了软件 开发的效率。make是一个命令工具,是一个解释makefile中指令的命令工具,一般来说,大多数的IDE都有这个命令,比如: Delphi的make,VisualC++的nmake,Linux下GNU的make。可见,makefile都成为了一种在工程方面的编译方法。 更新版本 hello.c程序 #include int main(){printf("Hello,World!\n");

return 0;}=== makefile开始=== Helloworld: hello.o gcc hello.o–o Helloworld Hello.o: hello.c hello.h gcc–MM hello.c gcc–c hello.c–o hello.o .PHONY: clean Clean: rm–rf*.o hellworld === makefile结束===

Linux如何写makefile文件

Linux如何写makefile文件 关于程序的编译和链接 —————————— 在此,我想多说关于程序编译的一些规范和方法,一般来说,无论是C、C++、还是pas,首先要把源文件编译成中间代码文件,在Windows下也就是 .obj 文件,UNIX下是 .o 文件,即 Object File,这个动作叫做编译(compile)。然后再把大量的Object File合成执行文件,这个动作叫作链接(link)。 编译时,编译器需要的是语法的正确,函数与变量的声明的正确。对于后者,通常是你需要告诉编译器头文件的所在位置(头文件中应该只是声明,而定义应该放在 C/C++文件中),只要所有的语法正确,编译器就可以编译出中间目标文件。一般来说,每个源文件都应该对应于一个中间目标文件(O文件或是OBJ文 件)。 链接时,主要是链接函数和全局变量,所以,我们可以使用这些中间目标文件(O文件或是OBJ文件)来链接我们的应用程序。链接器并不管函数所在的源文件, 只管函数的中间目标文件(Object File),在大多数时候,由于源文件太多,编译生成的中间目标文件太多,而在链接时需要明显地指出中间目标文件名,这对于编译很不方便,所以,我们要给 中间目标文件打个包,在Windows 下这种包叫“库文件”(Library File),也就是 .lib 文件,在UNIX下,是Archive File,也就是 .a 文件。 总结一下,源文件首先会生成中间目标文件,再由中间目标文件生成执行文件。在编译时,编译器只检测程序语法,和函数、变量是否被声明。如果函数未被声明, 编译器会给出一个警告,但可以生成Object File。而在链接程序时,链接器会在所有的Object File中找寻函数的实现,如果找不到,那到就会报链接错误码(Linker Error),在VC下,这种错误一般是:Link 2001错误,意思说是说,链接器未能找到函数的实现。你需要指定函数的Object File. 好,言归正传,GNU的make有许多的内容,闲言少叙,还是让我们开始吧。 Makefile 介绍 ——————— make命令执行时,需要一个 Makefile 文件,以告诉make命令需要怎么样的去编译和链接程序。 首先,我们用一个示例来说明Makefile的书写规则。以便给大家一个感兴认识。这个示例来源于GNU的make使用手册,在这个示例中,我们的工程有 8

编译内核Makefile时 混和的隐含和普通规则。停止

编译时makefile:Makefile:1503: *** 混和的隐含和普通规则。停止。处理 (2012-05-05 16:45:32) 转载▼ 1.makefile #AR=ar #ARCH=arm #CC=arm-linux-gcc obj-m := hello.o #if we need more than one source code to build the module #we should use the variable below: example: modules-objs := file1.o file2.o #KDIR := /lib/modules/$(shell uname -r)/build KDIR := /UP-Magic/kernel/linux-2.6.24.4 PWD := $(shell pwd) default: # $(MAKE) -C $(KDIR) M=$(PWD) modules $(MAKE) -C $(KDIR) M=$(PWD) modules clean: rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions *.order *.symvers make时出现; [root@hpx driverPractise]# make make -C /UP-Magic/kernel/linux-2.6.24.4 M=/bochuang/driverPractise modules make[1]: 进入目录“/UP-Magic/kernel/linux-2.6.24.4” Makefile:1503: *** 混和的隐含和普通规则。停止。 make[1]: 离开目录“/UP-Magic/kernel/linux-2.6.24.4” make: *** [default] 错误 2 搜索一下,有人解决了,转载过来: 在编译 kernel 时,有机会碰见下面的错误: Makefile: *** 混合的隐含和普通规则。停止。 Makefile: *** mixed implicit and normal rules. Stop. 这个原因可能是Make工具对低版本内核的Makefile一些旧的规则兼容不好,我们只需要修改对应的Makefile。 如一:

3-Makefile书写规则

3 Makefile书写规则 规则包含两个部分,一个是依赖关系,一个是生成目标的方法。 在Makefile中,规则的顺序是很重要的,因为,Makefile中只应该有一个最终目标,其它的目标都是被这个目标所连带出来的,所以一定要让make知道你的最终目标是什么。一般来说,定义在Makefile中的目标可能会有很多,但是第一条规则中的目标将被确立为最终的目标。如果第一条规则中的目标有很多个,那么,第一个目标会成为最终的目标。make所完成的也就是这个目标。 好了,还是让我们来看一看如何书写规则。 3.1 规则举例 foo.o : foo.c defs.h # foo模块 cc -c -g foo.c 看到这个例子,各位应该不是很陌生了,前面也已说过,foo.o是我们的目标,foo.c和defs.h是目标所依赖的源文件,而只有一个命令“cc -c - g foo.c”(以Tab键开头)。这个规则告诉我们两件事: 1. 文件的依赖关系,foo.o依赖于foo.c和defs.h的文件,如果foo.c 和defs.h的文件日期要比foo.o文件日期要新,或是foo.o不存 在,那么依赖关系发生。 2. 如果生成(或更新)foo.o文件。也就是那个cc命令,其说明 了,如何生成foo.o这个文件。(当然foo.c文件include了defs.h 文件) 3.2 规则的语法 targets : prerequisites command

... 或是这样: targets : prerequisites ; command command ... targets是文件名,以空格分开,可以使用通配符。一般来说,我们的目标基本上是一个文件,但也有可能是多个文件。 command是命令行,如果其不与“target:prerequisites”在一行,那么,必须以[Tab键]开头,如果和prerequisites在一行,那么可以用分号做为分隔。(见上) prerequisites也就是目标所依赖的文件(或依赖目标)。如果其中的某个文件要比目标文件要新,那么,目标就被认为是“过时的”,被认为是需要重生成的。这个在前面已经讲过了。 如果命令太长,你可以使用反斜框(‘\’)作为换行符。make对一行上有多少个字符没有限制。规则告诉make两件事,文件的依赖关系和如何成成目标文件。 一般来说,make会以UNIX的标准Shell,也就是/bin/sh来执行命令。3.3 在规则中使用通配符 如果我们想定义一系列比较类似的文件,我们很自然地就想起使用通配符。make支持三各通配符:“*”,“?”和“[...]”。这是和Unix的B-Shell是相同的。 波浪号(“~”)字符在文件名中也有比较特殊的用途。如果是“~/test”,这就表示当前用户的$HOME目录下的test目录。而“~hchen/test”则表示用户hchen的宿主目录下的test目录。(这些都是Unix下的小知识了,make也支持)而在Windows或是MS-DOS下,用户没有宿主目录,那么波浪号所指的目录则根据环境变量“HOME”而定。

跟我一起写Makefile(可以注释版)

跟我一起写 Makefile 作者:陈皓 整理:祝冬华

第一部分、概述 (6) 第二部分、关于程序的编译和链接 (6) 第三部分、Makefile 介绍 (7) 一、Makefile的规则 (7) 二、一个示例 (8) 三、make是如何工作的 (9) 四、makefile中使用变量 (10) 五、让make自动推导 (11) 六、另类风格的makefile (12) 七、清空目标文件的规则 (13) 第四部分、Makefile 总述 (13) 一、Makefile里有什么? (13) 1、显式规则。 (14) 2、隐晦规则。 (14) 3、变量的定义。 (14) 4、文件指示。 (14) 5、注释。 (14) 二、Makefile的文件名 (15) 三、引用其它的Makefile (15) 四、环境变量 MAKEFILES (16) 五、make的工作方式 (16) 第五部分、书写规则 (17) 一、规则举例 (17) 二、规则的语法 (17) 三、在规则中使用通配符 (18) 四、文件搜寻 (19) 五、伪目标 (20) 六、多目标 (22) 七、静态模式 (22) 八、自动生成依赖性 (24) 第六部分书写命令 (25) 一、显示命令 (26) 二、命令执行 (26) 三、命令出错 (27) 四、嵌套执行make (28) 五、定义命令包 (30) 第七部分使用变量 (30) 一、变量的基础 (31) 二、变量中的变量 (32) 三、变量高级用法 (34) 四、追加变量值 (37) 五、override 指示符 (37) 六、多行变量 (38)

八、目标变量 (39) 九、模式变量 (40) 第八部分使用条件判断 (40) 一、示例 (40) 二、语法 (42) 第九部分使用函数 (43) 一、函数的调用语法 (44) 二、字符串处理函数 (44) 1、subst (44) 2、patsubst (45) 3、strip (45) 4、findstring (46) 5、filter (46) 6、filter-out (46) 7、sort (47) 8、word (47) 9、wordlist (47) 10、words (47) 11、firstword (48) 12、字符串函数实例 (48) 三、文件名操作函数 (48) 1、dir (48) 2、notdir (48) 3、suffix (49) 4、basename (49) 5、addsuffix (49) 6、addprefix (49) 7、join (50) 四、foreach 函数 (50) 五、if 函数 (50) 六、call函数 (51) 七、origin函数 (51) “undefined” (52) “default” (52) “file” (52) “command line” (52) “override” (52) “automatic” (52) 八、shell函数 (53) 九、控制make的函数 (53) 1、error (53) 2、warning (54) 第十部分 make 的运行 (54)

makefile 中 $@ $^ % 使用

makefile 中$@ $^ %< 使用 https://www.wendangku.net/doc/aa15815216.html,/kesaihao862/article/details/7332528 这篇文章介绍在LINUX下进行C语言编程所需要的基础知识。在这篇文章当中,我们将会学到以下内容:源程序编译Makefile的编写程序库的链接程序的调试头文件和系统求助1.源程序的编译在Linux下面,如果要编译一个C语言源程序,我们要使用GNU的gcc编译器。下面我们以一个实例来说明如何使用gcc编译器。假设我们有下面一个非常简单的源程序(hello.c):int main(int argc,char **argv){printf("Hello Linux\n");}要编译这个程序,我们只要在命令行下执行:gcc -o hello hello.cgcc 编译器就会为我们生成一个hello的可执行文件。执行./hello就可以看到程序的输出结果了。命令行中gcc表示我们是用gcc来编译我们的源程序,-o 选项表示我们要求编译器给我们输出的可执行文件名为hello 而hello.c是我们的源程序文件。gcc编译器有许多选项,一般来说我们只要知道其中的几个就够了。-o 选项我们已经知道了,表示我们要求输出的可执行文件名。-c选项表示我们只要求编译器输出目标代码,而不必要输出可执行文件。-g选项表示我们要求编译器在编译的时候提供我们以后对程序进行调试的信息。知道了这三个选项,我

们就可以编译我们自己所写的简单的源程序了,如果你想要知道更多的选项,可以查看gcc的帮助文档,那里有着许多对其它选项的详细说明。2.Makefile的编写假设我们有下面这样的一个程序,源代码如下:/* main.c */#include "mytool1.h"#include "mytool2.h" int main(int argc,char **argv){mytool1_print("hello");mytool2_print("hello");}/* mytool1.h */ #ifndef _MYTOOL_1_H#define _MYTOOL_1_Hvoid mytool1_print(char *print_str);#endif/* mytool1.c */#include "mytool1.h"void mytool1_print(char *print_str){printf("This is mytool1 print %s\n",print_str);}/* mytool2.h */#ifndef _MYTOOL_2_H#define _MYTOOL_2_Hvoid mytool2_print(char *print_str);#endif/* mytool2.c */#include "mytool2.h"void mytool2_print(char *print_str){printf("This is mytool2 print %s\n",print_str);}当然由于这个程序是很短的我们可以这样来编译gcc -c main.cgcc -c mytool1.cgcc -c mytool2.cgcc -o main main.o mytool1.o mytool2.o这样的话我们也可以产生main程序,而且也不时很麻烦。但是如果我们考虑一下如果有一天我们修改了其中的一个文件(比如说mytool1.c)那么我们难道还要重新输入上面的命令?也许你会说,这个很容易解决啊,我写一个SHELL脚本,让她帮我去完成不就可以了。是的对于这个程序来说,是可

C++项目的Makefile编写

一个C++项目的Makefile编写-Tony与Alex的对话系列- - Tony : Hey Alex, How are you doing? Alex : 不怎么样。(显得很消沉的样子) Tony : Oh , Really ? What is the matter? Alex : 事情是这样的。最近有一个Unix下的C++项目要求我独自完成,以前都是跟着别人做,现在让自己独立完成,还真是不知道该怎么办,就连一个最简单的项目的Makefile都搞不定。昨晚看了一晚上资料也没有什么头绪。唉!! Tony : 别急,我曾经有一段时间研究过一些关于Makefile的东西,也许能帮得上忙,来,我们一起来设计这个项目的Makefile。 Alex : So it is a deal。(一言为定) Tony : 我们现在就开始吧,给我拿把椅子过来。 (Tony坐在Alex电脑的旁边) Tony : 把你的项目情况大概给我讲讲吧。 Alex : No Problem ! 这是一个“半成品”项目,也就是说我将提供一个开发框架供应用开发人员使用,一个类似MFC的东西。 Tony : 继续。 Alex : 我现在头脑中的项目目录结构是这样的: APL (Alex's Programming Library) -Make.properties -Makefile(1) -include //存放头文件 -Module1_1.h -Module1_2.h -Module2_1.h -Module2_2.h -src //存放源文件 -Makefile(2) -module1 -Module1_1.cpp -Module1_2.cpp -Makefile(3) -module2 -Module2_1.cpp -Module2_2.cpp -Makefile(3) -... -lib //存放该Project依赖的库文件,型如libxxx.a -dist //存放该Project编译连接后的库文件libapl.a -examples //存放使用该“半成品”搭建的例子应用的源程序 Makefile(4)

怎样使用Makefile

Mak k e f ile 跟我一 我一起起写Ma 陈皓 (CSDN) 概 述 什么是makefile?或许很多Winodws的程序员都不知道这个东西,因为那些Windows 的IDE都为你做了这个工作,但我觉得要作一个好的和professional的程序员,makefile还是要懂。这就好像现在有这么多的HTML的编辑器,但如果你想成为一个专业人士,你还是要了解HTML的标识的含义。特别在Unix下的软件编译,你就不能不自己写makefile了,会不会写makefile,从一个侧面说明了一个人是否具备完成大型工程的能力。 因为,makefile关系到了整个工程的编译规则。一个工程中的源文件不计数,其按类型、功能、模块分别放在若干个目录中,makefile定义了一系列的规则来指定,哪些文件需要先编译,哪些文件需要后编译,哪些文件需要重新编译,甚至于进行更复杂的功能 操作,因为makefile就像一个Shell脚本一样,其中也可以执行操作系统的命令。 makefile带来的好处就是——“自动化编译”,一旦写好,只需要一个make命令,整个工程完全自动编译,极大的提高了软件开发的效率。make是一个命令工具,是一个 解释makefile中指令的命令工具,一般来说,大多数的IDE都有这个命令,比如:Delphi的make,Visual C++的nmake,Linux下GNU的make。可见,makefile都成为了一种在工程方面的编译方法。 现在讲述如何写makefile的文章比较少,这是我想写这篇文章的原因。当然,不同产商的make各不相同,也有不同的语法,但其本质都是在“文件依赖性”上做文章,这里,我仅对GNU的make进行讲述,我的环境是RedHat Linux 8.0,make的版本是3.80。必竟,这个make是应用最为广泛的,也是用得最多的。而且其还是最遵循于IEEE 1003.2-1992 标准的(POSIX.2)。 在这篇文档中,将以C/C++的源码作为我们基础,所以必然涉及一些关于C/C++的编译的知识,相关于这方面的内容,还请各位查看相关的编译器的文档。这里所默认的编译器是UNIX下的GCC和CC。 关于程序的编译和链接 在此,我想多说关于程序编译的一些规范和方法,一般来说,无论是C、C++、还是pas,首先要把源文件编译成中间代码文件,在Windows下也就是 .obj 文件,UNIX下是 .o 文件,即 Object File,这个动作叫做编译(compile)。然后再把大量的Object File 合成执行文件,这个动作叫作链接(link)。

如何编写Makefile

概述 —— 什么是makefile?或许很多Winodws的程序员都不知道这个东西,因为那些Windows的IDE 都为你做了这个工作,但我觉得要作一个好的和professional的程序员,makefile还是要懂。这就好像现在有这么多的HTML的编辑器,但如果你想成为一个专业人士,你还是要了解HTML的标识的含义。特别在Unix下的软件编译,你就不能不自己写makefile了,会不会写makefile,从一个侧面说明了一个人是否具备完成大型工程的能力。 因为,makefile关系到了整个工程的编译规则。一个工程中的源文件不计数,其按类型、功能、模块分别放在若干个目录中,makefile定义了一系列的规则来指定,哪些文件需要先编译,哪些文件需要后编译,哪些文件需要重新编译,甚至于进行更复杂的功能操作,因为makefile就像一个Shell脚本一样,其中也可以执行操作系统的命令。 makefile带来的好处就是——“自动化编译”,一旦写好,只需要一个make命令,整个工程完全自动编译,极大的提高了软件开发的效率。make是一个命令工具,是一个解释makefile 中指令的命令工具,一般来说,大多数的IDE都有这个命令,比如:Delphi的make,Visual C++的nmake,Linux下GNU的make。可见,makefile都成为了一种在工程方面的编译方法。 现在讲述如何写makefile的文章比较少,这是我想写这篇文章的原因。当然,不同产商的make各不相同,也有不同的语法,但其本质都是在“文件依赖性”上做文章,这里,我仅对GNU的make进行讲述,我的环境是RedHat Linux 8.0,make的版本是3.80。必竟,这个make 是应用最为广泛的,也是用得最多的。而且其还是最遵循于IEEE 1003.2-1992 标准的(POSIX.2)。 在这篇文档中,将以C/C++的源码作为我们基础,所以必然涉及一些关于C/C++的编译的知识,相关于这方面的内容,还请各位查看相关的编译器的文档。这里所默认的编译器是UNIX 下的GCC和CC。 关于程序的编译和链接 —————————— 在此,我想多说关于程序编译的一些规范和方法,一般来说,无论是C、C++、还是pas,首先要把源文件编译成中间代码文件,在Windows下也就是 .obj 文件,UNIX下是 .o 文件,即 Object File,这个动作叫做编译(compile)。然后再把大量的Object File合成执行文件,这个动作叫作链接(link)。 编译时,编译器需要的是语法的正确,函数与变量的声明的正确。对于后者,通常是你需要告诉编译器头文件的所在位置(头文件中应该只是声明,而定义应该放在C/C++文件中),只要所有的语法正确,编译器就可以编译出中间目标文件。一般来说,每个源文件都应该对应于一个中间目标文件(O文件或是OBJ文件)。

Makefile两个实验教案

Makefile工程管理器 14.1 编写包含多文件的Makefile 【实验内容】 编写一个包含多文件的Makefile。 【实验目的】 通过对包含多文件的Makefile的编写,熟悉各种形式的Makefile,并且进一步加深对Makefile中用户自定义变量、自动变量及预定义变量的理解。 【实验平台】 PC机、CentOS 5 操作系统、gcc等工具。 【实验步骤】 1.用vi在同一目录下编辑两个简单的Hello程序,如下所示: #hello.c #include "hello.h" int main() { printf("Hello everyone!\n"); } #hello.h #include 2.仍在同一目录下用vim编辑Makefile,不使用变量替换,用一个目标体实现(即直接将 hello.c和hello.h编译成hello目标体)。并用make验证所编写的Makefile是否正确。 3.将上述Makefile使用变量替换实现。同样用make验证所编写的Makefile是否正确 4.用编辑另一Makefile,取名为Makefile1,不使用变量替换,但用两个目标体实现(也 就是首先将hello.c和hello.h编译为hello.o,再将hello.o编译为hello),再用make的‘-f’选项验证这个Makefile1的正确性。 5.将上述Makefile1使用变量替换实现 【详细步骤】 1.用vi打开上述两个代码文件‘hello.c’和‘hello.h’ 2.在shell命令行中用gcc尝试编译,使用命令:‘gcc hello.c -o hello’,并运行hello可执 行文件查看结果。 3.删除此次编译的可执行文件:rm –rf hello 4.用vim编辑Makefile,如下所示: hello:hello.c hello.h gcc hello.c -o hello 5.退出保存,在shell中键入:make查看结果 6.再次用vim打开Makefile,用变量进行替换,如下所示: OBJS :=hello.o CC :=gcc hello:$(OBJS) $(CC) $^ -o $@

要了解一个LINUX工程的结构必须看懂Makefile

要了解一个LINUX工程的结构必须看懂Makefile,尤其是顶层的,没办法,UNIX世界就是这么无奈,什么东西都用文档去管理、配置。首先在这方面我是个新手,时间所限只粗浅地看了一些Makefile规则。 以smdk_2410为例,顺序分析Makefile大致的流程及结构如下: 1) Makefile中定义了源码及生成的目标文件存放的目录,目标文件存放目录BUILD_DIR可以通过make O=dir 指定。如果没有指定,则设定为源码顶层目录。一般编译的时候不指定输出目录,则BUILD_DIR为空。其它目录变量定义如下: #OBJTREE和LNDIR为存放生成文件的目录,TOPDIR与SRCTREE为源码所在目录OBJTREE := $(if $(BUILD_DIR),$(BUILD_DIR),$(CURDIR)) SRCTREE := $(CURDIR) TOPDIR := $(SRCTREE) LNDIR := $(OBJTREE) export TOPDIR SRCTREE OBJTREE 2)定义变量MKCONFIG:这个变量指向一个脚本,即顶层目录的mkconfig。 MKCONFIG := $(SRCTREE)/mkconfig export MKCONFIG 在编译U-BOOT之前,先要执行 # make smdk2410_config smdk2410_config是Makefile的一个目标,定义如下: smdk2410_config : unconfig @$(MKCONFIG) $(@:_config=) arm arm920t smdk2410 NULL s3c24x0 unconfig: @rm -f $(obj)include/config.h $(obj)include/config.mk / $(obj)board/*/config.tmp $(obj)board/*/*/config.tmp 显然,执行# make smdk2410_config时,先执行unconfig目标,注意不指定输出目标时,obj,src变量均为空,unconfig下面的命令清理上一次执行make

吐血共享我收集并总结后的makefile的编写规则级方法

makefile关系到了整个工程的编译规则。一个工程中的源文件不计数,其按类型、功能、模块分别放在若干个目录中,makefile定义了一系列的规则来指定,哪些文件需要先编译,哪些文件需要后编译,哪些文件需要重新编译,甚至于进行更复杂的功能操作,因为makefile 就像一个Shell脚本一样,其中也可以执行操作系统的命令。 makefile带来的好处就是——“自动化编译”,一旦写好,只需要一个make命令,整个工程完全自动编译,极大的提高了软件开发的效率。make是一个命令工具,是一个解释makefile 中指令的命令工具。 unix下的中间代码文件是.o文件。 头文件中应该只是声明,而定义应该放在C/C++文件中 makefile的规则是: 1)如果这个工程没有编译过,那么我们的所有C文件都要编译并被链接。 2)如果这个工程的某几个C文件被修改,那么我们只编译被修改的C文件,并链接目标程序。 3)如果这个工程的头文件被改变了,那么我们需要编译引用了这几个头文件的C文件,并链接目标程序。 只要我们的Makefile写得够好,所有的这一切,我们只用一个make命令就可以完成,make 命令会自动智能地根据当前的文件修改的情况来确定哪些文件需要重编译,从而自己编译所需要的文件和链接目标程序。Makefile的规则。 target ... : prerequisites ... command ... ... target也就是一个目标文件,可以是Object File,也可以是执行文件。还可以是一个标签(Label),对于标签这种特性,在后续的“伪目标”章节中会有叙述。 prerequisites就是,要生成那个target所需要的文件或是目标。 command也就是make需要执行的命令。(任意的Shell命令) 这是一个文件的依赖关系,也就是说,target这一个或多个的目标文件依赖于prerequisites 中的文件,其生成规则定义在command中。说白一点就是说,prerequisites中如果有一个以上的文件比target文件要新的话,command所定义的命令就会被执行。这就是Makefile 的规则。也就是Makefile中最核心的内容 .PHONY : clean ,“.PHONY”表示,clean是个伪目标文件。 一般的风格都是: clean: rm edit $(objects)

相关文档