Home>

Analyze the following piece of code:

/*========sum.h=========*/
#ifndef sum_h
#define sum_h
#include<stdio.h>
int sum (int a, int b);
#endif;
/*========sum.c==========/
#include "sum.h"
int sum (int a, int b)
{
int c=a + b;
return c;
}
/*====main.cpp======*/
#include "sum.h"
void mian () {
cout<∑(1,2)<endl;
}

Recall the above three files,Compiled,But the implementation has the following problems:

obj:error lnk2001:Unresolved external symbol "int __cdecl sum (int, int)" (?sum @@ [email protected])

e:\ programming \ grapic \ test \ debug \ test.exe:fatal error lnk1120:1 unresolved external command

What's the problem?In the main.cpp, sum.c is called, that is, the c program is called in the c++ program. At this time, if there is no corresponding processing, a link error will occur.

extern "c" means that the internal symbol names generated by compilation use the c convention. c++ supports function overloading,C does not support it, and the compilation rules are different.Functions compiled in C++ have different names in the symbol library than in C.For example, suppose the prototype of a function is:void foo (int x, int y);After the function is compiled by the C compiler, the name in the symbol library may be _foo, and the C++ compiler will produce something like _foo_int_int Class name (different compilers may generate different names,But both use the same mechanism,The new name generated is called "mangled name"). The name _foo_int_int contains the function name, number of function parameters, and type information.C++ relies on this mechanism to implement function overloading.

So if you call c++ code in c, and how do you call c code in c++?

extern "c" means that the internal symbol names generated by compilation use the c convention.

1. How to call C in C++?

c++ calls c, the function of extern "c" is:let the c++ linker use the c method to find the symbol of the calling function

The written test questions presented at the beginning of this article can be modified as follows:

/*========sum.h=========*/
#ifndef sum_h
#define sum_h
#include<stdio.h>
int sum (int a, int b);
#endif;
/*========sum.c==========/
#include "sum.h"
int sum (int a, int b)
{
int c=a + b;
return c;
}
/*====main.cpp======*/
extern "c"
{
#include "sum.h"
}
void mian () {
cout<∑(1,2)<endl;
}

execution succeed

I believe it's almost clear here

2. How to call C++ in C?

Reference c++ functions in c (c calls c++, using extern "c" tells the compiler to compile the encapsulated interface according to the function defined by extern "c" in the cpp file(Of course, the C++ syntax in the interface function is still compiled in C++)

Execution:test1.obj:error lnk2019:Unresolved external symbol _sum, which is referenced in function _main

e:\ programming \ grapic \ test \ debug \ test.exe:fatal error lnk1120:1 unresolved external command

/*========sum.h=========*/
#ifndef sum_h
#define sum_h
#include<stdio.h>
int sum (int a, int b);
#endif;
/*========sum.cpp==========/
#include "sum.h"
extern "c"
{
int sum (int a, int b)
{
int c=a + b;
return c;
}
}
/*====main.c======*/
#include "sum.h"
void mian () {
cout<∑(1,2)<endl;
}

3. Standard specification writing

Generally we put function declarations in header files,When our function is likely to be used by c or c++, we cannot determine who is calling it,Makes it uncertain whether to declare the function in extern "c", so we can add

#ifdef __cplusplus
extern "c"
{
#endif
//function declaration
#ifdef __cplusplus
}
#endif

Using the above statement form can be comprehensively used.

When referring to functions and variables in c++ in c,C++ functions or variables must be declared in extern "c" {}, but extern "c" cannot be used in the C language, otherwise compilation errors will occur.(Error:error c2059:syntax error:"string", this error has been looking for a long time on the Internet,If the domestic website did not find a direct explanation,The reason is that extern "c" is a keyword in c++,Not c, everything will go wrong.

/*========sum.h=========*/
#ifndef sum_h
#define sum_h
#include<stdio.h>
int sum (int a, int b);
#endif;
/*========sum.cpp==========/
#include "sum.h"
int sum (int a, int b)
{
int c=a + b;
return c;
}
/*====main.c======*/
#include "sum.h"
void mian () {
cout<∑(1,2)<endl;
}
c
  • Previous Introduction to keyboard event design in Java graphical programming
  • Next JS achieves fresh style horizontal drop-down menu effect