0

I am trying to run a c program in vsCode but it keeps telling me "no such file or directory" when referring to one of my .h files (yet finds the others just fine...). I have tried googling this countless times but all the solutions seem to go way over my head and refer to things I cannot find such as the json file (and googling where that is didnt help either). Below is the error I am getting

enter image description here

2
  • Typo in the filename? Also, if this is not Windows then file names are case-sensitive. On linux and macOS, parser.h != Parser.h != parser.H. Commented Jan 30, 2023 at 0:37
  • 3
    "I am trying to run a c program" You're actually trying to compile it. That's a compilation error. Commented Jan 30, 2023 at 3:34

2 Answers 2

2

Have you tried using #include "parser.h"?

From the C Standard (ISO/IEC 9899:2018 (C18)), section 6.10.2 "Source file inclusion":

2. A preprocessing directive of the form
# include < h-char-sequence > new-line
searches a sequence of implementation-defined places for a header identified uniquely by the specified sequence between the < and > delimiters, and causes the replacement of that directive by the entire contents of the header. How the places are specified or the header identified is implementation-defined.
3. A preprocessing directive of the form
# include " q-char-sequence " new-line
causes the replacement of that directive by the entire contents of the source file identified by the specified sequence between the " delimiters. The named source file is searched for in an implementation-defined manner. If this search is not supported, or if the search fails, the directive is reprocessed as if it read
# include < h-char-sequence > new-line
with the identical contained sequence (including > characters, if any) from the original directive.

When including a source file, if you use the #include <header.h> notation, the compiler (gcc in your case) will search the header in a standard list of system directories (and, if used, the directories specified after the -l option), while if you use the #include "header.h" notation, the compiler will search the header in the directory containing the current file.

If you want to know where gcc is seeking for source files, I'd suggest you to have a look at this article.

Sign up to request clarification or add additional context in comments.

3 Comments

I think the original poster needs some explanation on how VS Code works, which is the IDE on top of whatever compiler he is using.
Thank you this was indeed the first problem. However, VS Code is now giving me the error "undefined reference". I googled that and found apparently there was a missing line in my task.json. However, I added the line and it still says undefined reference.
@Paul could you update your question adding the content of your task.json and the new error?
0

As mikyll98 has detailedly explained and indicated, #include <> and #include “” are for different things. Check which case your parser.h falls into.

In addition to miky’s answer, You should also check if the location of your header file is “visible” to VScode. This is where the json file comes in. VSCode settings is sometimes funky (I think) because sometimes you have to resort to json config files to fully modify the settings, and the location / configurable knobs of such json files are not explicit. You could open VSCode settings, type in the search bar “include path” or “include directories”, and go to the section relevant to C/C++. There should be an option where you either add extra directories via the GUI, or let VSCode open a json file and you can add your path to that file. But be aware that the configurable knobs of said json file isn’t explicit and you’ll have to look up VSCode’s documentation website to know what specific json attribute to add.

3 Comments

Wouldn't this only tell VSCode where the header is without affecting compilation at all?
Good question. If you’re right, there could be another knob to control the include paths for compilation…
There is a tasks.json file where command line options for the compiler can be entered. Maybe using the correct quotes for the filename as suggested in the other answer already solves the problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.