Method 1: add the extern keyword to the C function and use the extern "C" {include "C. H"} in the corresponding C ++ file to introduce the C header file.
---------/* Max. H */-------------------------
# Ifndef _ max_h _ # DEFINE _ max_h _ Extern int max (int x, int y ); # Endif |
# Ifndef _ max_h _ # DEFINE _ max_h _ Extern int max (int x, int y ); # Endif |
--------- // * Max. C */---------
# Include "Max. H" Int max (int x, int y) { Return x> Y? X: Y; } |
------------/* Test. cpp */-------------
Extern "C "{ # Include "Max. H" } Int main (void) { Int A = 2, B = 90; Int C = 0; C = max (A, B ); } |
Method 2: Use C ++ macro _ cplusplus
----------/* Max. H */-------------
# Ifndef _ max_h _ # DEFINE _ max_h _ # Ifdef _ cplusplus Extern "C "{ # Endif Extern int max (int x, int y ); # Ifdef _ cplusplus } # Endif # Endif/* _ max_h _*/ |
--------------/* Max. C */----------------
# Include "Max. H" Int max (int x, int y) { Return x> Y? X: Y; } |
-----------/* Test. cpp */---------------
# Include "Max. H" Int main (void) { Int A = 2, B = 90; Int C = 0; C = max (A, B ); } |
Because the _ cplusplus macro is not defined in C, in the C compiler's opinion, Max. H is:
Extern int max (int x, int y );
In C ++'s view, Max. H is:
Extern "C "{
Extern int max (int x, int y );
}