《C++ Primer》随附代码的使用方法

(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


The preprocessor variables that correspond to unimplemented features are defined in Version test.h

关于此问题,有专门文档说明,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

基础知识:

makefiles 

how to run nmake in windows (thx to  Validus Oculus @ stackoverflow. )
  1. 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
  2. open a command line and run vsdevcmd.bat (VS早前版本可能是vsvars32.bat) to open ”Visual Studio 2017 RC Developer Command Prompt v15.0.26206.0
  3. 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:
$ 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.











  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值