C and C + + mixed programming

Source: Internet
Author: User

Reprint Link: HTTP://FORUM.EEPW.COM.CN/THREAD/228871/1

extern "C" indicates that the compiled generated internal symbol name uses the C convention. C + + supports function overloading, and C is not supported, and the compilation rules are not the same. A function is compiled by C + + in a symbol library with a different name than the C language. For example, suppose a function has a prototype: void foo (int x, int y); The function is compiled by the C compiler in the symbol library name may be _foo, and the C + + compiler will produce names such as _foo_int_int (different compiler may produce different names, but all adopt the same mechanism, the resulting new name is called "Mangled Name"). _foo_int_int Such a name contains the function name, the number of function parameters and type information, C + + is to rely on this mechanism to implement the function overload. The following example shows how to use C's functions in C + +, or use C + + functions in C.
One://c++ reference to C function Example (c + + calls C,extern "C" is: let C + + connector to find the call function of the symbol in the way of C, such as)
TEST.c
#include
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 file to the above
Test.h
void MyTest ()
In the main.cpp after the extern "C"
{
#include "test.h"
}

Second://reference C + + functions in C (c call C + +, use extern "C" is to tell the compiler to the CPP file in the extern "C" definition of functions in accordance with C to compile the encapsulation interface, of course, the interface function inside the C + + syntax or compiled in C + + mode)
When referring to functions and variables in C + + language, a function or variable of C + + is declared in extern "C" {}, but no extern "C" can be used in C language, or a compilation error occurs. (Errors: Error C2059:syntax error: ' String ', this error in the online search for a long time, the domestic site did not find a direct explanation for the reason, because the extern "C" is the key word in C + +, not C, all will be wrong. How to use it. Read this article, or search extern "C". )
Test.cpp
#include
extern "C"
{
void MyTest ()
{
printf ("MyTest in. cpp file ok\n");
}
}
Main.c
void MyTest ();
int main ()
{
MyTest ();
return 0;
}

three. Comprehensive use
Generally we put the function declaration in the header file, when our function may be used by C or C + +, we can not determine who is called, so we are not sure whether to declare the function in extern "C", so we could add
#ifdef __cplusplus
extern "C"
{
#endif
function declaration
#ifdef __cplusplus
}
#endif
In this case, this file can be called either by C or C + +, and there will not be the error: Error C2059:syntax error: ' String '.
If we notice that many headers have this usage, such as string.h, and so on.
Test.h
#ifdef __cplusplus
#include
using namespace Std;
extern "C"
{
#endif
void MyTest ();
#ifdef __cplusplus
}
#endif
In this way, you can place the implementation of mytest () in a. C or. cpp file, and you can use the functions in the header file after you include "Test.h" in a. C or. cpp file without a compilation error.
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;
}
Examples of C + + reference functions and variables (from the Internet, you can Google)
Two files:
C File: c.c
***********************************************
int external= "5"; Global variable, the default is extern.
int func ()//global function, which defaults to 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 a different file and does not allocate storage space for it.
extern int func (); Although these two are in the extern "C" {}, but still want to explicitly specify extern, otherwise an error.
#ifdef __cplusplus//Not just functions, variables should also be placed in extern "C".
}
#endif


void Main (void)
{
cout<< "The value of external in C file is:" < external=10;
cout<< "modified in CPP:" <}
***********************************************
extern is a keyword that indicates the scope (visibility) of functions and global variables in the C/s + + language. It tells the compiler that its declared functions and variables can be used in this module or in other modules.
1. For an extern variable, it is simply a declaration of a variable that is not in the definition of allocating memory space. If the variable is defined multiple times, there will be a connection error
2. Typically, the module's header file provides the keyword extern declaration for functions and global variables that are referenced by this module to other modules. That is, the C file definition, if the function or variable with open to the outside, then in H file with extern to declare. So the external file is only available with the H file include. and the compile phase, the outside is unable to find the function, but not the error. The link phase finds this function from the target code that defines the module generation.
3. The keyword that corresponds to extern is static, and global variables and functions that are decorated with it can only be used in this module.

C + + call

Here is the assumption that the old C program library

Header file for C

* *-----------c.h--------------* *

#ifndef _c_h_

#define _c_h_

extern int Add (int x, int y);

#endif

source files for C

* *-----------C.C--------------* *

int add (int x, int y) {

return x+y;

}

Calls to C + +

* *-----------cpp.cpp--------------* *

#include "c.h"

void Main ()

{

Add (1, 0);

}

This compilation produces an error cpp.obj:error lnk2001:unresolved external symbol "int __cdecl Add (int,int)" ([email=?add@ @YAHHH @z]? add@ @YAHHH @z[/email]), because the target module for add cannot be found

This reminds me of C + + overloaded function naming and C function naming way, let us review: C in the function after compiling the name will be "_", such as the Add function compiled into obj file is actually named _add, and C + + naming is different, To implement a function overload the same function name add is compiled into different names because of the difference in parameters

For example

int add (int, int) ==>add@ @YAHHH @z,

Float Add (float, float) ==>add@ @YAMMM @z,

The above is the name of the VC6, different compilers will be different, in short, the same function names of different parameters are compiled into different target names, so that the function overload is to call the specific function.

In the compiler cpp.cpp the compiler finds add (1, 0) in the CPP file, and the function declares extern int add (int x, int y), and the compiler decides to find the [email=add@ @YAHHH @z]add@ @YAHHH @z[/ Email], but he couldn't find it, because C's source file put extern int add (int x, int y), and compiled into _add;

In order to solve this problem C + + adopted the extern "C", which is our theme, want to take advantage of the previous C program library, then you have to learn it, we can see the following standard header file you will find that many headers have the following structure

#ifndef __h

#define __h

#ifdef __cplusplus

extern "C" {

#endif

extern int F1 (int, int);

extern int F2 (int, int);

extern int f3 (int, int);

#ifdef __cplusplus

}

#endif

#endif/*__h*/

If we copy the header file, we can get

#ifndef _c_h_

#define _c_h_

#ifdef __cplusplus

extern "C" {

#endif

extern int Add (int, int);

#ifdef __cplusplus

}

#endif

#endif/* _c_h_ * *

This compiles

* *-----------C.C--------------* *

int add (int x, int y) {

return x+y;

}

At this point the source file for *.c,__cplusplus is not defined, extern "C" {} is not effective at this time for C he saw just extern int add (int, int);

The Add function is compiled into _add (int, int);

and compile C + + source files

* *-----------cpp.cpp--------------* *

#include "c.h"

void Main ()

{

Add (1, 0);

}

The source file is defined as *.cpp,__cplusplus, and for C + + he sees an extern "C" {extern int add (int, int);} The compiler will know the Add (1, 0), the called C-style function, you will know to go to C.obj to find _add (int, int) instead of [email=add@ @YAHHH @z]add@ @YAHHH @z[/email];

This is why the DLL often see extern "C" {},windows is compiled in C language he should first consider that C can correctly invoke these DLLs, while users may use C + + and extern "C" {} will be useful

When the original C language written in the header file does not consider this issue, you can write this:

#include

#include

extern "C" {

#include "Sift.h"

#include "Imgfeatures.h"

#include "kdtree.h"

#include "Utils.h"

#include "xform.h"

}

This can be used in C + + written by someone else in C language things.

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.