Use an example to illustrate how to use C header files/functions in C ++. For example, I have a C header file named C. the source code file of H and C is called C. c. The content is C. h: # ifndef _ asdfd_included _ # DEFINE _ asdfd_included _ # include <stdio. h> extern int test (int A); # endifc. c: # include "C. H "int test (int A) {printf (" A = % d \ n ", a); return a * A;} Now I want to use C. the function test () provided in C. The name of my c ++ file is. CPP, then the section related to C should be enclosed in the braces of extern "C" {} To see my. CPP: # include <iostream> using namespace STD; extern "C" {# include "C. H "} int main () {int B = 12; B = test (B); cout <"B =" <B <Endl; return 0;}. # include "C. H" is included by extern "C.
The following g ++ compilation will not result in the failure to find the function or structure in the header file.