Console Application for cainiao to learn C ++

Source: Internet
Author: User

I have been engaged in winform development for C # before. For c ++, although I have studied it in college, I have learned a lot since I was the first, and I have been putting it down for a long time. Therefore, this blog set up, I have defined it as c ++ for cainiao, and all the tools are vs2010. the use of vs2010 is purely due to the habit of using vs for c.

This document does not describe how to install vs2010. You can check it on the Microsoft official website.

The following describes the simplest project type that must be met before getting started with c ++: win32 console.

  1. Create a project:

File-> new-> project. In the displayed dialog box, select win32 Console Appliaction. In the displayed dialog box, click next, in the new pop-up dialog box, select the type of the new project and some attribute settings. For example, in the middle.

Windows Application: A windows Application.

Console Application: the Console Application that we are about to create.

DLL: Link Library, dynamic link library.

Static Library: Static Link Library.

Figure 1

Figure 2

Figure 3

Figure 4

Click Finish.

A series of files are automatically generated on the left:

Stdafx. h: First, you need to understand the c ++ compilation mechanism. A c ++ project must have many header files and cpp files, how the compiler makes it a dll or executable file. Here, in fact, the compiler only compiles the cpp file, and then compiles the corresponding. h file according to the. cpp File include. h file. When many. cpp files reference some public. H files, it is a waste of work to process these. H files separately for each. cpp file. Therefore, the stdafx. h file appears. Put the include of all standard header files in this header file, and the corresponding cpp file only needs the inlude file. The compiler considers all the code before # include "stdafx. h" to be precompiled. He will skip this instruction. The operating principle is as follows:

AppWizard works with the VisualC ++ compiler as follows:

◎ AppWizard creates the stdafx. h file, which contains all the standard header files required for the current project file. This file can change with the selected options.

◎ AppWizard, then stdafx. cpp is created. This file is usually the same.

◎ Then AppWizard creates the project file, so that the first compiled file is stdafx. cpp.

◎ When Visual C ++ compiles the stdafx. cpp file, it stores the result in a file named stdafx. pch. (The extension pch indicates the precompiled header file .)

◎ When Visual C ++ compiles each subsequent. cpp file, it reads and uses the. pch file it just generated. VisualC ++ does not analyze Windowsinclude files any longer, unless you have compiled stdafx. cpp or stdafx. h.

To use this file, you must follow the following rules:

◎ Any. cpp file you write must first contain stdafx. h.

◎ If you have a majority of. cpp files in the project file that require. h files, add them to stdafx. h (back) and pre-compile stdafx. cpp.

Targetver. h: defines the version macro. The automatic English comments are as follows:

// Including SDKDDKVer. h defines the highest available Windows platform.

 

// If you wish to build your application for a previous Windows platform, include WinSDKVer. h and

// Set the _ WIN32_WINNT macro to the platform you wish to support before including SDKDDKVer. h.

The _ tmain function in the file HelloWorld. cpp is the entry for running the program:

Int
_ Tmain (int
Argc, _ TCHAR * argv [])

{

Return 0;

}

As the function entry, add the commands to be loaded or run when the program starts. For example:

# Include
"Stdafx. h"

 

Int
_ Tmain (int
Argc, _ TCHAR * argv [])

{


Cout <"hello world" <endl;


System ("PAUSE ");


Return 0;

}

At this time, you can run the program:

 

 

 

 

Appendix:

1. Dynamic Link Library)
On Windows, you can create independent program modules as smaller DLL (Dynamic Linkable Library) files and compile and test them separately. During runtime, the system will load the EXE program to the memory space only when it does need to call these DLL modules. This method not only reduces the size of the EXE file and the need for memory space, but also enables these DLL modules to be used by multiple applications at the same time. Windows itself implements some major system functions in the form of DLL modules. Windows provides a wide range of function calls for applications. These function calls are included in the dynamic link library. Among them, there are three most important DLL, Kernel32.dll, which contains various functions used to manage memory, processes and threads; User32.dll, it contains various functions used to execute User Interface tasks (such as window creation and message transfer), and GDI32.dll, which contains various functions used to draw and display text.

When using dynamic link libraries, we usually provide two files: one Import Library and one DLL. Import to the database contains the name of the function and variable exported by the DLL. The DLL contains the actual function and data. When compiling a link to an executable file, you only need to import the link to the database. The function code and data in the DLL are not copied to the executable file, and the DLL is loaded at runtime, access the function exported in DLL. A Windows. EXE file has the function used to reference different dynamic link libraries. When a Windows program is loaded into the memory, the call in the program is directed to the DLL function entry. If the DLL is not in the memory, the system loads it into the memory. When linking a Windows program to generate an executable file, you must link a dedicated "import library" provided by the programming environment ". These imported libraries contain the dynamic link library name and reference information of all Windows function calls. The linked program uses this information to construct a table in the. EXE file. When the program is loaded, Windows uses this information to convert the call to a Windows function.

2. Static Library)
Functions and data are compiled into a binary file (usually with the extension. LIB ). When a static library is used, the linker copies these functions and data from the library and combines them with other modules of the application to create the final executable file (. EXE file ).
Import-to-database and static databases are very different. They are actually different. The static library itself contains the actual execution code, symbol table, and so on. For the import and export operations, the actual execution code is in the dynamic library, and the import and export operations only contain the address symbol table, make sure that the program finds some basic address information of the corresponding function.

Static link and dynamic link:

Static Link Method: # pragma comment (lib, "test. lib "), when the static link is loaded, the dynamic code or the address of the dynamic code used by the program will be determined. The link of the static library can use the static link, you can also use this method to link to the database for import and export.

Dynamic Link Methods: LoadLibrary ()/GetProcessAddress () and FreeLibrary (). programs using this method do not complete dynamic links at the beginning, but do not call dynamic library code until they actually call dynamic library code, load the program to calculate the logic address of the dynamic code (the called part). Then, when the program needs to call another dynamic code at a certain time, load the program to calculate the logic address of this part of the code, so this method makes the program initialization time shorter, but the performance during the running period is not as good as the static link program.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.