Everytime i try tp modify any new program, the program will put a error at “main.h” and say main.h not found. However in the program files, The include file has a “main.h”. Did i do something wrong?
open terminal ,then input
This is a commonly asked question.
There is actually no error going on. It’s just the linter giving you that error because it does not have accurate information.
The linter is the utility that gives you potential compile errors as you type, before you compile. However, you should never trust it. Sometimes it breaks, sometimes it lags, sometimes it gets confused. This is not a PROS thing, this is just the nature of some linters.
Nevertheless, when the linter works, it works great.
The linter relies on information from the compiler to know where the files are. The compile process is managed by the Makefile, which is what controls the execution of the actual gcc compiler. However, the linter cannot infer information straight from the Makefile. Because of this, without intervention, how could it know where
main.h is? The Makefile knows, because it knows to look in
include, but the linter does not.
This is solved whenever you compile successfully. Once the compile process is complete, the Makefile script generates a big list containing the directories of all the files in your project. It saves this list in
compile_commands.json. This file contains the directories of all the files in your project, along with some extra compile information.
This is the file the linter reads. With an up to date
compile_commands.json, the linter will know exactly where each file is located, and how the compiler goes about compiling. Only then will it be able to give you an accurate survey of any potential errors.
Whenever you rename your project, or move files around, the linter will break, because the cache will point to non-existent files (since the paths are absolute). Recompiling will fix this.
Note: When you compile, the Makefile checks to see if you have changed any files before compiling. This saves time in the compile process. However, if you need it to regenerate the cache, be careful to make sure it actually compiles, and does not skip due to the lack of any changes to the code.
To jog it back into compiling, change a
.cpp file, or run
The linter is just a tool that gives you potential errors as you type. It is separate from the compiler. It can get confused, as it requires the compiler telling the linter where everything is.
The compiler regenerates that linter cache (
compile_commands.json) every time it compiles. Without an up-to-date compile_commands, the linter will not work. Not finding
main.h is a linter error, not a compile error.
If you haven’t made any changes to your code between compiles, it will skip compiling. To get it to regenerate the cache, you need to make a change to the code to jog the compiler back into action. If you have a real compiler error, it won’t generate the linter cache until the compile process is successful.
I hope this clears things up for you, as well as for anyone else with this issue.
VEXcode has similar issues. We use a different linter but also rely on compile_commands.json. We cheat a little to make the linter play nicely with header files, the problem with the output of the compile process is that you do not usually compile header file directly, therefore there is usually nothing in compile_commands.json file about headers. If you open a header that assumes another header has been included in the source code first, you will see lots of errors, this would be the situation with many sdk header files that assume they are never used on their own but as part of a group. So if in doubt, build the project and trust the “real” compiler output.
However even when i build the program, the error still happens. Even when i open a brand new program the error still occurs. Btw i am using a Macbook pro
Can you actually post the output that appears in the terminal?
You need to elaborate. The error still happens in the linter, or when you compile?
Like I said, don’t trust the linter.
Like Electrobotz said, please post your error from the terminal.
As I mentioned here:
It might be because you don’t have the project folder properly open in the editor, so the linter does know even know where to look for to find
Try opening your project in a new window, with that project being the sole open folder.