C ++ Primer: source compiler operating instructions


1. Background

  Recently read "C ++ primer Chinese version (5th edition)," the first chapter, written source after source wanted to compare the official example, it found that the documentation is in English, the official record after roughly understand what is how to compile and run it source.

2. relevant information and environment

C ++ primer Official Download Source:
http://www.informit.com/store/c-plus-plus-primer-9780321714114
C ++ first

Download MS Visual Studio 2012

Download version Explanation
GCC 4.7.0 It applies to gcc version 4.7.0 and above
MS Visual Studio 2012 Suitable for VS2012 and above versions
GCC pre-C++ 11 compilers 2012 The following applies to VS2012 version
Microsoft pre-C++ 11 compilers The following applies to 4.7.0 version of gcc

Because the computer VS2017, so you can directly download MS Visual Studio 2012. The following compile and run run in VS environment.

MS Visual Studio 2012 directory

MS Visual Studio 2012 File Directory Description:

  1. Folder 1 to Folder 19 source on behalf of C ++ primer19 chapters.
  2. Version_test.h source is required header part, such as a folder on Sales_data.h 1 reference Version_test.h.
  3. CompilerNotes.pdf and Visual Studio 2012 README.pdf overall documentation.
  4. runpgms.bat 1 traverse to the folder from the folder 19, runp.bat to jump to the internal folder, performed inside runpgms.bat.
  5. nmake makefile and MS_makefile_template is the default executable file, compiled for all the source code directory, and generate the corresponding .exe file.

Section 1 of the folder directory

Chapter 1 folder directory Description:

  1. data folder cpp file storage section performs data input required.
  2. The first chapter is all .cpp file source.
  3. Sales_item.h is part of the cpp file to perform the required header files.
  4. runpgms.bat for performing .exe file, it will automatically import data in the data folder, and outputs the result. If this folder without a corresponding .exe file, runpgms.bat fails.
  5. README.txt documentation section
  6. nmake makefile is the default executable file, compiled for all source code under this directory, and generate the corresponding .exe file.

Thus all the folder with nmake makefile is run, so you need to create compile.bat VS to call the vsvars32.bat nmake to build the operating environment. When you double-click compile.bat, the system opens cmd, all compilation commands are executed on it.
compile.bat Code:

@echo off
call "C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\Tools\vsvars32.bat"
cmd

Tip: Use vsvars32.bat address to be modified, vsvars32.bat As used herein, the following address:

vsvars32.bat address

3. Compile and run

3.1 compile and run single file

  The compile.bat copy and paste to the directory where the file, double click to run compile.bat, the pop-up window, enter cmd compile command. Compile command as follows, where / EHsc start of exception, / nologo prohibited claims.

// add.cpp编译成add.obj
cl /EHsc /nologo /c add.cpp
// add.obj编译成add.exe
cl /EHsc /nologo add.obj
// 一步到位,add.cpp编译成add.exe
cl /EHsc /nologo add.cpp

Multistep compilation:
Multistep compilation
single-step compile:
Compilation step
compile the results:
Compilation results

3.2 file redirection

  You can run .exe, without having to enter through the redirection file, folder data directly to the file as input, and outputs the calculation result.

data / add_item data:

0-201-78345-X 3 20.00
0-201-78345-X 2 25.00

Compile command:

// add_item.cpp编译成add_item.exe
cl /EHsc /nologo add_item.cpp
// 自动导入data/add_item计算结果
add_item.exe <data/add_item

Compile the results:
operation result

Note: If the following compilation errors, the reason cited is add_item.cpp #include "Sales_item.h", while Sales_item.h and references #include "Version_test.h". Therefore, you need to add "/ I ..." compilation command, is a reference to the parent directory, that is the main directory. Version_test.h main directory can also be copied to the catalog.

Compile error

The modified compiler command:

cl /EHsc /nologo /I.. add_item.cpp

Compile the results:
File Directory

  When a folder corresponding to all .cpp are present .exe, a plurality of calculation results .exe files can redirect all runpgms.bat output. If cmd flashed, need runpgms.bat last line with "pause" to pause.
runpgms.bat

runpgms.bat before the amendment:

echo on
add < data/add 
add_item < data/add_item
add_item2 < data/add_item
avg_price < data/book_sales
item_io < data/book_sales 
occurs < data/occurs
mysum < data/mysum

for %%i in ( for_ex forcount main_only whilecount) do %%i

runpgms.bat modified:

echo on
add < data/add 
add_item < data/add_item
add_item2 < data/add_item
avg_price < data/book_sales
item_io < data/book_sales 
occurs < data/occurs
mysum < data/mysum

for %%i in ( for_ex forcount main_only whilecount) do %%i
pause

operation result

More than 3.3 compile and run the file

  If a file that compile and run a file too much trouble, you can use compile.bat run makefile, compile all the source code to achieve the following three commands, the clearance of .obj and .exe.

// 编译目录下所有源码
nmake
// 删除文件夹下所有.obj
nmake clean
// 删除文件夹下所有.obj和.exe
namke clobber

nmake

nmake execution results

nmake clean

nmake clean execution results

namke clobber

namke clobber the results

Note: When you compile and run inits.cpp in Section 2 folder, there will be "error C2732: Link Specification and" lround "early specification of conflict" error, which need to Version_test.h the "#ifndef LROUND" amended to "#ifdef LROUND".

inits.cpp directory

image.png

before fixing:

#ifndef LROUND
inline long lround(double d)
{
    return (d >= 0) ? long(d + 0.5) : long(d - 0.5);
}
#endif

Modified:

// #ifndef LROUND
#ifdef LROUND
inline long lround(double d)
{
    return (d >= 0) ? long(d + 0.5) : long(d - 0.5);
}
#endif

4. Summary

  1. Compiler environment is not important, use gcc, g ++ or cl can, it is important to write code and compile process.
  2. Learn to use file redirection and makefile, and make programming more efficient.

5. References

  1. [Tips] 2018-08-08 about c ++ primer section 1.6 Sales_item.h header files related problems and solutions (free) in the community edition VS2015
  2. Run NMAKE
Published 77 original articles · won praise 25 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_34801642/article/details/103827224