Some programming problems about embedded c&&c++ (iv.) __linux

Source: Internet
Author: User

Here's a discussion of C&&c++ 's hybrid programming. The main reference to Bowen http://blog.csdn.net/skdkjzz/article/details/17073339; in fact, mixed programming methods in the writing process is very common, such as MATLAB, Python equals C, C + + mixed programming, Make full use of the strengths of different languages or the library files to be used;

In C and C + + mixed programming is mainly related to extern "C" and C + + files in the _cplusplus;

I. The role of extern "C"

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.
The 2.c++ language supports function overloading, and the C language does not support function overloading. A function is compiled in C + + and the name in the library is different from the C language. Suppose that the prototype of a function is: void foo (int x, inty), the function is _foo by the name of the C compiler in the library, and the C + + compiler produces names like _foo_int_int. C + + provides an extern "C" of the specified symbol, "C." To resolve the name matching problem.
3. A function or variable defined by extern "C" is an extern type; extern is a keyword that indicates the scope (visibility) of functions and global variables in the C + + language, 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, and 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 plusplus) is "C + +", in the header file for C + + documents, the above code means: if the C + + file (*.cpp) suffix, then use extern "C", in C + + Project application is very extensive. 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 a laugh

Third, C calls C + + functions (interface)
1. Design procedures, a total of four documents
Animal.cpp animal.h main.c Makefile
1.1animal.h
[root@localhostcc++] #catanimal. h
#ifndef__ANIMAL_H__//Prevent repeated inclusion
#define__ANIMAL_H__
#ifdef__cplusplus
extern "C" {
#endif
classanimal{
Public
ANIMAL (char*);
~animal ();
char* getname (void);
Private
char* name;
};
Voidprint (void);
#ifdef__cplusplus
}
#endif
#endif//__animal_h__

1.2animal.cpp:c++ file
[root@localhostcc++] #catanimal. cpp
#include "animal.h"
#include <string.h>
UsingNamespace 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;
}
Voidprint (void)//external interface, and must have a non-class method in order to be called by C
{
ANIMAL ANIMAL ("dog");
char* animal_name = Animal.getname ();
cout << "Animal name is:" << animal_name <<endl;
}

1.3MAIN.C:C file
[Root@localhostcc++] #cat main.c
Intmain (void)
{
Print ();
return 0;
}

1.4Makefile
[Root@localhostcc++] #cat Makefile
Main:main.canimal.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@localhostcc++] #make
g++-C Animal.cpp
Gcc-lstdc++ main.c Animal.o-o Main
2.2 Run executable program main
[Root@localhostcc++]#./main
Animalname 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@localhostaa] #cat1. h
#ifndef_1__H_
#define_1__H_
extern voidprint (char*);
#endif

[Root@localhostaa] #cat1. C
#include
#include "1.h"
Voidprint (char* data)
{
printf ("%s\n", data);
}

[Root@localhostaa] #catmain. cpp
extern "C" {
#include "1.h"}
Intmain (void)
{
Print ("hello,world\n");
return 0;
}

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

GCC compiles. cpp files (animal.cpp). The default. cpp file by System is a C + + file format.

About GCC and g++:https://www.zhihu.com/question/20940822/answer/69547180

The compile phase, g++ will call GCC, for C + + code, the two are equivalent, but because the GCC command is not automatic and C + + program use of the library join, so often use g++ to complete the link, for the sake of unification, simply compile/link all use g++, this gives a person an illusion, As if the CPP program can only use g++-like.

Misunderstanding two: GCC does not define __cplusplus macros, and g++ will actually, the macro simply indicates that the compiler will interpret the code in C or C + + syntax, as described above, if the suffix is. C and the GCC compiler is used, the macro is undefined, otherwise it is defined.

Misunderstanding three: compilation can only use GCC, links can only be used g++ strictly speaking, this sentence is not wrong, but it confused the concept, it should be said: The compilation can be used gcc/g++, and links can be used g++ or gcc-lstdc++. Because the GCC command does not automatically connect to libraries used by C + + programs, g++ is usually used to complete the join. In the compile phase, however, g++ automatically invokes GCC, which is equivalent.

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.