在C++程序中包含C头

Arj*_*van 33 c c++ header header-files

我有一个C++程序(.cpp),我希望在其中使用C头文件中存在的一些函数,如stdio.h,conio.h,stdlib.h,graphics.h,devices.h等.

我可以在我的cpp文件中包含stdio.h库,如下所示:#include <cstdio>.如何包含其他库文件?

如何添加graphics.h库?

我正在使用Microsoft Visual Studio 6.0企业版和Turbo C++ 3.0.

Sch*_*ron 60

有关C标准C标头(stdio,stdlib,assert,...)的列表,请在ac之前添加并删除.h.例如stdio.h成为cstdio.

对于其他标题,请使用

extern "C"
{
  #include "other_header.h"
}
Run Code Online (Sandbox Code Playgroud)


Fla*_*ash 35

#ifdef __cplusplus
extern "C"
{
#endif

// your functions here for the header

#ifdef __cplusplus
}
#endif
Run Code Online (Sandbox Code Playgroud)

这种格式应该可以帮助您使用C和C++的头文件而不会出现任何问题......

希望这可以帮助...:)


MBZ*_*MBZ 6

我不确定你需要什么,但如果你想在C++程序中使用老式的C函数,你可以通过删除.h并添加"c"前缀来轻松包含它们.

例如,如果要包含math.h使用

#include <cmath>
Run Code Online (Sandbox Code Playgroud)