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

11,708

Solution 1

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

}

Solution 2

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.

Solution 3

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

Solution 4

Sometimes the reason is very simple: some file name(s) in your project are the same as in standard C/C++ library that is included in your project by default settings of VS. For example, string.h. And this fact will spawn the error. Just rename such files and you will get success in building your project.

Share:
11,708
Benyamin Jane
Author by

Benyamin Jane

Updated on June 13, 2022

Comments

  • Benyamin Jane
    Benyamin Jane almost 2 years

    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