⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chapter 11 preprocessor directives -- valvano.htm

📁 关于如何利用C++在嵌入式系统中应用
💻 HTM
📖 第 1 页 / 共 2 页
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0058)http://www.ece.utexas.edu/~valvano/embed/chap11/chap11.htm -->
<HTML><HEAD><TITLE>Chapter 11: Preprocessor Directives -- Valvano</TITLE>
<META http-equiv=content-type content=text/html;charset=iso-8859-1>
<META content="MSHTML 5.50.3825.1300" name=GENERATOR>
<META 
content="StarMax HD:Microsoft Office 98:Templates:Web Pages:Blank Web Page" 
name=Template></HEAD>
<BODY vLink=#800080 link=#0000ff>
<P><!--Developing Embedded Software in C using ICC11/ICC12/Hiware by Jonathan W. Valvano--><B><FONT 
face="Times New Roman,Times" size=4>Chapter 11: Preprocessor 
Directives</FONT></B></P>
<P><B><I><FONT face=Helvetica,Arial>What's in Chapter 11?</FONT></I></B></P>
<DIR>
<P><A 
href="http://www.ece.utexas.edu/~valvano/embed/chap11/chap11.htm#MACRO">Using 
#define to create macros</A><FONT face=Monaco><BR></FONT><A 
href="http://www.ece.utexas.edu/~valvano/embed/chap11/chap11.htm#DEFINITIONS">Using 
#ifdef to implement conditional compilation</A><FONT face=Monaco><BR></FONT><A 
href="http://www.ece.utexas.edu/~valvano/embed/chap11/chap11.htm#INTERPRETER">Using 
#include to load other software modules</A><FONT face=Monaco><BR></FONT><A 
href="http://www.ece.utexas.edu/~valvano/embed/chap11/chap11.htm#PRAGMA">Using 
#pragma to write interrupt software</A></P></DIR>
<P><FONT face="Times New Roman,Times">C compilers incorporate a preprocessing 
phase that alters the source code in various ways before passing it on for 
compiling. Four capabilities are provided by this facility in C. They are: 
</FONT></P>
<UL>
  <P><FONT face="Times New Roman,Times">macro processing <BR>inclusion of text 
  from other files <BR>conditional compiling <BR>in-line assembly language 
  </FONT></P></UL>
<P><FONT face="Times New Roman,Times">The preprocessor is controlled by 
directives which are not part of the C language proper. Each directive begins 
with a <B>#</B>character and is written on a line by itself. Only the 
preprocessor sees these directive lines since it deletes them from the code 
stream after processing them. </FONT></P>
<P><FONT face="Times New Roman,Times">Depending on the compiler, the 
preprocessor may be a separate program or it may be integrated into the compiler 
itself. C has an integrated preprocessor that operates at the front end of its 
single pass algorithm. </FONT></P>
<P><FONT face="Times New Roman,Times"><B><A 
name=MACRO></A></B></FONT><B><I><FONT face=Helvetica,Arial>Macro 
Processing</FONT></I></B></P>
<P><FONT face="Times New Roman,Times">We use macros for three reasons. 1) To 
save time we can define a macro for long sequences that we will need to repeat 
many times. 2) To clarify the meaning of the software we can define a macro 
giving a symbolic name to a hard-to-understand sequence. The I/O port #define 
macros are good examples of this reason. 3) To make the software easy to change, 
we can define a macro such that changing the macro definition, automatically 
updates the entire software. </FONT></P>
<UL>
  <P><CODE>#define Name CharacterString?... </CODE></P></UL>
<P><FONT face="Times New Roman,Times">define names which stand for arbitrary 
strings of text. After such a definition, the preprocessor replaces each 
occurrence of <I>Name</I> (except in string constants and character constants) 
in the source text with <I>CharacterString?...</I>. As C implements this 
facility, the term macro is misleading, since parameterized substitutions are 
not supported. That is, <I>CharacterString?...</I> does not change from one 
substitution to another according to parameters provided with <I>Name</I> in the 
source text. </FONT></P>
<P><FONT face="Times New Roman,Times">C accepts macro definitions only at the 
global level. </FONT></P>
<P><FONT face="Times New Roman,Times">The <I>Name</I> part of a macro definition 
must conform to the standard C naming conventions as described in <A 
href="http://www.ece.utexas.edu/~valvano/embed/chap2/chap2.htm">Chapter 2</A>. 
<I>CharacterString?...</I> begins with the first printable character following 
<I>Name</I> and continues through the last printable character of the line or 
until a comment is reached. </FONT></P>
<P><FONT face="Times New Roman,Times">If <I>CharacterString?...</I> is missing, 
occurrences of <I>Name</I> are simply squeezed out of the text. Name matching is 
based on the whole name (up to 8 characters); part of a name will not match. 
Thus the directive </FONT></P>
<UL>
  <P><CODE>#define size 10</CODE></P></UL>
<P><FONT face="Times New Roman,Times">will change</FONT></P>
<UL>
  <P><CODE>short data[size];</CODE></P></UL>
<P><FONT face="Times New Roman,Times">into</FONT></P>
<UL>
  <P><CODE>short data[10];</CODE></P></UL>
