在c++程序中包含C标头

时间:2022-07-02 15:04:25

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.

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

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

我可以把它包括在里面。我的cpp文件中的h库为:#include 。如何包含其他库文件?

How do I add the graphics.h library?

如何添加图形。h图书馆吗?

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

我正在使用Microsoft Visual Studio 6.0 Enterprise Edition和Turbo c++ 3.0。

5 个解决方案

#1


43  

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

对于C标准C标头(stdio, stdlib, assert,…)的列表,请在前面加上C并删除.h。例如它的。h变得cstdio。

For other headers, use

对于其他头文件,使用

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

#2


26  

#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 ...

这种格式应该可以帮助您毫无问题地使用C和c++的头文件……

Hope this helps...:)

希望这有助于…:)

#3


3  

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

只要将它们包含在一个外部的“C”块中,它们就应该像预期的那样工作。

#4


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.

我不确定您确切地需要什么,但是如果您想在c++程序中使用过时的C函数,您可以通过删除.h并添加“C”前缀来轻松地包含它们。

for example if you want to include math.h use

例如,如果你想包含数学。h使用

#include <cmath>

#5


1  

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

您可以使用它们的原始名称来包含它们。# include < stdio。h>在c++中很好用。

#1


43  

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

对于C标准C标头(stdio, stdlib, assert,…)的列表,请在前面加上C并删除.h。例如它的。h变得cstdio。

For other headers, use

对于其他头文件,使用

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

#2


26  

#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 ...

这种格式应该可以帮助您毫无问题地使用C和c++的头文件……

Hope this helps...:)

希望这有助于…:)

#3


3  

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

只要将它们包含在一个外部的“C”块中,它们就应该像预期的那样工作。

#4


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.

我不确定您确切地需要什么,但是如果您想在c++程序中使用过时的C函数,您可以通过删除.h并添加“C”前缀来轻松地包含它们。

for example if you want to include math.h use

例如,如果你想包含数学。h使用

#include <cmath>

#5


1  

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

您可以使用它们的原始名称来包含它们。# include < stdio。h>在c++中很好用。