在 C++ 程序中包含 C 头文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3329159/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Including C headers inside a C++ program
提问by Arjun Vasudevan
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、graphics.h、devices.h 等。
I could include the stdio.h library inside my cpp file as : #include <cstdio>
.
How do I include the other library files?
我可以有我的cpp文件里面的文件stdio.h库:#include <cstdio>
。如何包含其他库文件?
How do I add the graphics.h library?
如何添加 graphics.h 库?
I'm using Microsoft Visual Studio 6.0 Enterprise Edition and also Turbo C++ 3.0.
我正在使用 Microsoft Visual Studio 6.0 企业版和 Turbo C++ 3.0。
回答by Scharron
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 等)的列表,请添加 ac 并删除 .h。例如 stdio.h 变成 cstdio。
For other headers, use
对于其他标题,请使用
extern "C"
{
#include "other_header.h"
}
回答by Flash
If you put this inside your headers:
如果你把它放在你的标题中:
#ifdef __cplusplus
extern "C"
{
#endif
// your normal definitions here
#ifdef __cplusplus
}
#endif
Then it will work for both C and C++ without any problem ...
然后它将适用于 C 和 C++ 没有任何问题......
Hope this helps...:)
希望这可以帮助...:)
回答by MBZ
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
例如,如果您想包括math.h
使用
#include <cmath>
回答by Axel Gneiting
Just include them inside a extern "C"
block an they should work like expected.
只需将它们包含在一个extern "C"
块中,它们就会按预期工作。
回答by Steven
You can #include
them using their original names. #include <stdio.h>
works just fine in C++.
您可以#include
使用它们的原始名称。#include <stdio.h>
在 C++ 中工作得很好。