c++ - Compiler says it cannot find file and yet reports errors in "unfound" file? -


i have 2 short files located in same directory. contents of each shown below.

  • file test.cpp contains:

    int main() {     #include <test.h> } 
  • file test.h contains:

    syntax_error 

upon compiling test.cpp either g++ or clang++, error expected.

test.cpp:3:11: error: 'test.h' file not found <angled> include; use   "quotes" instead     #include <test.h>              ^~~~~~~~              "test.h" 

however, second error seems contradict first error.

in file included test.cpp:3: ./test.h:1:1: error: use of undeclared identifier 'syntax_error' syntax_error ^ 

essentially, first error reports compiler cannot find file test.h, , second reports syntax error in file compiler reported not find.

these 2 errors generated.

i understand why compiler reports first error , should use quotes #include in case. why, though, compiler cannot find file when has found it? and, why continue report errors in "unfound" file?

this feature, not bug.

the idea if error trivial (like missing semicolon), compiler try continue compiling if had fixed error. enables fix multiple errors in 1 go. useful when compiling code takes long time.

imagine fixing missing semicolon, recompiling 5 hours, compiler finds missing semicolon. , have recompile again. frustrating, no?

basically, compiler try recover errors far able to, able report errors possible. compilers have flag this.

why, though, compiler cannot find file when has found it?

the compiler found file yes, that's why gave hint use "" instead of <>. if hadn't, might not have given hint. still, compiler not allowed compile code correctly, because code ill-formed.

as analogy, because compiler found missing semicolon, doesn't mean can compile code missing character (if tries standards compliant). recover , try find other errors, if any.


Comments

Popular posts from this blog

What is happening when Matlab is starting a "parallel pool"? -

angular - DownloadURL return null in below code -

php - Cannot override Laravel Spark authentication with own implementation -