How can I check if the syntax of a header file is correct with gcc tools?
GCC looks for headers requested with #include " file " first in the directory containing the current file, then in the directories as specified by -iquote options, then in the same places it would have looked for a header requested with angle brackets. For example, if /usr/include/sys/stat. h contains #include "types.
To check if an header file has been included or not in a C or C++ code, we need to check if the macro defined in the header file is being defined in the client code. Standard header files like math. h have their own unique macro (like _MATH_H ) which we need to check. Consider this example of checking if math.
gcc -I adds include directory of header files.
GCC needs to install corrected versions of some system header files. This is because most target systems have some header files that won't work with GCC unless they are changed. Some have bugs, some are incompatible with ISO C, and some depend on special features of other compilers.
-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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With