An instance method that calls each other between C and C + +

Source: Internet
Author: User
Tags instance method naming convention wrapper

  This article mainly introduces the C and C + + call each other instance method, we refer to the use of the bar

1. Export C functions for projects in C or C + +   If you are using a DLL written in C that you want to export a function to a C or C + + module, you should use the __cplusplus preprocessor macro to determine the language you are compiling. If used from a C + + language module, these functions are declared with a C link. If you use this technique and provide a header file for your DLL, these functions can be used by C and C + + modules intact.   The following code demonstrates the header files that can be used by C and C + + client applications:     Code is as follows://MyCFuncs.h #ifdef __cplusplus extern "C" { //only nee D to export C interface if              //used by C + + source code #endif   __DEC Lspec (dllimport) void Mycfunc (); __declspec (dllimport) void Anothercfunc ();   #ifdef __cplusplus} #endif       Mycfunc () and Anothercfunc () are exported functions for the C language DLL.   If you need to link a C function to a C + + executable, and the function declaration header file does not use the above technique, add the following in the C + + source file to prevent the compiler from modifying the C function name:       Code is as follows: extern "C"   {#include "MyCHeader.h"}     The code tells the compiler that "MyCHeader.h" is written in C and does not modify the C function name in the header file, otherwise the connection will not be found.   2, export C + + functions for C language Projects   If you have a function in a DLL written in C + + that you want to access from the C language module, you should declare these functions by using C links instead of C + + links. Unless otherwise specified, the C + + compiler uses the C + + type security naming convention (also known as name Decoration) and the C + + calling convention (which can be difficult to invoke from C using this calling convention).   To specify the C link, specify extern "C" for the function declaration in the DLL. For example:     Codeas follows: extern "C" __declspec (dllexport) int MyFunc (long parm1);     in C language functions can not directly invoke C + + code, if you want to call, you could do a wrapper, such as Call_lib_cppfunction, its declaration and implementation are as follows:     code is as follows:// wrapper function extern "C" void Call_lib_cppfunction (lib* p, dataattribute* dataattribute)   {    P->da Fun (Dataattribute); }   //Dafun is the implementation of our C + + code void Lib::d afun (dataattribute* dataattribute) {    map<string, mmsinfo& Gt;::iterator it;    //...}  
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.