How to call C code in C ++

Source: Internet
Author: User

I once summarized an article (http://www.cppblog.com/franksunny/archive/2007/11/29/37510.html), about how to call C ++ code in C, at that time did not fully expand, just simple to do the next debugging, recently, I saw that a question requires that the code in C and C ++ be called each other. The results are obtained through extern.
But there are some differences.

Let's give a brief review of Calling C ++ code in C:

1. For non-class member functions in C ++, you can simply add the extern "C" before the function declaration. Generally, the function declaration is located in the header file, of course, you can also put the Declaration and Function Definition together in cpp. If no declaration is made, add extern directly before the definition.
"C" can also

2. For C ++ class member functions, you need to create another cpp file to wrap the functions to be called.

For examples of the preceding two items, see the article on how to call C ++ code in C.

To call C code in C ++, perform the following operations:

For function code in C, either modify the header file of C code and add extern to the Declaration when it is included in C ++ code.
"C" or re-declare the C function in the C ++ code, and add the extern
"C" header.

Through the above instructions, I understand that the header of the extern "C" must be added to the code file of C ++ to take effect.

 

The following analyzes the substantive causes of this phenomenon:

The C compiler compiles a function without the type information of the function. It only contains the function symbol name. For example, the C compiler compiles the int a (float x) function into a symbol similar to _, the C connector can be connected successfully as long as it finds the symbol for calling the function. It assumes that the parameter type information is correct, which is a disadvantage of the C compiling connector. In order to implement function overloading, the C ++ compiler will include the type information of the function during compilation. For example, it may compile the above a function into a symbol such as _ a_float to achieve overloading, note that it still does not contain returned value information, which is one of the reasons why C ++ does not support using function return values to distinguish function overloading. Of course, it is also important for function users to handle function return values (for example, ignore.

Based on the above, C ++ needs to first encapsulate calls to C ++ classes into C functions for C calls, so the function of extern "C" is to let the compiler know about it, then compile and connect the encapsulated functions in C Language (usually compile the encapsulated functions in C ++ compiler in C ++ mode, and use extern
After "C", the compiler compiles and encapsulates the interface according to the C method. Of course, the C ++ syntax in the interface function is still compiled in the C ++ method. For the C language part-the caller, or compile it in C language. Compile the C ++ Interface part and C part respectively, and then connect to C to call C ++ ). Instead, C ++ calls the C function, extern
The function of "C" is to use the C method for the C ++ connector to find the symbol used to call the function, that is, use _ a instead of _ a_float to call the function.

 

For more information, seeHttp://www.cppblog.com/Files/franksunny/CCallCpp.rar

Note: If you encounter an error similar to "fatal error C1010: unexpected end of file while looking for precompiled header directive" when compiling an attachment with VC6.0, please copy bb. right-click the file in the project and select setting. In the c/c ++ column, select PreCompiled
Headers, set the first option, and select not to use the pre-compiled header.

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.