C + + Applications compile, link under Windows: Part I overview __c++

Source: Internet
Author: User

This article is an in-depth understanding and analysis of C + + applications compiled and linked under Windows, with the following directory:

Let's take a look at the first chapter of the overview section. 1 Overview 1.1 Compilation Tools Introduction

Cl.exe is the compiler under Windows platform, Link.exe is the linker under Windows platform, the C + + source code compiles and links with them, and the resulting executable file can run under the Windows operating system. Cl.exe and Link.exe are integrated in Visual Studio and are installed in the VC-related directory as development tools are installed in Visual Studio.

There are two ways to use the compiler, one in the Visual Studio development environment, the direct click of a command button, the start of the compiler through Visual Studio, and the other way to compile C + + source code files in a command-line window through the C l command.

The various default parameters for the C L command have been set in Visual Studio in the integrated development environment, and when you compile C + + source code using Visual Studio, you will eventually invoke the compilation tool and use these preset default parameters.

When you install Visual Studio, Setup sets the various parameters and variables required by the compiler (Cl.exe) and Linker (Link.exe) in the command-line tool, Visual Studio 2008 Command Prompt, and therefore, in the The Command Line window of the Visual Studio 2008 Command Prompt tool enables you to compile C + + source code using C-L. The path to the Visual Studio 2008 Command Prompt tool is: Start-"All Programs-" visual studio-Visual Studio tools-Visual Studio 2008 Command Prompt.

When compiling C + + source code, the compiler needs to use three environment variables: path, which sets the path of the compiler cl.exe and the path where the compiler relies on a dynamic-link library (mspdb80.dll). After setting this environment variable, you can type the C l command directly in the Command line window without having to locate the current directory to the Cl.exe installation directory; For example: "C:\Program Files (x86) \microsoft Visual Studio 9.0\vc\bin ; C:\Program Files (x86) \microsoft Visual Studio 9.0\team tools\performance Tools ". The previous address specifies the path where the cl.exe is located, and the following address specifies the path of the Mspdb80.dll. Include, for setting the path of the C run-time Library header file. After setting this environment variable, in the C + + source code, you can use the form "#include <stdio.h>" to introduce the runtime header file, if you do not set this variable, you must use the #include <c:\program files ( x86) introduces the runtime's header file in the form of \microsoft Visual Studio 9.0\vc\include\stdio.h>, otherwise the compiler will not be able to find the header files to be introduced at compile time. For example, "C:\Program files (x86) \microsoft Visual Studio 9.0\vc\include", which specifies the location of the C run-time Library header file. Lib, which is used to set the path of the C run-time library destination file. The linker uses the environment variable to locate the target file for the C Run-time library. For example, "C:\Program files (x86) \microsoft Visual Studio 9.0\vc\lib", which specifies the location of the C run-time library destination file.

If we set these three environment variables in the system environment variable, we can use the CL command in the Normal command-line window to compile C + + source code instead of using Visual Studio's Integration tool, "Visual Studio 2008 Command Prompt." 1.2 Application Sample 1.2.1c++ source code

This article will be discussed in the following application example, through the compilation of the application, the introduction of the link process, focusing on the data format of the PE file, as well as how the operating system performs the "Reset base Address" in the application loading process, and the "dynamic link" between each DLL.

The invocation relationship between the modules of the sample application is shown in the following illustration:

In the sample application, the source code files are described in the following table:

Serial number

File name

Describe

1

DemoDef.h

Define the import of functions, export, define global variables and global functions

2

DemoMath.h

Definition of mathematical Operation class

3

DemoOutPut.h

Definition of information Output class

4

DemoMath.cpp

Implementation of mathematical operation class, definition of global function, definition of global variable

5

DemoOutPut.cpp

Realization of information Output class

6

Main.cpp

Main function

The source code for the sample application is as follows:

------------------------------------main.cpp--------------------------------------------

#include "DemoDef.h"

#include "DemoMath.h"

#include <iostream>

using namespace Std;

int nglobaldata = 5;

int main ()

{

Demomath Objmath;

Objmath.adddata (10,15);

Objmath.subdata (nglobaldata,3);

Objmath.divdata (10,0);

Objmath.divdata (10,nglobaldata);

Objmath.area (2.5);

int ntimes = Getopertimes ();

cout << "The number of operations:" << ntimes << Endl;

To stop the command line

int k = 0;

Cin >> K;

}

----------------------------------------DemoDef.h------------------------------------

#ifndef _demodef_h

#define _demodef_h

#include <stdio.h>

Define the import of a function, export

#ifdef Demodll_exports

#define Demodll_export _declspec (dllexport)

#else

#define Demodll_export _declspec (dllimport)

#endif

A symbolic constant in a file scope that will perform a constant collapse

Const double PI = 3.14;

Declaring global variables, logging the number of operations

extern int nopertimes;

Declares a global function that returns the number of operations

int Demodll_export getopertimes ();

#endif

-------------------------------------DemoMath.h-------------------------------------

#ifndef _demomath_h

#define _demomatn_h

#include "DemoDef.h"

Class Demooutput;

Class Demodll_export Demomath

{

Public

Demomath ();

~demomath ();

void AddData (double a,double b);

void Subdata (double a,double b);

void Muldata (double a,double b);

void Divdata (double a,double b);

void area (double R);

Private

Demooutput * M_POUTPUT;

};

