C + + Primer learning Note _108 (Grand finale!!!) _ Special tools and techniques-inherent non-portable features [next]

Source: Internet
Author: User
Tags class definition strcmp

Special tools and Technologies [Grand Finale]--inherent non-portable features[Under

Three. Link indication: extern "C"

C + + programs sometimes need to invoke functions written in other programming languages , and the most common language is the C language. Like any name, the name of a function written in another language must be declared, and the declaration must specify the return type and formal parameter list . The compiler examines calls to external language functions in the same way as normal C + + functions, but the compiler generally must produce different code to invoke functions written in other languages. C + + Use The link indicates the language in which any non- C + + function is indicated.

1. Declaring non- C + + functions

A link indicates that it cannot appear inside a class definition or function definition, it must appear on the first declaration of the function ( Remember here as a declaration ) . such as : Some of the C functions declared in Cstdlib

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

[ description ]

1) extern is followed by string literals , and string literals indicate the programming language in which the function is written .

2) ignores curly braces , and the function name declared in curly braces is visible , just like declaring a function outside of curly braces .

2. link indication with header file

You can apply a multi-declaration form to the entire header file . For example, C + + of the CString The header file can look like this:

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

When the #include is indicated in the curly brackets indicated by the composite link, It is assumed that all normal function declarations in the header file are functions written in the language indicated by the link . The link indicates that it can be nested, so if the header file contains a function with a link indication, the link to the function is not affected.

A function that allows C + + to inherit from the C function library is defined as a C function, but not must be defined as a c function -- The decision is to use C or with the C + + Implement C function library, which is each C + + realization of things.

3. Export C + + functions to other languages

Use the link indication for a function definition ( as defined here ) so that programs written in other languages can use C + + function :

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

When the compiler generates code for the function, it produces code that is appropriate for the specified language.

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

4. Supported languages for link indication

Requires the compiler to support the C language link indication. The compiler can provide link descriptions for other languages. For example,extern "Ada",extern "FORTRAN", extern "Java" (g++ can be used for java  Support ) and so on.

[ note ]

What languages are supported is changed with the compiler. You must consult the User Guide for further information about any non- C link instructions that the compiler can provide .

preprocessor support linked to C sometimes requires compiling the same source file in C and C + + . When compiling C + + , the preprocessor name __cplusplus(two underscores) is automatically defined, so you can conditionally include code based on whether you are compiling C + +

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

5. Overloaded functions and link indications

The interaction between link indications and function overloads depends on the target language . If the language supports overloaded functions, the compiler that implements links for that language is likely to support the overloads of these functions for C + + .

The only language supported by C + + guarantees is C. C The language does not support function overloading, so in a set of overloaded functions you can only be a single C The function specifies the link indication. It is wrong to declare more than one function with a C link with the given name:

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

Overloading C functions is common in C + + programs , but the other functions in the overloaded collection must all 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 C programs and C + + programs . The remaining functions are C + + functions with type parameters that can only be called from C + + programs. The order of the declarations is not important.

Pointer to 6.extern "C" function

The language in which the function is written is part of the function type. To declare a pointer to a function written in another programming language, you must use a link indicator:

extern "C" Void (*PF) (int);

When using pf to invoke a function , assume that the call is a C function call and compile the function .

pointers to c functions have different types than pointers to C + + functions, and pointers to C functions cannot be initialized or assigned to C + +  A pointer to a function (or vice versa). When this mismatch is present, a compile-time error is given:

void (*PFC) (int), extern void (*pfcpp) (int);p FC = Pfcpp;    Error

7. Link indication applied to the entire declaration

When using a link indicator, it is applied to functions and any function pointers, as return types or parameter types:

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

This statement says thatF1 is a C function that does not return a value , and it has a formal parameter, which is a pointer to a function that does not return a value and accepts a single parameter. The link indicates that the function pointer and F1are applied. When calling, you must pass the C function name or the C function pointer to it.

Because a link indicates that all functions are applied to a declaration , you must use a type alias to pass a pointer to the C function to the C + + function :

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

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.