How to check if a C++ header file is correct with gcc tools?

-fsyntax-only

Does exactly what you want:

echo 'int i;' > good.hpp
echo 'int i' > bad.hpp
g++ -fsyntax-only good.hpp
echo $?
# 0
g++ -fsyntax-only bad.hpp
# bad.hpp:1:5: error: expected initializer at end of input
# int i
#     ^
echo $?
# 1
g++ --version | head -n1
g++ (Ubuntu 4.8.1-2ubuntu1~12.04) 4.8.1

man g++ says:

-fsyntax-only
    Check the code for syntax errors, but don't do anything beyond that.

You could try compiling it with g++, as in g++ -c myheader.h. This will catch any syntax errors.


make a cpp file which does but include the header and compile it ;)

Tags:

C++

Header

Gcc