Summary of extern "C" usage

Source: Internet
Author: User

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 );
}
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.