Slighten
Slighten

Reputation: 19

'template with C linkage' error when using template keyword in main.cpp

In my main.cpp, I create multiple objects of self-defined classes, e.g.:

#include "device_manager.hpp"
DeviceManager deviceManager; //compiles and works just fine

However, when I try to implement a simple template just below in the same main.cpp:

template <typename T>
inline T max(T a, T b) {
    return a > b ? a : b;
}

I get:

template with C linkage main.cpp line 35    C/C++ Problem

Seems like the compiler fails to understand the template keyword. I did not add any extern "C" blocks inside my main.cpp. I have tried using gnu++14 compiler (C++14 + gnu extensions) and gnu++11 for compiling my C++ code and gnu11 for compiling C code inside my C-to-C++ converted project. What could possibly go wrong with understanding the template keyword?

EDIT: The project was generated as a C project which I later converted to a C++ project using an IDE tool, also changing main.c to main.cpp. Since then, I could declare my class objects in main.cpp without problems, but when I try to use the template keyword in main.cpp, I get the aforementioned error as though it was inside extern "C" statement (but I didn't put it there).

C compiler command: arm-atollic-eabi-gcc -c, compiler: gnu11

C++ compiler command: arm-atollic-eabi-g++ -c, compiler: gnu++14

Linker command: arm-atollic-eabi-g++ (I am on an ARM embedded system, writing in Atollic TrueStudio IDE)

Upvotes: 1

Views: 4770

Answers (1)

Alexey Polyudov
Alexey Polyudov

Reputation: 164

the "C linkage" part makes me suspect that you might not close the

extern "C" {
//...
}

declaration in your header file.

this is the error exactly the code like this will produce:

extern "C" {
template <typename T>
inline T max(T a, T b) {
  return a > b ? a : b;
}
}

Upvotes: 3

Related Questions