Including C headers inside a C++ program

纵然是瞬间 提交于 2019-11-26 10:58:12

问题


I have a C++ program (.cpp) inside which I wish to use some of the functions which are present inside the C header files such as stdio.h, conio.h, stdlib.h, graphics.h, devices.h etc.

I could include the stdio.h library inside my cpp file as : #include <cstdio>. How do I include the other library files?

How do I add the graphics.h library?

I\'m using Microsoft Visual Studio 6.0 Enterprise Edition and also Turbo C++ 3.0.


回答1:


For a list of C standard C headers (stdio, stdlib, assert, ...), prepend a c and remove the .h. For example stdio.h becomes cstdio.

For other headers, use

extern "C"
{
  #include "other_header.h"
}



回答2:


#ifdef __cplusplus
extern "C"
{
#endif

// your functions here for the header

#ifdef __cplusplus
}
#endif

This format should help you use the header files for both C and C++ without any problem ...

Hope this helps...:)




回答3:


I'm not sure what you need exactly, but if you want to use old fashioned C functions inside you C++ program, you can easy include them by removing the .h and add a "c" prefix.

for example if you want to include math.h use

#include <cmath>



回答4:


Just include them inside a extern "C" block an they should work like expected.




回答5:


You can #include them using their original names. #include <stdio.h> works just fine in C++.



来源:https://stackoverflow.com/questions/3329159/including-c-headers-inside-a-c-program

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!