Introduction to C + +

Source: Internet
Author: User

How to generate an executable program with C + + source files

A C + + program consists of one or more compilation units. Each compilation unit is a separate source code file, usually a file with A. cpp , and the compiler compiles a single file compilation unit at a time, generating a . obj or . O The target file for the suffix. The program is running on these target files. These target files are binary files. Once all the compilation units have generated the target file, we use the connector to connect them to an executable . exe file.

We can implement multiple classes in a . cpp file, or you can distribute multiple classes in multiple . cpp files. When a . cpp file is modified, we only need to recompile the file to generate a new executable program.

With the compiler, you can compile the . o file separately for the two CPP files . generate an executable program through the . O file. For example:

Cc-c main.cpp

Cc-c Test.cpp

CC MAIN.O test.o-o Test

Finally executed ./test 123

InMainfile that was calledTestthe function in the file. Need to declare before callingTestfunctions in the. If there are too many files, there will be a duplicate statement, such asAstated in theBin the function,Calso stated in theBthe function in theAcalledCThis function declares a duplicate error, the connector will error output "unresolved symbol"(non-resolvable notation), so in practice, we write this function declaration to the header file, as. H\.hh\.phh\.hxxand so on, when we need to call this function, the header file containing the function is available. The format is as follows:

#ifndef Test_h

#define Test_h

int test (int);

#endif

Do not assume that the program is composed of some target files. In practice, executable programs often connect many libraries, which have implemented many off-the-shelf functionality. There are two main types of libraries:

The static library can be placed directly into an executable program, just like the target file, but makes the executables very large.

The dynamic library DLLs are located in a standard location on the machine, and they are loaded automatically when the application starts.

C + + 's recompilation and performance-seeking are different from those in Java and C # . C + + does not detect when the array is out of bounds at run time, and no garbage collector reclaims dynamic memory that is allocated but no longer used.

A high-byte system architecture (such asPowerPCand theSPARC), atbit Variable Value0x12345678will be stored as4bytes:0x12,0x34,0x56,0x78. For high-byte in-front system architectures (such asIntel x86system). The order in which these bytes are stored is reversed. This will result in differences in the memory area of the data being copied to the disk or sent to the network on a binary program.

Second,C + + syntax

The introverted function (inline) has implemented the method in the class definition. The corresponding is to put the function prototype in the header file, and the implementation of the functions of the code in the . cpp file. These two methods are syntactically equivalent, but when we invoke inline functions, the vast majority of compilers simply extend this function without generating actual function calls. This will make the program run more efficiently, but the corresponding increase in the application. Therefore, only very simple functions are implemented as inline functions.

Virtual Fun () =0// pure virtual functions-functions that do not have the default implementation code and must be implemented in subclasses.

In C + + , if you want a pointer to an instance object to access a class method, you must use"--" If it is an instance. ".

New dynamically allocated objects are generally allocated on the "heap", while local variables (variables defined in the function) are stored in the "stack".

The pointer is defined as a const type, and you can limit the pointers to not modify them, only to invoke constant members.

Typedef redefine data type, set to other name (alias)

data type coercion in C + +, this syntax is very powerful. You can change the pointer type, remove the const , and so on.

4 New cast types with more accurate semantics are introduced in C + + .

1 static_cast<t> () can be used to point to a is cast to point to b b must be class a

2,dynamic_cast<t> (), similar to static_cast<t> () , Only it uses the runtime type information method to verify that the object associated with this pointer is an instance of class B . If it is not, the cast returns a null pointer NULL.

3.const_cast<t> () Adds or removes a Const qualification to a pointer or reference .

4.reinterpret_cast<t> () converts a pointer or reference of one type to any other type.

May be a little blurry, my understanding is static_cast<t> () This situation is for those who operate the conversion specification does not have the problem of conversion,dynamic_cast<t> () is for those who may be wrong conversion, because there is return information can see whether the conversion success, I hope there is a better explanation below to tell me. The other two are fairly clear.

the function of the static statically keyword is to allow the variable or function declared by this keyword to be used only under this compilation unit, which is said to be more statically connected (staticlinkage). In other cases, it becomes an external connection (external linkage).

Namespaces are used in addition to:: Preceded by this namespace name, there are three different mechanisms:

Defining aliases for namespaces

Namespace a = AAAAAAAAAAAAAAAAAAAAAAAAA;

Import a simple identifier from the namespace

Using Aaaaaaaaaaaaaaaaaaaaaaaaaaa::fun;

Fun ();

Import an entire namespace with just one instruction

Using namespace Aaaaaaaaaaaaaaaaaa;

A preprocessor is a program that converts a "#" instruction to a source file that does not contain any instruction characters.

1. #include instructions will extend <> and "" included header files into their content.

2, #define replace the content defined by macro.

3. #undef to remove macro definitions.

4, #if,#elif,#else , and #endif can process or skip a certain part of the code.

5, #ifdef,#ifndef to avoid repeating the inclusion of a header file.

6. #error can give user-defined error messages at compile time.

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.