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
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 File Directory Description:
- Folder 1 to Folder 19 source on behalf of C ++ primer19 chapters.
- Version_test.h source is required header part, such as a folder on Sales_data.h 1 reference Version_test.h.
- CompilerNotes.pdf and Visual Studio 2012 README.pdf overall documentation.
- runpgms.bat 1 traverse to the folder from the folder 19, runp.bat to jump to the internal folder, performed inside runpgms.bat.
- nmake makefile and MS_makefile_template is the default executable file, compiled for all the source code directory, and generate the corresponding .exe file.
Chapter 1 folder directory Description:
- data folder cpp file storage section performs data input required.
- The first chapter is all .cpp file source.
- Sales_item.h is part of the cpp file to perform the required header files.
- 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.
- README.txt documentation section
- 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:
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:
single-step compile:
compile the 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:
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.
The modified compiler command:
cl /EHsc /nologo /I.. add_item.cpp
Compile the results:
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 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
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
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".
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
- Compiler environment is not important, use gcc, g ++ or cl can, it is important to write code and compile process.
- Learn to use file redirection and makefile, and make programming more efficient.