extern "C"

  In many C / C ++ header files, you may often see the use of the code extern "C" {} contains up. The presence of such grammar is to make the C and C ++ code to be compatible. compatible? C ++ to C is originally not compatible with it? Indeed, at the level of grammar, C ++ supports almost all the C syntax. But here is compatible object files or library-level compatible, including compatible in both cases. One is to call existing C code libraries written in C ++ code; the other is to use C to write code, but want to be able to make the C ++ code can call.
  To continue the discussion extern "C", you would have to mention name mangling.
  We know that, compared to C, C ++ supports function overloading, namespaces support, support classes and member functions. Function overloading allows different functions we declare the name of the same parameters, namespace (namespace), and partitioning of the name (or symbol) different domains so that we can use the same names in different domains. In order to achieve such properties, C ++ will use the name mangling. In other words, C ++ compiler when compiling C ++ files to each function name "upset" and into the domain of the function, parameter type, another name calling convention-related (because the assembly level code, all symbols are the same domain). C does not exist because of these characteristics, it is usually not on the C compiler symbol mangling.
  Because of this mangling of C ++ in the presence of the C and C ++ are not unified on the symbol, not directly call each other. For example such a function in C,

//~ add.h
int add(int, int);
//~ add.c
int add(int a, int b)
{
    return a + b;
}

Use gcc compiler add.c. Then in C ++ code directly call the add function,

//~ main.cpp
#include <add.h>
int main()
{
    int a = add(0, 1);
    return 0;
}

In this way compile main.cpp,

$ gcc -c add.c
$ g++ -c main.cpp
$ g++ main.o add.o -o main
main.o: In function `main':
main.cpp:(.text+0x13): undefined reference to `add(int, int)'
collect2: ld returned 1 exit status

Error add (int, int) is found. After main.cpp compiled into assembler, see the call to add code

movl    $1, %esi # 参数入栈
    movl    $0, %edi # 参数入栈
    call    _Z3addii # 调用add, 注意符号
    movl    %eax, -4(%rbp) # 返回值赋给int a

  Solution to this situation is to use extern "C", in the add.h, add the statement into extern "C" {} in. In this way, g ++ will add not be mangling, the linker will find in add.o and add links to main.o. Typically, the header C code (including libc header files, such as string.h), the declaration of the function is generally the case,

//~ add.h
#ifdef __cplusplus
extern "C" {
#endif
int add(int, int);
#ifdef __cplusplus
}
#endif

Thus, if the C code comprising add.h, it would be declared directly contains the add function. If C ++ code contains add.h, then extern "C" it will be introduced. About name mangling, please refer to here , and here .

Reproduced in: https: //my.oschina.net/dake/blog/196624

Guess you like

Origin blog.csdn.net/weixin_34174105/article/details/91508111