如何编译包含 extern "C" 指令的 C 代码?
How to compile C code containing extern "C" directive?
在 Visual Studio 2008 年,文件 dbgheap.c
中编码了一个很好的调试堆。它实现了_CrtDumpMemoryLeaks()
.
等功能
我注意到该文件包含 extern "C"
指令,Microsoft 自己的编译器似乎不允许这些指令。代码如下:
extern "C" _CRTIMP int __cdecl _CrtDumpMemoryLeaks(
void
)
{
/* only dump leaks when there are in fact leaks */
如果您尝试自己编译,只会产生错误。显然有允许这种语法的编译器标志,但有人知道这些标志是什么吗?
在这种情况下,事实证明,虽然代码是 100% 合法的 C,但除了这些 extern "C"
限定符外,它还可以使用标志 /TP
进行完美编译,这意味着 "compile as C++ disregarding the file name."
所以,我想这就是他们所做的。
我认为 MSFT 只是忽略了将 extern "C"
限定符放在某种依赖于 __cplusplus
标志或其他东西的条件编译中。
在 Visual Studio 2008 年,文件 dbgheap.c
中编码了一个很好的调试堆。它实现了_CrtDumpMemoryLeaks()
.
我注意到该文件包含 extern "C"
指令,Microsoft 自己的编译器似乎不允许这些指令。代码如下:
extern "C" _CRTIMP int __cdecl _CrtDumpMemoryLeaks(
void
)
{
/* only dump leaks when there are in fact leaks */
如果您尝试自己编译,只会产生错误。显然有允许这种语法的编译器标志,但有人知道这些标志是什么吗?
在这种情况下,事实证明,虽然代码是 100% 合法的 C,但除了这些 extern "C"
限定符外,它还可以使用标志 /TP
进行完美编译,这意味着 "compile as C++ disregarding the file name."
所以,我想这就是他们所做的。
我认为 MSFT 只是忽略了将 extern "C"
限定符放在某种依赖于 __cplusplus
标志或其他东西的条件编译中。