A deep understanding of C + + mixed programming _c language

Source: Internet
Author: User
Tags mixed
At work, C、C++Inseparable and do our embedded aspect of course more isC, but sometimes they don't.C++, but isC、C++Mix-and-match ( Mixed Programming) Together, for example,RtpVideo transmission,live555Multimedia playback, etc. areC++Under which he needed to callJrtplibLibrary, and then, for example, I sent that email, and I usedC++Written, defines an EMailObject that contains the Member:Send and receive email address, username, password, etc., and Method: Mail headers, Base64 code and mail to send these operations, very easy to use, so, many times,C++It's pretty good .... But*.cAnd*.cppFiles mixed together, not so simple, knowledge is always: When the side hate less AH!!! Now, let's get to know it slowly.

I.the role of extern "C" (most important)

1. The real purpose of extern "C" is to implement mixed programming of Class C and C + + . extern "C" is a connection-exchange-specified symbol provided by C + + to tell C + + that this code is a C function . the function after extern "C" does not use the name of C + +, but instead uses C. This is because the function name in C + + compiled libraries becomes very long, inconsistent with C generation, resulting in C + + not directly calling C functions.

    2. C + + language supports function overloading, C language does not support function overloading . The name in the library after the function is compiled by C + + differs from the C language. Suppose that the prototype of a function is: void foo (int x, int y); the function was compiled by the C compiler in the library with the name _foo , and the C + + compiler will produce something like _foo_int_int , and so on The name of the . C + + provides a C connection Exchange specified symbol extern "C" to resolve the name matching problem.

3. a function or variable that is qualified by extern "C" is an extern type ;extern is C + + A keyword in a language that indicates the scope (visibility) of a function and a global variable, which tells the compiler that its declared functions and variables can be used in this module or in other modules.   Variables and functions that are decorated by extern "C" are compiled and connected in a C-language manner.

4. the keyword corresponding to extern is static , the global variables and functions that are modified by it can only be used in this module. Therefore, a function or variable can only be used by this module, it cannot be decorated by extern "C" .

Second,the extern "C" and __cplusplus

#ifdef __cplusplus
extern "C" {
#endif
#ifdef __cplusplus
       }
#endif
cplusplus(c plus plus) is "C + +", for C + + In the header file of the document, the code above means that if it is a C + + file (*.cpp) suffix, use extern "C", which iswidely used in C + + projects. Even if compiled with the GCC compiler, the function name is type C, such as _foo. Personally think, understand these two keywords, especially the understanding of extern "C" (again stressed, not too, hehe), the next mixed programming is almost, hahaha ....

Third,C calls C + + functions (interface)

1. Design procedures, a total of four documents

Animal.cpp animal.h main.c Makefile

1.1 Animal.h

[Root@localhost cc++]#cat animal.h
#ifndef __animal_h__ // prevent repeated inclusions
#define __animal_h__
#ifdef __cplusplus
extern "C" {
#endif

Class ANIMAL{
Public
ANIMAL (char*);
~animal ();
char* getname (void);
Private
char* name;
};
void print (void);
#ifdef __cplusplus
}
#endif
#endif//__animal_h__

1.2 Animal.cpp:C + + files

[Root@localhost cc++]#cat Animal.cpp
#include "animal.h"
#include <iostream>
using namespace Std;
Animal::animal (char* data)// constructor
{ name = new CHAR[64];
strcpy (name, data);
}
Animal::~animal () // destructor

{
if (name)
{

                delete[] Name
                name = NULL;
       }
}
char* animal::getname (void)
{         return name;
}
void print (void) // external interface, and must have a non-class method To be C
{
        ANIMAL ANIMAL ("Dog" );
        char* animal_name = Animal.getname ();
        cout << "Animal name is:" << animal_name << Endl;
}

1.3 main.c:C file

[Root@localhost cc++]#cat main.c
int main (void)

{ print ();
return 0;
}

1.4 Makefile

[Root@localhost cc++]#cat Makefile
MAIN:MAIN.C ANIMAL.O
GCC -lstdc++ main.c animal.o-o Main

Animal.o:animal.h
g++-C Animal.cpp
. Phony:clean
Clean
-RM ANIMAL.O Main

2. Test

2.1 Build Executable program main

[Root@localhost cc++]# make
g++-C Animal.cpp
Gcc-lstdc++ main.c Animal.o-o Main

2.2 Run executable program main

[Root@localhost cc++]# ./main
Animal name is:d og

c C- function call

Should this relatively simple, I will not write more, directly write code.
Total three files:1.h 1.c main.cpp
[Root@localhost aa]#cat 1.h
#ifndef _1__h_
#define _1__h_
extern void print (char*);
#endif

[Root@localhost aa]#Cat 1.c
#include <stdio.h>
#include "1.h"
void print (char* data)
{
printf ("%s\n", data);
}
[Root@localhost aa]#cat main.cpp

extern "C" {
#include "1.h"}
int main (void)
{
Print ("hello,world\n");
return 0;

}
Gcc–c 1.c
g++ main.cpp 1.O

Then ,/a.out, we can also appear the magic of the Hello,world, C+ + call code is very simple, but C call C + + interface can give me tired, bitter ah. This is the -lstdc++ after gcc, undefined reference to ' __gxx_personality_v0 ' this error. is because you compile the. cpp file (animal.cpp) with GCC.the default . cpp file by System is the file format for C + +. Of course, when mixing, I also encountered some other problems, are some small problems, if the above explanation is not enough to let you solve c\c++ mixed programming problems, you can contact me oh.

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.