extern "C" in C and C ++ function calls (2)

  As already understand too extern "C", and further explore the following extern "C" to use.

1, C code contains extern "C", C code can not compile ([1] in C ++, C method call 1 Error)

  code show as below:

// C header files Code CDemo.h 
#include <stdio.h> 
#ifndef C_SRC_DEMO_H   
#define C_SRC_DEMO_H  
 extern  " C "  int F ( int X, int Y);
 #endif   // C_SRC_DEMO_H

 

//C代码CDemo.c
#include "CDemo.h"

int f(int x,int y)
{
        printf("In C file\n");
        printf("x + y = %d\n",x+y);
        return 0;
}

  Under Linux, $: gcc -c CDemo.c compiler pass

  Error message as follows:

  

  Conclusion: In the first file in the C language, its external function can only be specified as extern type, C language does not support extern "C" declaration, included in the .c file compiler syntax error occurs when the extern "C".

 

2, if the function declaration of the function in the C header file (f (int x, int y)) as extern type, including the header files in C ++, and then re-statement and add the extern "C" (extern int f (int x, int y)), C ++ compiler file pass method ([1] in the C ++ calling C 2 error)

code show as below: 

// C header files Code CDemo.h 
#include <stdio.h> 
#ifndef C_SRC_DEMO_H   
#define C_SRC_DEMO_H  
 extern  int F ( int X, int Y);
 #endif   // C_SRC_DEMO_H

 

//C代码 CDemo.c
#include "CDemo.h"

int f(int x,int y)
{
        printf("In C file\n");
        printf("x + y = %d\n",x+y);
        return 0;
}

 

//C++代码 cppDemo.cpp
#include "CDemo.h"
#include <iostream>
extern "C" int f(int x,int y);

int main()
{
        f(2,3);
        return 0;
}

  Under Linux, $: gcc -c CDemo.c can, and generate CDemo.o file

  $: G ++ -c cppDemo.cpp compile error

  Error message:

  

But also note two points:

(1) If you do not re-statement that C ++ code is changed to the following:

//C++代码 cppDemo.cpp
#include "CDemo.h"
#include <iostream>

int main()
{
        f(2,3);
        return 0;
}

  Compiled through, but when the two .o files linked to produce an executable file, an error results in the following:

  $ G ++ cppDemo.o CDemo.o -o cppDemo

  

  This shows that the C ++ file is not found define f function in the C file. This is because the function is different from the name of the C compiler to compile the symbol library name to C ++ compiler produces, such as, C compiler generates the function f the symbol library name is: f, and for the _Z1fii (different compiler may generate different names, but all use the same mechanism to generate a new name called "mangled name") C ++ compiler generated.

(2) whether there is no statement in C code containing the keyword extern, as long as there is in C ++ extern "C", when the link will not go wrong.

references:

[1]  "to write high-quality code: C ++ program to improve the 150 recommendations" Recommendation 19: to understand how to use the C in C ++

Reproduced in: https: //www.cnblogs.com/dpflnevergiveup/p/3289024.html

Guess you like

Origin blog.csdn.net/weixin_33781606/article/details/93268165