<P>&nbsp;</P>
<P><FONT face="Times New Roman,Times">but it will have no effect on</FONT></P>
<UL>
  <P><CODE>short data[size1];</CODE></P></UL>
<P><FONT face="Times New Roman,Times">Replacement is also performed on 
subsequent <B>#define</B> directives, so that new symbols may be defined in 
terms of preceding ones. </FONT></P>
<P><FONT face="Times New Roman,Times">The most common use of <B>#define</B> 
directives is to give meaningful names to constants; i.e., to define so called 
<I>manifest constants</I>. However, we may replace a name with anything at all, 
a commonly occurring expression or sequence of statements for instance. To 
disable interrupt during a critical section we could implement.</FONT></P>
<DIR>
<P><CODE>#define START_CRITICAL asm(" tpa\n staa %SaveSP\n sei")<BR>#define 
END_CRITICAL asm( ldaa %SaveSP\n tap")<BR>void function(void) {unsigned char 
SaveSP;<BR>&nbsp;&nbsp;&nbsp;&nbsp;START_CRITICAL; /* make atomic, entering 
critical section */<BR>&nbsp;&nbsp;&nbsp;&nbsp; /* we have exclusive access to 
global variables */<BR>&nbsp;&nbsp;&nbsp;&nbsp;END_CRITICAL; /* end critical 
section */<BR>}</CODE></P></DIR>
<ADDRESS><FONT face="Times New Roman,Times">Listing 11.1: Example of 
#define</FONT></ADDRESS>
<P>&nbsp;</P>
<P><FONT face="Times New Roman,Times"><B><A name=CONDITION></A>Conditional 
Compiling</B></FONT></P>
<P><FONT face="Times New Roman,Times">This preprocessing feature lets us 
designate parts of a program which may or may not be compiled depending on 
whether or not certain symbols have been defined. In this way it is possible to 
write into a program optional features which are chosen for inclusion or 
exclusion by simply adding or removing <B>#define </B>directives at the 
beginning of the program. </FONT></P>
<P><FONT face="Times New Roman,Times">When the preprocessor encounters 
</FONT></P>
<UL>
  <P><CODE>#ifdef Name</CODE><FONT face="Times New Roman,Times"> </FONT></P></UL>
<P><FONT face="Times New Roman,Times">it looks to see if the designated name has 
been defined. If not, it throws away the following source lines until it finds a 
matching </FONT></P>
<UL>
  <P><CODE>#else </CODE></P></UL>
<P><FONT face="Times New Roman,Times">or </FONT></P>
<UL>
  <P><CODE>#endif </CODE></P></UL>
<P><FONT face="Times New Roman,Times">directive. The <B>#endif </B>directive 
delimits the section of text controlled by <B>#ifdef</B>, and the <B>#else</B> 
directive permits us to split conditional text into true and false parts. The 
first part (<B>#ifdef...#else</B>) is compiled only if the designated name is 
defined, and the second (<B>#else...#endif</B>) only if it is not defined. 
</FONT></P>
<P><FONT face="Times New Roman,Times">The converse of <B>#ifdef</B> is the 
</FONT></P>
<UL>
  <P><CODE>#ifndef Name </CODE></P></UL>
<P><FONT face="Times New Roman,Times">directive. This directive also takes 
matching <B>#else</B> and <B>#endif</B> directives. In this case, however, if 
the designated name is not defined, then the first (<B>#ifndef...#else</B>) or 
only (<B>#ifndef...#endif</B>) section of text is compiled; otherwise, the 
second (<B>#else...#endif</B>), if present, is compiled. </FONT></P>
<P><FONT face="Times New Roman,Times">Nesting of these directives is allowed; 
and there is no limit on the depth of nesting. It is possible, for instance, to 
write something like </FONT></P>
<P><CODE>#ifdef ABC<BR>... /* ABC */<BR>#ifndef DEF<BR>... /* ABC and not DEF 
*/<BR>#else<BR>... /* ABC and DEF */<BR>#endif<BR>... /* ABC */<BR>#else<BR>... 
/* not ABC */<BR>#ifdef HIJ<BR>... /* not ABC but HIJ */<BR>#endif<BR>... /* not 
ABC */<BR>#endif</CODE></P>
<ADDRESS><FONT face="Times New Roman,Times">Listing 11.2: Examples on 
conditional compilation</FONT></ADDRESS>
<P><FONT face="Times New Roman,Times">where the ellipses represent conditionally 
compiled code, and the comments indicate the conditions under which the various 
sections of code are compiled.</FONT></P>
<P><FONT face="Times New Roman,Times">A good application of conditional 
compilation is inserting debugging instrumemts. In this example the only purpose 
of writing to PORTC is assist in performance debugging. Once the system is 
debugged,we can remove all the debugging code, simply by deleting the 
</FONT><CODE>#define Debug 1</CODE><FONT face="Times New Roman,Times"> 
line.</FONT></P>
<P><CODE>#define Debug 1<BR>int Sub(int j){ int i;<BR>#ifdef 
Debug<BR>&nbsp;&nbsp;&nbsp;&nbsp;PORTC|=0x01; /* PC0 set when Sub is entered 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -