C ++ knowledge point-the role of extern "C"

Source: Internet
Author: User

In C ++ or CSource codeThe main reason is that the name adaptation method of each compiler is different. The following is from:
Http://blog.csdn.net/vinep/archive/2009/02/17/3899780.aspx an original article, paste it, ready to view.
 

Taking int func (INT, INT) as an example, the C compiler will adapt the name to _ FUNC, the C ++ compiler is adapted to _ func_int_int or _ funcii (different compilers ). If this function is compiled into a library in C, the function name in the target file is _ FUNC. When C ++ is called in this function, the C ++ compiler will find _ funcii in the target file. The result cannot be found and an error occurs. To prevent this problem, add an extern "C" before the function declaration to the C ++ compiler and do not modify the name, instead, go directly to _ FUNC. The C and C ++ compilers have different modifiers for function names. To ensure that functions written in C and C ++ can be called by each other, this

Link indicator

For functions compiled in C ++, if you want to use them in C, we usually add extern "c" to modify the function name according to the C style during compilation. Normally, functions written in C may be declared in the following form if they are used in other languages: # ifdef _ cplusplusextern "C" {# endif/***** some declaration or so *****/# ifdef _ cplusplus} # endif/* end __ cplusplus */That is, extern "C" has been added during the declaration without the need for calling. For example, Calling C library functions in VC ++ is not used as a special declaration.Usage of extern "C"

 

 

 

 

 

 

 

 

1. It can be a single statement
Extern "C" Double SQRT (double );
2. It can be a composite Statement, which is equivalent to adding extern "c" to all declarations in the composite statement"
Extern "C"
{
Double SQRT (double );
Int min (INT, INT );
}
3. the header file can be included, which is equivalent to adding extern "c" to all declarations in the header file"
Extern "C"
{
# I nclude <cmath>
}
4. You cannot add extern "c" to the function.
5. If a function has multiple declarations, you can add extern "c" to them, or you can only add them to the first declaration. The subsequent declaration will accept the rules of the first link indicator.
6. In addition to extern "C", there are extern "Fortran" and so on.

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.