I'm running emacs 25.3.2 with a current flycheck installed. Flycheck uses clang-5 as a checker for C++.

I like to keep my declarations and definitions separated in two files even when working with templates, thus I use the include-the-definition trick. Take these two files for example:


#ifndef FOO_HPP
#define FOO_HPP

template<class T>
class Foo {

#include "foo.cpp"



#include "foo.hpp"

template<class T>
Foo<T>::Foo() {}

Note that the #include directive in foo.cpp is only necessary to allow foo.cpp to be compiled as a compilation unit, which is what flycheck does.

Now, if I do M-x flycheck-compile RET c/c++-clang RET, this is the output:

-*- mode: compilation; default-directory: "~/tmp/mnwe/flycheck/" -*-
Compilation started at Wed Aug 29 11:59:45

clang -fsyntax-only -fno-color-diagnostics -fno-caret-diagnostics -fno-diagnostics-show-option -iquote /home/lukas/tmp/mnwe/flycheck/ -Wall -Wextra -x c\+\+ - < /home/lukas/tmp/mnwe/flycheck/foo.cpp
<stdin>:4:9: error: redefinition of 'Foo<T>'
./foo.cpp:4:9: note: previous definition is here

Compilation exited abnormally with code 1 at Wed Aug 29 11:59:45

So, for some reason, it sees the definition twice, once on <stdin>? How does that happen? How do I fix that? The problem does not happen during actual compilation.

  • The definition of the constructor for Foo should be marked inline. Also, it seems wrong to me to provide declarations in cpp files. It seems likely to screw up tooling. How about from foo.hpp including foo.inl instead. There is no need for a foo.cpp here. There is no include guard in foo.cpp and no protection from multiple inclusion... because it is not a header file. – InHarmsWay Aug 30 '18 at 9:35

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.