1

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)

4
  • This is C++ not C. I removed the extraneous tag. Commented May 10, 2020 at 19:48
  • @jwdonahue The question mentions that both C code and C++ are compiled, and apparently the problem is that linker/compiler handles C++ as C. Are you sure about the tag being extraneous? Commented May 10, 2020 at 19:49
  • 2
    Please edit your question to provide a minimal reproducible example. You might also want to show how you compile/link. Commented May 10, 2020 at 19:49
  • Question already edited. I will be happy to answer more detailed questions on my problem Commented May 10, 2020 at 20:00

1 Answer 1

3

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;
}
}
Sign up to request clarification or add additional context in comments.

4 Comments

I am aware of that, but as I wrote, checked all possible extern "C" blocks and this is not the case. Still, declaring classes objects just a line above in main.cpp works fine for me
please, do me a favor, and remove that header; if template compiles OK without it, there MUST be something you missed inside that header (or transitively in any header it includes).
That might be a case with an extern "C" lost somewhere in files included from main.h, just began tracking it
That apparently solved the issue. There was a block of extern "C" { not closed with a } in a library (yes) included file. Beware of your included libraries

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.