[C \ c ++] Using C functions in the C ++ Environment

Source: Internet
Author: User

Extern "C"
When using C functions in the C ++ environment, the compiler often fails to find the C function definition in the OBJ module, resulting in a link failure. How can this problem be solved?
Answer and analysis:

During compilation, C ++ will combine function names and parameters to generate an intermediate function name to solve the problem of function polymorphism. c ++ does not, therefore, the corresponding function cannot be found during the link. In this case, the C function needs to use the extern "C" to specify the link. This tells the compiler to keep my name, do not generate an intermediate function name for the link for me.

Testc. h

# Ifndef _ testc # DEFINE _ testc # ifdef _ cplusplusextern "C" {# endifvoid show (); # ifdef _ cplusplus} # endif/* _ testc */

Testc. c

# Include "testc. H" void show () {printf ("Haha ");}

In vs2008, right-click testc. C and choose Properties> pre-compilation header> no pre-compilation header.

Then you can use it in C ++.

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.