C ++ Primer study note _ 108 (finale !!!) _ Special tools and technologies, primer_108

Source: Internet
Author: User

C ++ Primer study note _ 108 (finale !!!) _ Special tools and technologies, primer_108
Special tools and technologies [Finale]-inherent unportable features [II]

 

3. Link indication: extern "C"

C ++ programs sometimes need to call functions written in other programming languages. The most common language is C. Like any name, the name of a function written in other languages must be declared, and the return type and parameter table must be specified. The compiler checks calls to external language functions in the same way as normal C ++ functions. However, the compiler generally has to generate different code to call functions written in other languages. C ++ uses the link to indicate the language used by any non-C ++ function.

 

1. Declare non-C ++ Functions

The link indication cannot appear inside the class definition or function definition. It must appear in the first declaration of the function (remember to declare it here. For example, some C functions declared in cstdlib

// A single extern "C" size_t strlen (const char *); // composite extern "C" {int strcmp (const char *, const char *); char * strcat (char *, const char *);}

[Description]

1) The value of the string is followed by the extern. The value of the string indicates the programming language used to compile the function.

2) If curly braces are ignored, the function names declared in curly braces are visible, just as they are declared outside curly brackets.

 

2. Link indication and header file

Multiple declarations can be applied to the entire header file. For example, the cstring header file of C ++ can be like this:

extern "C"{#include <string.h>}

When the # include indicator is included in the curly brackets of the composite link indication, it is assumed that all common function declarations in the header file are functions written in the Link indication language. The link indication can be nested. Therefore, if the header file contains a function with the link indication, the link of the function will not be affected.

Allow C ++ to define the function inherited from the C function library as a C function, but it is not necessarily defined as a C function-decide whether to use C or C ++ to implement the C function library, is implemented by every C ++.

 

3. Export C ++ functions to other languages

By using link instructions for function definitions (remember this is the definition here), you can use C ++ functions for programs written in other languages:

extern "C"double calc(double dparm){    /**.....**/}

When the compiler generates code for the function, it generates code suitable for the specified language.

Each declaration of a function defined with Link indication must use the same link indication.

 

4. Supported languages

The compiler is required to support the link indication for the C language. The compiler can provide links for other languages. For example, extern "Ada", extern "FORTRAN", and extern "Java" (g ++ can support Java.

[Annotation]

The language that is supported varies with the compiler. You must refer to the user guide for further information about any non-C link descriptions that the compiler can provide.

 

Support for preprocessors linked to C sometimes requires compiling the same source file in C and C ++. When C ++ is compiled, the Preprocessor name _ cplusplus (two underscores) is automatically defined. Therefore, you can include the Code conditionally based on whether C ++ is being compiled.

#ifdef __cplusplusextern "C"#endif // __cplusplusint strcmp(const char *,const char *);

 

5. Overload functions and link instructions

The interaction between the link indication and function overload depends on the target language. If the language supports overload functions, the compiler that implements link indication for the language may also support the overloading of these functions in C ++.

C ++ ensures that the only supported language is C. C language does not support function overloading. Therefore, in a group of overloaded functions, only link indication can be specified for a C function. It is wrong to declare more than one function using the C link with the given name:

extern "C" void print(const char *);extern "C" void print(int);//Error

In C ++ programs, overloading C functions is very common. However, other functions in the overload set must be C ++ functions:

class SmallInt{    /*...*/};class BigNum{    /*...*/};extern "C" double calc(double);SmallInt calc(SmallInt &);BigNum calc(BigNum &);      //OK

You can call the C version of calc from the C program and C ++ program. Other functions are C ++ functions with type parameters and can only be called from C ++ programs. The Declaration Order is not important.

 

6. pointer to the extern "C" function

The language used to write a function is part of the function type. To declare pointers to functions written in other programming languages, link instructions must be used:

extern "C" void (*pf)(int);

When using pf to call a function, it is assumed that the call is a C function call and the function is compiled.

The pointer of a C function has different types than that of a C ++ function. You Cannot initialize or assign a pointer to a C ++ function (or vice versa ). If this mismatch exists, a compilation error is returned:

void (*pfC)(int);extern void (*pfCPP)(int);pfC = pfCPP;    //Error

7. link instructions applied to the entire declaration

When using the link indication, it is applied to the function and any function pointer and used as the return type or form parameter type:

extern "C" void f1(void (*)(int));

This statement indicates that f1 is a C function that does not return values. It has a form parameter. This form parameter is a pointer that does not return values and accepts a single form parameter. Link indication is applied to this function pointer and f1. When calling a function, you must pass the C function name or C function pointer to it.

Because the link indication is applied to all functions in a declaration, the type alias must be used to pass the pointer of the C function to the C ++ function:

extern "C" typedef void FC(int);void f2(FC *);




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.