5

I am trying to build a c++ program using CodeBlocks with Mingw I receive errors from STL libraries files such as stl_uninitalized.h and vector.cc

|=== Build: all in MinervaSegs (compiler: GNU GCC Compiler) ===| C:\PROGRA~2\CODEBL~1\MinGW\lib\gcc\mingw32\4.9.2\include\c++\bits\stl_uninitialized.h|63|error: template with C linkage

too honest for this site
  • 12,050
  • 4
  • 30
  • 52
Amr Azzam
  • 81
  • 1
  • 1
  • 10
  • Possible duplicate of [\[ this \]](http://stackoverflow.com/questions/4877705/why-cant-templates-be-within-extern-c-blocks). – sjsam Jul 21 '16 at 12:22
  • To quote one of the answers in the above link, templates are implemented with name mangling and extern C forbids name mangling. – sjsam Jul 21 '16 at 12:27
  • There is no extern C in any part of the code. this message appears from stl_uninitalized.h which is header file in the implementation of C++ – Amr Azzam Jul 23 '16 at 14:58
  • @sjsam you can find the code here https://github.com/SUTDNLP/NNTargetedSentiment – Amr Azzam Jul 23 '16 at 15:03
  • Can you please add the full set of error messages below what you have added there. The context of the error message should also give references to the code you wrote/are building. – Greg Jul 23 '16 at 22:27
  • @Greg This is the Full Message ||=== Build: all in MinervaSegs (compiler: GNU GCC Compiler) ===| C:\msys64\mingw64\include\c++\5.3.0\bits\stl_uninitialized.h|105|error: template with C linkage| C:\msys64\mingw64\include\c++\5.3.0\bits\stl_bvector.h|297|note: previous declaration 'std::_Bit_iterator std::operator+(std::ptrdiff_t, const std::_Bit_iterator&)'| C:\msys64\mingw64\include\c++\5.3.0\bits\stl_bvector.h|410|error: template with C linkage| C:\msys64\mingw64\include\c++\5.3.0\bits\stl_bvector.h|540|error: template with C linkage| – Amr Azzam Jul 24 '16 at 16:09
  • 4
    Just as a hint for future readers: If you pass an include directory to gcc via -isystem instead of -I, some versions may implicitly apply C linkage. – Jan Schatz Jul 01 '19 at 15:02
  • @JanSchatz THANK YOU!!! – Francisco Aguilera May 18 '21 at 21:44
  • For people landing here via a search engine: I was encountering this problem because I had named one of my files "strings.h" which conflicts w/ a file in the STL. Renaming that file to have a prefix like "app_strings.h" resolved the issue. – garrettmills Oct 17 '22 at 17:18

1 Answers1

14

You may be using the scoped extern "C" notation and including C++ code within that scope. eg:

#ifdef __cplusplus
extern "C" {
#endif

#include <vector> // could generate this error

template <typename T> // would also generate this error
struct MyExample
{
    T data;
};

#ifdef __cplusplus
}  // end extern "C"
#endif

You should generally avoid inclusions inside an extern "C" region altogether as this can result in multiple declarations for what would otherwise be the same symbol.

There are specific exceptions to this when interworking with C code that was not made C++ aware - but caveat emptor there.

Greg
  • 2,549
  • 2
  • 24
  • 30
  • No Extern "C" in the code. it is related to compilers issue here is the code https://github.com/SUTDNLP/NNTargetedSentiment – Amr Azzam Jul 23 '16 at 15:04