C and C ++ Mixed Programming

Source: Internet
Author: User

From: http://blog.ednchina.com/tianlebo/479334/message.aspx

Extern "C" indicates that the internal Symbol names generated by Compilation Use the C Convention. C ++ supports function overloading, but C does not. The compilation rules of the two are different. The name of the function in the symbol library after being compiled by C ++ is different from that in the C language. For example, assume that the prototype of a function is void foo (int x, int y). After the function is compiled by the C compiler, its name in the symbol library may be _ foo, the C ++ compiler generates names such as _ foo_int_int (different compilers may generate different names, but all adopt the same mechanism, the new name is called "mangled name "). A name such as _ foo_int_int contains the function name, number of function parameters, and type information. C ++ relies on this mechanism to implement function overloading. The following example shows how to use C functions in C ++ or C ++ functions in C.

I ://Example of C ++ referencing C Functions(C ++ calls C, extern "C" to make C ++ connector use the C method for calling function symbols, for example)

// Test. c

# Include <stdio. h>

Void mytest ()

{

Printf ("mytest in. c file OK \ n ");

}

// Main. cpp

Extern "C"

{

Void mytest ();

}

Int main ()

{

Mytest ();

Return 0;

}

You can also add a header file.

// Test. h

Void mytest ()

After extern "C" in main. cpp"

{

# Include "test. h"

}

II: //Reference C ++ functions in C(C calls C ++ and uses extern "C" to tell the compiler to compile and encapsulate the functions defined by extern "C" in the cpp file in C mode, of course, the C ++ syntax in the interface function is still compiled in C ++ mode)

When C references functions and variables in C ++, C ++ functions or variables must be declared in extern "C" {} But cannot use extern "C" in the C language. Otherwise, the compilation fails. (Error: error C2059: syntax error: 'string'. This error has been found online for a long time. The Chinese website has not found any direct explanation for the cause, the reason is that extern "C" is the keyword in C ++. If it is not C, all errors will occur. So how to use it? Read this article or search for extern "C "!)

// Test. cpp

# Include <stdio. h>

Extern "C"

{

Void mytest ()

{

Printf ("mytest in. cpp file OK \ n ");

}

}

// Main. c

Void mytest ();

Int main ()

{

Mytest ();

Return 0;

}

Iii .//Comprehensive use

Generally, we place the function declaration in the header file. When our function may be used by C or C ++, we cannot determine who calls it, this makes it uncertain whether to declare the function in extern "C". Therefore, we can add

# Ifdef _ cplusplus

Extern "C"

{

# Endif

// Function declaration

# Ifdef _ cplusplus

}

# Endif

In this case, this fileWhether it is called by C or C ++, the above error: error C2059: syntax error: 'string' will not appear'.

If we notice that many header files have such usage, such as string. h.

// Test. h

# Ifdef _ cplusplus

# Include <iostream>

Using namespace std;

Extern "C"

{

# Endif

Void mytest ();

# Ifdef _ cplusplus

}

# Endif

In this way, the implementation of mytest () can be placed in. c or. in the cpp file, you can. c or. include "test. h "and then use the functions in the header file without any compilation errors.

// Test. c

# Include "test. h"

Void mytest ()

{

# Ifdef _ cplusplus

Cout <"cout mytest extern OK" <endl;

# Else

Printf ("printf mytest extern OK n ");

# Endif

}

// Main. cpp

# Include "test. h"

Int main ()

{

Mytest ();

Return 0;

}

Another example of C ++ referencing C Functions and variables (from the Internet, google)

Two files:

C file: C. c
**************************************** *******
Int external = "5"; // global variable. The default value is extern.
Int func () // global function. The default value is extern.
{
Return external;
}
**************************************** *******
Cpp file: CPP. cpp
**************************************** *******
# Include "iostream"
Using namespace std;
# Ifdef _ cplusplus
Extern "C"
{
# Endif
Extern int external; // tells the compiler that extern is an int defined in another file and will not be allocated storage space here.
Extern int func (); // although both are in extern "C {} But you still need to explicitly specify the extern. Otherwise, an error is returned.
# Ifdef _ cplusplus // not only functions, but also variables in extern "C.
}
# Endif

Void main (void)
{
Cout <"the value of external in c file is:" <EXTERNAL <ENDL;
External = 10;
Cout <"after modified in cpp is:" <FUNC () <ENDL;
}
**************************************** *******

Extern is a keyword in C/C ++ that indicates the range (visibility) of functions and global variables ., it tells the compiler that the declared functions and variables can be used in this module or other modules.

1. For the extern variable, it is just a declaration of the variable, instead of defining the allocation of memory space. If the variable is defined multiple times, a connection error occurs.

2. In general, the function and global variables referenced by this module to other modules are declared with the keyword extern in the module header file. That is to say, the c file defines that if the function or variable is open to the outside, it will be declared with extern in the H file. Therefore, you only need to include the H file for external files. In addition, this function cannot be found outside of the compilation stage, but no error is reported. This function is found in the target code generated by the definition module in the link phase.

3. The keyword corresponding to extern is static. The global variables and functions modified by it can only be used in this module.

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.