C + + calls to function

Source: Internet
Author: User

In order to prevent unscrupulous site crawler crawling articles, hereby identified, reproduced please indicate the source of the article. Laplacedemon/shijiaqi.

Http://www.cnblogs.com/shijiaqi1066/p/6349243.html

There is a program, the code directory is as follows:

.
|--Calc
| |--calc.c
| '--calc.h
'--main.cpp

If main.cpp needs to call a function in Calc.h (CALC.C is C code)

After the traditional C programming requires # include "calc/calc.h", the function is declared and then called.

But since Main.cpp is a C + + code. The g++ compiler cannot compile by using the C-mode call.

Solution One:
If the amount of code in Calc is very small, or the code is written by itself can directly use C + + compatible with the characteristics of C, the CALC.C changed to Calc.cpp. can be compiled.

Solution Two:
At the top of each function in calc.h, add: extern "C"

The simpler scenario is:

extern " C " {     void fun1 (int  arg1);      void fun2 (intint  arg2);      void fun3 (intint int arg3);}

If you are not sure whether the current compilation environment is C or C + +, you can:

extern " C " {#endifvoid fun1 (intvoid fun2 (intint)    void fun3 (int int int arg3); #ifdef __cplusplus}    #endif

Workaround Three:
What if someone else has written a header file that we can't modify? Rewrite a header file that is specifically used by C + +.

Example: Writing a header file cpp_calc.h

extern " C "  "calc.h";}

In order to prevent unscrupulous site crawler crawling articles, hereby identified, reproduced please indicate the source of the article. Laplacedemon/shijiaqi.

Http://www.cnblogs.com/shijiaqi1066/p/6349243.html

C + + calls to function

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.