(Reproduced from: http://hi.baidu.com/joyjjjz/blog/item/e9d2347ab62671e92f73b305.html)
Detailed explanation of VC Compiler
You may have been using VC to develop software, but you may not be familiar with this compiler. There are many reasons. In most cases, we only "use" it, instead of "Understanding" it. Because it is just a tool, we would rather focus more on the C ++ language and software design. We are used to this "pattern": creating a project, writing code, compiling, and debugging. However, the so-called: "to do good deeds, you must first sharpen your tools ". If we are better at the VC development environment, can we be more comfortable?
Let's talk less. Let's take a look at the VC processing process, which is roughly divided into two steps: Compilation and connection. After the source file is compiled and translated, all the. objfile named .objfile and the. libfile are connected to the. exe file or. dll file. Next, we will discuss the details of these two steps.
Compilation parameter settings. The Project-> Settings-> C/C ++ page of the VC menu is used. We can see the content in Project Options at the bottom of this page, which is generally as follows:
/Nologo/MDd/W3/Gm/GX/ZI/Od/D "WIN32"/D "_ DEBUG"/D "_ WINDOWS"/D "_ AFXDLL"/D" _ M
BCS "/Fp" Debug/WritingDlgTest. pch "/Yu" stdafx. h "/Fo" Debug/"/Fd" Debug/"/FD/GZ/c
For the meaning of each parameter, refer to Msdn. For example, the/nologo indicates that these settings are not displayed in the output window during compilation (we can remove this parameter to see the effect. Generally, we do not directly modify these settings, but do it through the items in the top Category on this page.
1) General: some General settings. Warning level is used to control Warning information, where Level 1 is the most serious level; Warnings as errors treats Warning information as an error; Optimizations is code optimization, you can perform more detailed settings in the Optimizations item of Category; Generate browse info is used to Generate. you can set more parameters in the Listing Files of Category. Debug info, generate debugging information: None, do not generate any debugging information (compilation is faster); Line Numbers Only, Only generate debugging information of global and external symbols. OBJ file or. EXE file to reduce the size of the target file; C 7.0-Compatible, record all the symbols used by the debugger. OBJ file and. EXE file; Program Database, create. all debugging information is recorded in the PDB file; Program Database for "Edit & Continue", created. the PDB file records all debugging information and supports editing during debugging.
2) C ++ Language: pointer_to_member representation is used to set the class definition/reference sequence. Generally, Best-Case Always indicates that the class must have been defined before the class is referenced. Enable Exception Handling, synchronization exception handling; Enable Run-Time Type Information forces the compiler to add the code to check the object Type at runtime; Disable Construction Displacements, set the class constructor/destructor to call virtual functions.
3) Code Generation: Processor indicates Code command optimization, which can be 80386, 80486, Pentium, Pentium Pro, or Blend. Use run-time library is used to specify the Runtime library (single thread or multi-thread, Debug version or Release version) used when the program is running. One principle is, A process should not use several versions of runtime libraries at the same time. Single-Threaded, static connection LIBC. LIB Library; Debug Single-Threaded, static connection LIBCD. LIB Library; Multithreaded, static connection LIBCMT. LIB Library; Debug Multithreaded, static connection LIBCMTD. LIB Library; Multithreaded DLL, dynamic connection to MSVCRT. DLL library; Debug Multithreaded DLL, dynamic connection to MSVCRTD. DLL library. Multi-threaded calling is not supported when a single-threaded database is connected. connecting to a multi-threaded database requires the creation of multi-threaded applications.
Calling convention can be used to set the call conventions, including __cdecl, _ fastcall, and _ stdcall. The main difference between various call conventions is that when a function is called, the function parameter is pushed from left to right into the stack or from right to left into the stack; when the function is returned, the parameters pushed into the stack are cleared by the caller of the function or by the function itself; and the name modification of the function name during compilation (you can see various naming methods through Listing Files ). Struct member alignment is used to specify the size of the member variables in the data structure in the memory. The data access speed varies with the alignment mode based on the number of digits in the computer data bus. This parameter is especially important for applications such as packet network transmission. It is not about the access speed, but about the precise definition of data bit. It is generally specified by # pragma pack in the program.
4) Customize: Disable Language Extensions, indicating that the Language extension made by Microsoft for Standard C is not used; Eliminate Duplicate Strings, mainly used for string optimization (put the string in the slow-filling pool to save space ), with this parameter
Char * sBuffer = "This is a character buffer ";
Char * tBuffer = "This is a character buffer ";
SBuffer and tBuffer point to the same memory space. Enable Function-Level Linking tells the compiler to compile each Function in the packaging format. Enables minimal rebuild saves the association information. the IDB file enables the compiler to recompile only the source files modified by the latest class definition to improve the Compilation speed. Enable Incremental Compilation also passes. the Information stored in the IDB file. Only the latest modified functions are recompiled. Suppress Startup Banner and Information Messages are used to control whether the parameters are output in the output window.
5) The Listing Files: Generate browse info function has been mentioned above. You can perform more settings here. Exclude Local Variables from Browse Info indicates whether to put information about Local Variables in the. SBR file. The Listing file type can be used to set the content of the generated list information file: Assembly-Only Listing Only generates the Assembly code file (. ASM extension); Assembly With Machine Code to generate Machine Code and Assembly Code files (. COD extension); Assembly With Source Code to generate Source Code and Assembly Code files (. ASM extension); Assembly, Machine Code, and Source generate Machine Code, Source Code, and Assembly Code file (. COD extension ). The Listing file name is the path of the generated information file. Generally, it is in the Debug or Release directory. The generated file name automatically retrieves the file name of the source file.
6) Optimizations: code optimization settings. You can select Maximize Speed to generate the fastest code, mini-Mize Size to generate the smallest program, or custom Optimization of mimize. Customized content includes:
Assume No Aliasing, No alias (Increase speed );
Assume Aliasing implements SS Function CILS. Only aliases are not used inside the Function;
Global Optimizations, Global optimization, such as storing frequently used variables using registers or computing Optimization in a loop, such
I =-100;
While (I <0) {I + = x + y ;}
Will be optimized
I =-100;
T = x + y;
While (I <0) {I + = t ;}
Generate Intrinsic Functions, replace some function calls with internal Functions (increase the speed );
Improve Float Consistency, optimized for floating point operations;
Favor Small Code, Program (exe or dll) Size Optimization takes precedence over Code Speed Optimization;
Favor Fast Code, Program (exe or dll) Code Speed Optimization takes precedence over Size Optimization;
Frame-Pointer Omission, which does not use Frame pointers to increase the function call speed;
Full Optimization combines several parameters to generate the fastest program code.
Inline function expansion, three optimizations of inline function Extension (using Inline can save the overhead of function calling and speed up the program): Disable does not use inline; Only _ Inline, only the inline or _ inline mark before function definition uses inline; Any Suitable, except for the functions marked by inline or _ inline, the compiler "thinks" That inline functions should be used, use inline.
7) Precompiled Headers: Precompiled header file settings. Pre-compilation can improve the speed of repeated compilation. Generally, VC adds some public and slightly changed header files (such as afxwin. h) To stdafx. h, this part of the Code does not have to be re-compiled every time (unless it is Rebuild All ).
8) Preprocessor: Pre-compiled. Some constants can be defined or removed. Additional include directories, you can specify an Additional Include directory, which is generally relative to the directory of the project, such as. \ include.
Connection parameter settings. The Project-> Settings-> Link page of the VC menu item is used. We can see the content in Project Options at the bottom of this page, which is generally as follows:
/Nologo/subsystem: windows/incremental: yes/pdb: "Debug/WritingDlgTest. pdb"/debug/machi
Ne: I386/out: "Debug/WritingDlgTest.exe"/pdbtype: sept
Next, let's take a look at the various settings in Category.
1) General: some General settings. You can set the generated file path and file name, connect to the library file, Generate debug info, and Generate Debug information. PDB file (the specific format can be set in Category-> Debug); Ignore All Default Libraries, discard All Default library connections; Link Incrementally, through generation. the ILK file implements incremental connections to increase the speed of subsequent connections, but the files (EXE or DLL) generated in this mode are large; Generate Mapfile, Generate. the MAP file records information about the module. Enable Profiling, this parameter is usually used together with the Generate Mapfile parameter, and cannot be used if Debug information is generated. PDB file, and Microsoft Format must be used.
2) Customize: You can set the database file. Force File Output, which forces the generation of Output files (EXE or DLL); Print Progress Messages, which can Output the Progress information in the connection process to the Output window.
3) Debug: sets whether to generate debugging information and the format of debugging information. The Format can be Microsoft Format, COFF Format (Common Object File Format), and Both Formats. Separate Types indicates that the Debug Format information is independent. PDB files are stored directly in the source files. in the PDB file. If selected, the latter method is used. This method is faster for debugging and startup.
4) Input: Specify the library file to be connected and discard the database file to be connected. You can also add an additional library file directory, which is generally relative to the directory of the project, such as. \ Lib. Force Symbol References. You can specify a library to connect to a specific Symbol definition.
5) Output: The Base Address can change the default Base Address of the program (the default Base Address of the EXE file is 0x400000, and the default DLL Address is x0000000 ), when the operating system loads a program, it always tries to start from this base address. Entry-Point Symbol can specify the Entry address of a program. Generally, it is a function name (and the _ stdcall call Convention must be used ). Generally, for Win32 programs, the EXE entry is WinMain and the DLL entry is DllEntryPoint. It is best to enable the connector to automatically set the program entry point. By default, it is implemented through a C Runtime library function: the console uses mainCRTStartup (or wmainCRTStartup) to call the main (or wmain) function of the program; windows programs use WinMainCRTStartup (or wWinMainCRTStartup) to call WinMain (or wWinMain, which must use the _ stdcall call Convention) of the program ); DLL uses _ DllMainCRTStartup to call the DllMain function (the _ stdcall call Convention must be used ). Stack allocations, used to set the Stack size used by the Program (in decimal format). The default value is 1 MB. Version Information tells the connector to add the Version number to the start part of the EXE or DLL file.
It is worth noting that the above parameters are case-sensitive. Adding "-" after the parameter indicates that this parameter is invalid; parameter value options
"*" Indicates the default value of this parameter. You can use the "Reset" button in the upper-right corner of the page to restore all default settings on this page.
Other parameter settings
1) Project-> Settings-> General, you can set the method for connecting to the MFC Library (static or dynamic ). If it is a dynamic connection, do not forget to include the mfc dll when your software is released.
2) Project-> Settings-> Debug: You can set executable files and command line parameters that run during debugging.
3) Project-> Settings-> Custom Build, you can set to automatically execute some operations after the compilation/connection is successful. When writing COM, you want VC to automatically register the compiled COM file. You can set it as follows:
Description: Register COM
Commands: regsvr32/s/c $ (TargetPath)
Echo regsvr32 exe. time> $ (TargetDir) \ $ (TargetName). trg
Outputs: $ (TargetDir) \ $ (TargetName). trg
4) Tools-> Options-> Directories: Set the Include and Library paths of the system.
Tips
1) Sometimes, during compilation, the computer suddenly shuts down illegally (maybe someone accidentally hits the power or your memory is unstable ). After you restart the machine, open the project and re-compile it. The VC will crash. You may think that your VC compiler is broken, but it is not (you should try to compile other projects !), You only need to delete the project's. ncb,. opt,. aps,. clw files, and all the files under the Debug and Release directories, and then recompile them.
2) If you want to share your source code project with others, but copying the entire project is too large. You can delete the following files: dsw,. ncb,. opt,. aps,. clw,. plg, and all files in the Debug and Release directories.
3) when your Workspace contains multiple projects, you may not be able to intuitively and intuitively see which Project is the current one. You can set it as follows: Tools-> Options-> Format, select Workspace window in Category, and change its default font (for example, set it to Fixedsys.
4) how to change the name of an existing Project? Turn off the Project. Open the. dsp file in text format and replace the original Project name.
5) VC6 is useful for smart prompting of class members, but sometimes it fails. You can close the project first. clw and. delete ncb and re-open the project. Click View> ClassWizard in the menu item. In the displayed dialog box, click "Add All" and Rebuild All. The problem should be solved.
Appendix: VC compilation syntax explanation
VC-Project Setting-Debug-Project Option syntax
-Optimization-
/O1 minimize space
/Op [-] improve floating point consistency improve floating-pt consistency
/O2 maximum speed maximize speed
/OS preferred code space favor code space
/Oa assume there is no alias assume no aliasing
/Ot preferred code speed favor code speed
/Ob inline expansion (default n = 0) inline expansion (default n = 0)
/Ow assume that the cross-function alias assume cross-function aliasing
/Od disable optimization (default) disable optimizations (default)
/Ox maximization option. (/Ogityb2/Gs) maximum opts. (/Ogityb1/Gs)
/Og enable global optimization
/Oy [-] enable framework pointer to omit enable frame pointer omission
/Oi enable the built-in function enable intrinsic functions
-Code Generation-
/G3 is 80386 optimized optimize for 80386
Optimize for 80486 for 80486
/GR [-] enable C ++ RTTI
/G5 optimize for Pentium
/G6 optimized optimize for Pentium Pro
/GX [-] enable C ++ Exception Handling (same as/ESCS) enable C ++ EH (same as/ESCS)
/EHs enable synchronous C ++ Exception Handling enable synchronous C ++ EH
/GD optimizes optimize for Windows DLL
/GB for mixed model optimization (default) optimize for blended model (default)
/EHa enable asynchronous C ++ Exception Handling enable asynchronous C ++ EH
/Gd _ cdecl call convention _ cdecl calling convention
/EHc extern "C" Default Value: nothrow extern "C" defaults to nothrow
/Gr _ fastcall call convention _ fastcall calling convention
/Gi [-] enable incremental compilation
/Gz _ stdcall call convention _ stdcall calling convention
/Gm [-] enable minimum rebuild enable minimal rebuild
/GA optimizes optimize for Windows Applications
/Gf enable string pool enable string pooling
/QIfdiv [-] enable Pentium FDIV to fix enable Pentium FDIV fix
/GF enable the read-only string pool enable read-only string pooling
/QI0f [-] enable Pentium 0x0f to fix enable Pentium 0x0f fix
/Gy separates the linker function separate functions for linker
/GZ enable runtime debugging check enable runtime debug checks
/Gh enable hook function call
/Ge force stack check for all functions force stack checking for all funcs
/Gs [num] disable stack check call disable stack checking CILS
-Output file-
/Fa named assembly list file name assembly listing file
/Fo name object file
/FA [SC] configure the assembly List configure assembly listing
/Fp naming precompiled header file name precompiled header file
/Fd name. PDB file name. PDB file
/Fr name source browser file
/Fe name the executable file name executable file
/FR name extension. SBR file name extended. SBR file
/Fm name ing file name map file
-Pre-processor-
/FI naming forces include file name forced include file
/C does not extract the comment don't strip comments
/U remove the predefined macro remove predefined macro
/D {=|#} definition macro define macro
/U remove all predefined macros
/E directs preprocessing to standard output preprocess to stdout
/I add to the search path containing files add to include search path
/EP directs preprocessing to the standard output. do not include the row number preprocess to stdout, no # line
/X ignore "standard location" ignore "standard places"
/P preprocessing to file preprocess to file
-Language-
/Zi enable debugging information
/Zl ignores the default library name omit default library name in. OBJ.
/ZI enable the "Edit and Continue" function of debugging information enable Edit and Continue debug info
/Zg generates the function prototype generate function prototypes
/Z7 enable the old debug information enable old-style debug info
/Zs only performs syntax check only
/Zd only requires line number debugging information line number debugging info only
/Vd {0 | 1} disable/enable vtordisp
/Zp [n] wraps the structure pack structs on n-byte boundary on the n-byte boundary.
/Vm pointing to the member's pointer type of pointers to members
/Za disable extension (/Op) disable extensions (implies/Op)
/NoBool disable the "bool" keyword disable "bool" keyword
/Ze enable extension (default) enable extensions (default)
-Miscellaneous-
/?, /Help print this help message
/C is compiled only and does not link compile only, no link
/W sets the warning level (default n = 1) set warning level (default n = 1)
/H maximize external name length max external name length
/J the default char type is unsigned
/Nologo cancel display copyright message suppress copyright message
/WX treats the warning as an error treat warnings as errors
/Tc compile the file as. c compile file as. c
/Yc create. PCH file create. PCH file
/Tp compile the file as. cpp compile file as. cpp
/Yd put the debugging information in each. OBJ put debug info in every. OBJ
/TC compile all files into. c compile all files as. c
/TP compile all files into. cpp compile all files as. cpp
/Yu use the. PCH file use. PCH file
/V: set version string
/YX automatic. PCH file automatic. PCH
/W disable all warnings
/Zm maximum memory allocation (% by default) max memory alloc (% of default)