我怎么写,可以在C ++程序中使用C头文件? [英] How do I write a C header file that can be used in C++ programs?
问题描述
可能重复:结果
<一href=\"http://stackoverflow.com/questions/4149229/how-to-check-via-the-$p$pprocessor-if-a-c-source-file-is-being-compiled-as-c\">How检查(通过preprocessor)如果C源文件被编译为C ++ code
我试图找到一个标准的宏将测试一个头文件是否被编译为C或C ++。这样做的目的是,头部可以由C或C ++ code包括在内,必须表现略有不同,这取决于。具体做法是:
I'm trying to find a standard macro which will test whether a header file is being compiled as C or as C++. The purpose of this is that the header may be included by either C or C++ code, and must behave slightly differently depending on which. Specifically:
在C,我需要这是在code:
In C, I need this to be the code:
extern size_t insert (const char*);
在C ++中,我需要这是在code:
In C++, I need this to be the code:
extern "C" size_t insert (const char*);
此外,有没有办法避免把的#ifdef周围的每一个声明在头?
Additionally, is there a way to avoid putting #ifdef's around every declaration in the header?
推荐答案
这是正常的支架C头文件如下这样他们就可以在C ++程序中使用。检查你的系统的头文件,如stdio.h中,你可能会看到:
It is normal to bracket C header files as follows so they can be used in C++ programs. Check your system header files such as stdio.h and you will probably see this:
#ifdef __cplusplus
extern "C" {
#endif
...
#ifdef __cplusplus
}
#endif
这篇关于我怎么写,可以在C ++程序中使用C头文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!