Link indication: extern "C"

Source: Internet
Author: User

C + + programs sometimes need to invoke functions written in other languages, most commonly by invoking a function written in the C language. Like all other names, the names of functions in other languages must also be declared in C + +, and the declaration must specify a return type and a formal parameter list. For functions written in other languages, the compiler checks that they are called the same way as normal C + + functions, but the generated code differs. C + + uses link indication (linkage Directive) to indicate the language used by any non-C + + function.

  Declare a non-C + + function:

Link indications can have two forms: single or composite. A link indicates that it cannot appear inside a class definition or function definition.

For example:

Single statement: extern "C" size_t strlen (const char *);

Compound statement: extern "C" {

int strcmp (const char*, const char*);

Char *strcat (char*, const char*);

}

Link indication with header file:
Compound Statement :

 extern "C" {

#include <string.h>

}

Pointer to extern "C" function:

The language in which the function is written is part of the function type. (pointers to functions written in other languages must be indicated by the same link as the function itself)

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

When we use PF to invoke a function, the compiler determines that a C function is currently being called.

A pointer to a C function is a different type than a pointer to a C + + function.

The link indicates that the entire declaration is valid:

  When we use the link indicator, he is not only valid for the function, but also for function pointers as return types or parameter types.

F1 is a C function whose formal parameter is a pointer to a C function

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

Because the link indicates that all functions are acting on the declaration statement at the same time, if we want to pass a C + + function to a pointer to a C function, you must use the type alias.

FC is a pointer to a C function

extern "C" typedef void FC (int);

F2 is a C + + function in which the formal parameter is a pointer to a C function

void F2 (FC *);

Export C + + functions to other languages:

  By using link indications to define a function, we can make a C + + function available in programs written in other languages.

The Calc function can be called by a C program

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

The compiler will generate code for the function that is appropriate for the specified language

  Support for pre-processors linked to C

Sometimes it is necessary to compile the same source file in C and C + +, for this purpose, the preprocessor defines __cplusplus (two underscores) when compiling a C + + version of the program. With this variable, we can conditionally include some code when compiling a C + + program:

#ifndef __cplusplus

Correct: We are compiling C + + programs

extern "C"

#endif

int strcmp (const char*, const char*);

  Overloaded functions and links indicate:

  The C language does not support function overloading, so it is not difficult to understand why a C link indicates that only one row in a set of overloaded functions can be described:

Error: two extern "C" functions have the same name

extern "C" void print (const char*);

extern "C" void print (int);

If you have a C function in a set of overloaded functions, the rest of the functions must be C + + functions.

Link indication: extern "C"

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.