(Source: C++ Primer, Fifth Edition Visual Studio 2012 Code Distribution README )
Conditional Compilation
第5版包含C++11新特性的介绍,但VS 2012还不完全支持C++11,于是作者通过条件编译,规避了使用了C++11特性的代码不能在VS2012上顺利编译的问题。
示意代码:
#ifdef VARIABLE_NAME
code1 // this code will be compiled if VARIABLE_NAME has been defined
#else
code2 // this code will be compiled if VARIABLE_NAME has not been defined
#endif
关于此问题,有专门文档说明,CompilerNotes.pdf
鉴于我用的是VS2017,基本不存在条件编译的问题。因此具体细节等学到条件编译(Section 6.5.3 )的时候再回头看。
Building Executables
资源中随附了makefile方便使用nmake统一编译。# Windows machines
> nmake # compiles all the programs
> nmake clean # removes all the object files and stackdumps
> nmake clobber # removes executable, object and stackdump files
基础知识:
how to run nmake in windows (thx to
Validus Oculus @ stackoverflow. )
- add path of your visual studio common tools(i.e.C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\Tools) into the system path
- open a command line and run vsdevcmd.bat (VS早前版本可能是vsvars32.bat) to open ”Visual Studio 2017 RC Developer Command Prompt v15.0.26206.0“
- call nmake
或者在VS下,Tools->Visual Studio Command Prompt
MAKEFILES IN WINDOWS
An introduction to Makefiles for Visual Studio developers
The Microsoft Program Maintenance Utility (NMAKE.EXE) is a command-line tool included with Visual Studio that builds projects based on commands that are contained in a description file.
补充:
Walkthrough: Compiling a Native C++ Program on the Command Line
Input and Output
Each Chapter subdirectory contains a README.txt that explains the input, if any, expected by each executable file.Sample Data Files
用数据文件代替手动输入
add_item < data\add_item
(C++ Primer p22)
USINGFILEREDIRECTION
It can be tedious to repeatedly type these transactions as input to the programs you are testing. Most operating systems support file redirection, which lets us associate a named file with the standard input and the standard output:
It can be tedious to repeatedly type these transactions as input to the programs you are testing. Most operating systems support file redirection, which lets us associate a named file with the standard input and the standard output:
$ addItems <infile >outfile
Assuming
$
is the system prompt and our addition program has been compiled into an executable file named
addItems.exe
(or
ddItems
on UNIX systems), this command will read transactions from a file named
infile
and write its output to a file named
outfile
in the current directory.
运行需要文件名参数的程序
# word_transform takes two files, samples are in the data directory
# this execution uses the file data\rules to transform the text in data\text
word_transform data\rules data\text
Running the Programs
批处理可以统一执行文件夹中的所有程序
Each directory contains a simple shell script namedrunpgms.batthat runs the executables made in that directory.