I am using Clang as a syntax checker in my C++ project. It's called through Flycheck in Emacs, and I am getting an annoying use of undeclared identifier
error, the following Minimal Working Example to illustrates the problem:
In file testnamepace.cpp
:
#include "testnamespace.hpp"
int main() {
const unsigned DIM = 3;
testnamespace::A<DIM> a;
a.f();
a.g();
return 0;
}
In file testnamespace.hpp
:
#ifndef testnamespace_h
#define testnamespace_h
#include <iostream>
namespace testnamespace {
// My code uses lots of templates so this MWE uses a class template
template <unsigned DIM> class A;
}
template <unsigned DIM>
class testnamespace::A{
public:
static const unsigned dim = DIM;
A() {std::cout << "A(): dim = " << dim << std::endl;}
// in my case some functions are defined in a .hpp file...
void f() {
std::cout << "call f()" << std::endl;
}
// ...and others are defined in a .ipp file
void g();
};
#include "testnamespace.ipp"
#endif
In file testnamespace.ipp
:
template <unsigned DIM>
void testnamespace::A<DIM>::g() {
// ^^^^^^^^^^^^^ this results in the following error:
// testnamespace.ipp:2:6:error: use of undeclared identifier 'testnamespace' (c/c++-clang)
std::cout << "g()" << std::endl;
}
Since the code compiles with no warning using g++ -Wall testnamespace.cpp -o testnamespace
(gcc version 4.7.2) I am wondering if this is an error in my coding or if it's just a 'feature' of using Clang.
ipp
header instead of thehpp
header; but your test case compiles with no error. – Lirtestnamespace.ipp:3:11:error: expected ';' after top level declarator (c/c++-clang)
– Briseno-E
(preprocess only) option, to see exactly which headers it's including and what declarations it's finding in them. – Lir$ clang --version
gives:Ubuntu clang version 3.0-6ubuntu3 (tags/RELEASE_30/final) (based on LLVM 3.0)
– Briseno$ gcc --version
returns:gcc (Ubuntu/Linaro 4.7.2-2ubuntu1) 4.7.2
. I tried your suggestion$ gcc -E testnamespace.cpp > out
, at the bottom ofout
I see all my code minus the comments, searching for *pp doesn't see to result in anything suspicious. – Brisenoclang -E
, since you say it's Clang that fails. If GCC works, then it won't tell you anything useful about why a different compiler fails. (For what it's worth, your code works for me with exactly that version of Clang). – Lir$ clang++ -Wall -Wextra testnamespace.cpp -o testnamespace
and everything works, I also triedclang++ -fsyntax_only testnamespace.cpp
and that works. I am using Emacs flycheck to drive clang I guess this could mean the problem is there, since when calling clang manually everything is fine? I should have mentioned that sorry... – Briseno