#endif

--------------------------------------------------DemoOutPut.h-----------------------------------------

#ifndef _demooutput_h

#define _demooutput_h

Performing information output

Class Demooutput

{

Public

Demooutput ();

~demooutput ();

Output values

void Outputinfo (double a);

Output string

void Outputinfo (const char* PSTR);

};

#endif

-----------------------------------------------------DemoMath.cpp-----------------------------------------

#include "DemoMath.h"

#include "DemoOutPut.h"

Definition of global variables

int nopertimes = 0;

Definition of global function

int Getopertimes ()

{

return nopertimes;

}

Implementation of the class method

Demomath::D Emomath ()

{

M_poutput = new Demooutput ();

}

Demomath::~demomath ()

{

if (m_poutput!= NULL)

{

Delete M_poutput;

M_poutput = NULL;

}

}

void Demomath::adddata (Double A, double b)

{

nopertimes++;

M_poutput->outputinfo (A + B);

}

void Demomath::subdata (Double A, double b)

{

nopertimes++;

M_poutput->outputinfo (A-b);

}

void Demomath::muldata (Double A, double b)

{

nopertimes++;

M_poutput->outputinfo (A * b);

}

void Demomath::D ivdata (Double A, double b)

{

if (b = = 0)

{

M_poutput->outputinfo ("Divisor cannot be zero");

Return

}

nopertimes++;

M_poutput->outputinfo (A/b);

}

void Demomath::area (Double R)

{

nopertimes++;

M_poutput->outputinfo (R * R * PI);

}

---------------------------------------------------------DemoOutPut.cpp---------------------------------------------

#include <iostream>

#include "DemoOutPut.h"

Demooutput::D emooutput ()

{

}

Demooutput::~demooutput ()

{

}

void Demooutput::outputinfo (Double A)

{

Std::cout << "The result of the calculation is:" << a << Std::endl;

}

void Demooutput::outputinfo (const char *PSTR)

{

Std::cout << pstr << Std::endl;

}

1.2.2Visual Studio Support for C + + source code

When writing C + + source code, if you want to use a class library, then you must introduce this class library header file, you must know the specific path of this type header file. In the previous code example, the "#include <stdio.h>" was used to introduce a C run-time library header file. In this reference, we did not set the specific path of the header file, nor did we set the specific path of the header file in another location, but the integrated development environment Visual Studio was able to find the exact location of the file. The specific reason for this is that when you install Visual Studio, Setup has set the location of the C Run-time Library header file in Visual Studio. You can view these pre-set information through the menu "Tool-options->projects and Solutions->vc++directories". The specific situation is as follows:

In the illustration above, you can select the type of path you want to set by using the Drop-down window "show directories for", including the path to the header file (include files), the path to the Lib file (Library files), source code files Path, and so on.

In the header file path setting, a total set of four types of header file path, is the C run-time Library header file path, MFC class Library header file path, WIN32API development related header file path, as well as the framework related to the header file path.

In addition to the various paths that the system has set up beforehand, we can also set the various other paths we need in the window. 1.3c++ The source code compilation process 1.3.1 Compilation Process Overview

When compiling C + + source code, the entire compilation process can be divided into two phases, namely compile phase and link stage. In the compile phase, the C + + source code (header file + source file) written by the programmer is input, after the compiler processing, output COFF format of the binary target file; In the link phase, the target file output in the compile phase is input, and the executable file in the PE format is exported through the linker link. The entire compilation process is shown in the following illustration:

The compilation phase can be further subdivided into three sub phases: Precompilation, compilation and compilation. In each sub phase, the different work content and output of different outputs will be corresponding.

Since the programmer is the C/C + + runtime based on the development of the base of the + + application, so in the link phase, in addition to the compilation phase of the output of the target file to link, but also to add to the C + + Run-time database related to the target file links. This type of link is divided into two situations. One scenario is a link that is caused by an explicit call to a function in the C/C + + runtime from the source code For example, a function in the C/C + + runtime is called in the + + source code: printf (), then when linking, you need to link the target file of printf (). Another case is implicitly, automatically done by the linker. When a C + + application is running, it must be supported by the C + + runtime library, so the library files that support the C + + application running are also automatically linked by the linker at the time of the link. In either case, the C + + Run-time library must be linked to the C + + application.

In the Command Line window, you can use the C l command to compile the C + + source code. At compile time, you can set different compilation options, and then get different output results. For example: You can complete the compilation work in one step, directly obtain the PE format executable file. In this case, Cl.exe will automatically invoke Link.exe to perform the link work when the compilation is complete. You can also get compile results from different stages in a phased compilation, and you can subdivide the compilation process by setting different C l command options. For example:/C command means only compile, not link, you can get the target file through this command; the/P command indicates that only precompilation is performed, which allows you to view the results of precompilation; the/FA command represents the assembly operation, through which you can obtain program files in assembly language format.

In the C + + source code compilation process, the detailed description of each step is shown in the following table:

The

Ordinal

Step

Input

Output

Description

C l command

1

Pre-compilation

C + + source files

. i file

Outputs the preprocessed file.

cl/p xxx.cpp

2

Compile

P>c++ the source file

. asm file

Output assembly file.

Cl/fa xxx.cpp

Related Article

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.