6

我有

error C2894: templates cannot be declared to have 'C' linkage

当我尝试编译我的 C++ 项目时。

所有错误都指向 vc++ 文件

1>c:\program files\microsoft visual studio 10.0\vc\include\iosfwd(32): error C2894: templates cannot be declared to have 'C' linkage
1>c:\program files\microsoft visual studio 10.0\vc\include\iosfwd(120): error C2894: templates cannot be declared to have 'C' linkage
1>c:\program files\microsoft visual studio 10.0\vc\include\iosfwd(133): error C2894: templates cannot be declared to have 'C' linkage
1>c:\program files\microsoft visual studio 10.0\vc\include\iosfwd(256): error C2894: templates cannot be declared to have 'C' linkage
1>c:\program files\microsoft visual studio 10.0\vc\include\iosfwd(279): error C2894: templates cannot be declared to have 'C' linkage
1>c:\program files\microsoft visual studio 10.0\vc\include\iosfwd(375): error C2894: templates cannot be declared to have 'C' linkage
1>c:\program files\microsoft visual studio 10.0\vc\include\iosfwd(474): error C2894: templates cannot be declared to have 'C' linkage
1>c:\program files\microsoft visual studio 10.0\vc\include\iosfwd(486): error C2039: 'memcmp' : is not a member of '`global namespace''
1>c:\program files\microsoft visual studio 10.0\vc\include\iosfwd(486): error C3861: 'memcmp': identifier not found
1>c:\program files\microsoft visual studio 10.0\vc\include\iosfwd(491): error C2039: 'strlen' : is not a member of '`global namespace''
1>c:\program files\microsoft visual studio 10.0\vc\include\iosfwd(491): error C3861: 'strlen': identifier not found
1>c:\program files\microsoft visual studio 10.0\vc\include\iosfwd(511): error C2039: 'memchr' : is not a member of '`global namespace''
1>c:\program files\microsoft visual studio 10.0\vc\include\iosfwd(511): error C3861: 'memchr': identifier not found
1>c:\program files\microsoft visual studio 10.0\vc\include\iosfwd(522): error C2039: 'memset' : is not a member of '`global namespace''
1>c:\program files\microsoft visual studio 10.0\vc\include\iosfwd(522): error C3861: 'memset': identifier not found
1>c:\program files\microsoft visual studio 10.0\vc\include\iosfwd(568): error C2894: templates cannot be declared to have 'C' linkage
1>c:\program files\microsoft visual studio 10.0\vc\include\iosfwd(571): error C2894: templates cannot be declared to have 'C' linkage
1>c:\program files\microsoft visual studio 10.0\vc\include\iosfwd(574): error C2894: templates cannot be declared to have 'C' linkage
1>c:\program files\microsoft visual studio 10.0\vc\include\iosfwd(577): error C2894: templates cannot be declared to have 'C' linkage
1>c:\program files\microsoft visual studio 10.0\vc\include\iosfwd(580): error C2894: templates cannot be declared to have 'C' linkage
1>c:\program files\microsoft visual studio 10.0\vc\include\iosfwd(583): error C2894: templates cannot be declared to have 'C' linkage
1>c:\program files\microsoft visual studio 10.0\vc\include\iosfwd(586): error C2894: templates cannot be declared to have 'C' linkage
1>c:\program files\microsoft visual studio 10.0\vc\include\iosfwd(589): error C2894: templates cannot be declared to have 'C' linkage
1>c:\program files\microsoft visual studio 10.0\vc\include\iosfwd(592): error C2894: templates cannot be declared to have 'C' linkage
1>c:\program files\microsoft visual studio 10.0\vc\include\iosfwd(596): error C2894: templates cannot be declared to have 'C' linkage
1>c:\program files\microsoft visual studio 10.0\vc\include\iosfwd(600): error C2894: templates cannot be declared to have 'C' linkage
1>c:\program files\microsoft visual studio 10.0\vc\include\iosfwd(604): error C2894: templates cannot be declared to have 'C' linkage
1>c:\program files\microsoft visual studio 10.0\vc\include\iosfwd(608): error C2894: templates cannot be declared to have 'C' linkage
1>c:\program files\microsoft visual studio 10.0\vc\include\iosfwd(611): error C2894: templates cannot be declared to have 'C' linkage
1>c:\program files\microsoft visual studio 10.0\vc\include\iosfwd(614): error C2894: templates cannot be declared to have 'C' linkage
1>c:\program files\microsoft visual studio 10.0\vc\include\iosfwd(617): error C2894: templates cannot be declared to have 'C' linkage
1>c:\program files\microsoft visual studio 10.0\vc\include\iosfwd(622): error C2894: templates cannot be declared to have 'C' linkage
1>c:\program files\microsoft visual studio 10.0\vc\include\iosfwd(625): error C2894: templates cannot be declared to have 'C' linkage
1>c:\program files\microsoft visual studio 10.0\vc\include\iosfwd(628): error C2894: templates cannot be declared to have 'C' linkage
1>c:\program files\microsoft visual studio 10.0\vc\include\limits(80): error C2894: templates cannot be declared to have 'C' linkage
1>c:\program files\microsoft visual studio 10.0\vc\include\limits(133): error C2894: templates cannot be declared to have 'C' linkage
1>c:\program files\microsoft visual studio 10.0\vc\include\limits(139): error C2894: templates cannot be declared to have 'C' linkage
1>c:\program files\microsoft visual studio 10.0\vc\include\limits(139): fatal error C1003: error count exceeds 100; stopping compilation

这些错误是什么以及我如何解决它们。在我从未看到这些错误之前,这些错误突然出现。

在我正在做的项目中,我从不使用 extern C

谢谢

4

4 回答 4

7

当您在extern "C" 命令中包含头文件时会发生此错误

extern "C"
{

    #include "Header.h"
}

只包括#include "cs.h"外面extern c,你很高兴。

#include "Header.h"
extern "C"
{

    //code

}
于 2014-03-28T13:51:29.563 回答
4

如果不显示您的来源,很难为您提供任何帮助,但MSDN 上有关错误 #2894其他一些门户网站的信息非常简单:

// C2894.cpp
extern "C" {
   template<class T> class stack {};   // C2894 fail

   template<class T> void f(const T &aT) {}   // C2894
}

也许您的文件名为.c而不是.cpp,或者切换了一些编译标志。

于 2012-10-16T09:32:57.523 回答
2

如果你写了这个……

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

而不是写成

extern "C++"
{
    #include "Header.h"
}

当您尝试在 C++ 中引用 C 函数时,它将通过错误来避免您只是将其引用到 C++,以便您可以在 .CXX 或 .Cpp 函数中引用它

于 2017-04-27T05:40:09.200 回答
1

有时原因很简单:您的项目中的某些文件名与标准 C/C++ 库中的相同,该库默认包含在您的项目中。例如,字符串.h。这个事实将产生错误。只需重命名这些文件,您就可以成功构建您的项目。

于 2020-05-08T23:32:48.797 回答