extern "C" call test and validation-2015.01.06

Source: Internet
Author: User
Tags modifiers visual studio 2010

1 Invocation Scenario Description

In the previous article on the extern "C" principle and usage, it explains in detail why extern "C" is required and how it can be used to solve the problems encountered in mixed C and C + + programming. Next, use the example validation method to verify that the C and C + + functions are named in various cases when the function is compiled and linked with the extern "C" and not the extern "C" modifier. The following situations are mainly formed:

1, C function call C function, do not use extern "C" decoration

2, C function call C function, using the extern "C" decoration, in fact, the essence of the same as 1, because only the C function call, so there is no macro defined __cplusplus, extern "C" decoration does not work

3, C + + functions call the function, do not use extern "C" decoration

4. C + + function calls the function, using extern "C" modifier

5, C + + call C + + function, do not use extern "C" decoration

6. C + + calls C + + functions, using extern "C" modifier

7, C call C + + function, do not use extern "C" decoration

8, C call C + + functions, using extern "C" decoration

Compilation Environment Visual Studio 2010, function File Description: C function header file and source file respectively command for c.h and c.c,c++ function header file and source file named Cpp.h and cpp.cpp,c call file command for example.c,c++ The Invoke File command is example.cpp.

1 c function calls C function, does not use extern "C" modifier

File Structure view

where c.h file content

#ifndef _c_h_ #define _c_h_void  c_fun (); #endif

C.C File Contents

" c.h "  <stdio.h>void  C_fun () {    printf ("This isC function\n " );}

EXAMPLE.C File Contents

" c.h " int Main () {    c_fun ();     return 0 ;}

Using DUMPBIN to view the compiled obj file, the C.obj symbol table information is as follows:

Where the C_fun compiled function name is _c_fun.

The EXAMPLE.OJB symbol table information is as follows:

The function name linked by the main function is also _c_fun when the link is called, so the C function calls the C function, and the compile link uses the C naming convention.

2 C function calls C function, using extern "C" modifier

Add the extern "C" modifier to the c.h header file in Scenario 1, as follows:

#ifndef _c_h_ #define _c_h_#ifdef __cplusplusextern"C"  {# endifvoid  c_fun (); #ifdef __cplusplus}#endif#endif

The C.OJB symbol table information is as follows:

Consistent with the C.obj symbol table information in Scenario 1. Because scenario 2 is essentially the same as 1 because only the C function is called, so there is no macro that defines the __cplusplus, and the extern "C" modifier does not work

3 C + + functions call the C. Function and do not use extern "C" modifiers

With scenario 1 keep all file contents intact, only modify the calling file Example.c file name to Example.cpp

The above can be compiled through, but the link does not pass, prompted to find the following symbol information:

To view example.obj symbol table information:

In the EXAMPLE.OJB symbol table information, see the _main function call C_fun function name already in case 1 of the _c_fun into [email protected]@YAXXZ, and in C.obj, C_fun function name is still _C_ Fun, and therefore cannot be linked successfully.

4 C + + functions call the C. Function, using extern "C" modifiers

On the basis of scenario 3, in the C.h header file, add the extern "C" modifier, which tells C + + compilation not to compile with C + + compilation rules, but instead uses C compilation rules.

The c.h file is detailed as follows:

#ifndef _c_h_ #define _c_h_#ifdef __cplusplusextern"C"  {# endifvoid  c_fun (); #ifdef __cplusplus}#endif#endif

To view example.obj symbol table information:

As you can see, with the extern "C" modifier, the symbolic information that is linked in the _main function is _c_fun named according to the C compilation rule, so the link is compiled.

5 C + + calls C + + functions without using extern "C" adornments

File Structure View:

Cpp.h Header File Contents:

#ifndef _cpp_h_ #define _cpp_h_void  cpp_fun (); #endif

Cpp.cpp Source file Contents:

" Cpp.h "  <stdio.h>void  Cpp_fun () {    printf ("this isC + + function\n  ");}

Example.cpp Call file Contents:

" Cpp.h " int Main () {    cpp_fun ();     return 0 ;}

Cpp.obj Symbol Table information:

Example.obj Symbol Table information:

From the above symbol table information can be seen, compiled using the C + + compiler naming rules.

6 C + + calls C + + functions, using extern "C" adornments

In addition to adding the extern "C" modifier to the Cpp.h header file, the other file contents and file structure are consistent with scenario 5.

The contents of the Cpp.h file are as follows:

#ifndef _cpp_h_ #define _cpp_h_#ifdef __cplusplusextern"C"  {# endifvoid  cpp_fun (); #ifdef __cplusplus}#endif#endif

Cpp.obj Symbol Table information:

Example.obj Symbol Table information:

As seen from the symbol table information above, the compile link uses the C compile naming convention after using the extern "C" modifier.

7 C calls C + + functions and does not use extern "C" adornments

The file content is consistent with scenario 5 and only modifies the Example.cpp file name to Example.c, as follows:

Cpp.obj Symbol Table information:

Example symbol table information:

During the linking process, the function information linked in the _main function is _cpp_fun, and the Cpp_fun function in the Cpp.obj symbol table information is named [email protected]@YAXXZ, so there is a link error as follows.

8 C calls C + + functions, using extern "C" adornments

On the basis of scenario 7, add the extern "C" modifier to the Cpp.h header file, with the following code:

#ifndef _cpp_h_ #define _cpp_h_#ifdef __cplusplusextern"C"  {# endifvoid  cpp_fun (); #ifdef __cplusplus}#endif#endif
Cpp.obj Symbol Table information:

Example.obj Symbol Table information:

As can be seen from the above symbol table information, compile links are compiled with C naming rules, so you can compile the link through.

extern "C" call test and validation-2015.01.06

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.