Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I got

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

when i try to compile my C++ project.

all error point me to vc++ files

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

what are these errors and how i can solve them.' these errors appear suddenly while before i never see these errors.

in the project i am working on, i never use extern C

thanks

like image 798
Benyamin Jane Avatar asked Sep 11 '12 12:09

Benyamin Jane


3 Answers

This error happens when you include your header file inside extern "C" command

extern "C"
{

    #include "Header.h"
}

just include #include "cs.h" outside extern c and you are good to go.

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

    //code

}
like image 79
Mayank Patel Avatar answered Oct 04 '22 18:10

Mayank Patel


It's hard to provide any help for you without showing your sources but info on MSDN for error #2894 and on some other portals is pretty straight forward:

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

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

Maybe you have file named .c instead of .cpp, or switched some compilation flags.

like image 37
Vyktor Avatar answered Oct 04 '22 16:10

Vyktor


if you have written this...

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

than write it as

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

When you try to refer a C functions in C++ it will through error to avoid that you just quote it to C++ so that you can refer it in .CXX or .Cpp functions

like image 44
Siddu Avatar answered Oct 04 '22 17:10

Siddu