8

I installed clang with Visual Studio and then built the highlighted project as it's said in the documentation.

enter image description here

The build was successful, however when I try this:

clang -cc1 -analyze -analyzer-checker=core.DivideZero test.c

It says:

test.c:1:10: fatal error: 'stdio.h' file not found
#include <stdio.h>
         ^~~~~~~~~
1 error generated.

I tried many suggestions but nothing worked.

However if I do something like this it works

clang --analyze text.c

I don't know if this uses all the available checkers. I need to write my own checker and test it...

Any ideas?

Output of clang --version

clang version 7.0.0 (trunk 322536)
Target: i686-pc-windows-msvc
Thread model: posix
InstalledDir: C:\path\build\Debug\bin
UnguruBulan
  • 890
  • 4
  • 12
  • 24
  • 2
    You need to set your path in your cmd.exe user environment to include the clang install path and insure the include search path matches where `/path/to/clang/include` is located. You should also be able to add `-I/path/to/clang/include` to your `clang -cc1 ...` command line (double check that clang uses `-I` for that purpose -- or use the option is provides) – David C. Rankin Jan 21 '18 at 17:40
  • How do I find the "include search path"? – UnguruBulan Jan 21 '18 at 17:59
  • 1
    It is an environment variable generally set by clang or VS (they will each have a default for where they look for include files). Each also provides a command line option to specify where to look for them. You will have to look at wherever clang installed to (e.g. `c:\Program Files\clang\...` or `c:\Program Files(x86)\clang\...` to find out where that is. See [Adding directory to PATH Environment Variable in Windows](https://stackoverflow.com/questions/9546324/adding-directory-to-path-environment-variable-in-windows) – David C. Rankin Jan 21 '18 at 18:03
  • From your output above `InstalledDir: C:\path\build\Debug\bin` check under `c:\path` (which seems like are really weird place to install to) – David C. Rankin Jan 21 '18 at 18:05
  • Have you considered [reading the clang FAQ](https://clang.llvm.org/docs/FAQ.html)? – autistic Jan 21 '18 at 18:12
  • @David. I tried your suggestion and it worked meaning that it doesn't say that stdio.h is missing. But now it's telling me to add it even though it's on the first line.. – UnguruBulan Jan 21 '18 at 18:30
  • Also, running another example it says that i have C99. Is there a way to change it? – UnguruBulan Jan 21 '18 at 18:30
  • 1
    All compilers have a way of configuring everything from the command line. What the other answer says makes sense. I've never used clang, so I can't tell you exactly how to do it, but all compilers work more-or-less the same. You just have to consult there documentation to figure out what each wants. – David C. Rankin Jan 21 '18 at 18:57

1 Answers1

2

Yes, I have an idea. Remove -cc1 or <stdio.h>. According to the clang FAQ this is your error. It states quite explicitly, giving your precise example:

$ clang -cc1 hello.c
hello.c:1:10: fatal error: 'stdio.h' file not found
#include <stdio.h>
         ^
1 error generated.

Reading on, it gives other alternative solutions, as well as a useful explanation, which you should certainly read in its entirety, since it's our job as programmers to read the manuals for the technology we use.

clang -cc1 is the frontend, clang is the driver. The driver invokes the frontend with options appropriate for your system. To see these options, run:

$ clang -### -c hello.c

Some clang command line options are driver-only options, some are frontend-only options. Frontend-only options are intended to be used only by clang developers. Users should not run clang -cc1 directly, because -cc1 options are not guaranteed to be stable.

If you want to use a frontend-only option (“a -cc1 option”), for example -ast-dump, then you need to take the clang -cc1 line generated by the driver and add the option you need. Alternatively, you can run clang -Xclang <option> ... to force the driver pass <option> to clang -cc1.

The emphasis is mine. This should give you enough guidance to get what you need done.

autistic
  • 1
  • 3
  • 35
  • 80
  • 3
    yes I have. But That's the only way to run a custom checker. – UnguruBulan Jan 21 '18 at 18:23
  • 3
    It not clear at all. "If you want to use a frontend-only option (“a -cc1 option”), for example -ast-dump, then you need to take the clang -cc1 line generated by the driver and add the option you need. " What -cc1 generated by the driver? – UnguruBulan Jan 21 '18 at 18:38
  • ... and in case you miss my question, here's the part you stopped short of, beginning with "alternatively", which tells you a SECOND option: "Alternatively, you can run `clang -Xclang – autistic Jan 21 '18 at 21:33
  • 1
    I already tried all these options and ended up telling me to include while it was already inserted. However, I found an workaround without stdio. But I will accept your answer, bc you tried to help – UnguruBulan Jan 23 '18 at 09:14
  • I'd love to have seen what you tried, since as I'm sure you're aware, experiments can be flawed. Please show the output of `clang -### -c hello.c` (where `hello.c` is any file), and the command you're running. – autistic Jan 23 '18 at 12:23
  • 1
    The output is way to long. The first option displayed is exactly "-cc1". – UnguruBulan Jan 23 '18 at 14:35
  • 3
    The following are: -triple" "i686-pc-windows-msvc19.12.25831" "-emit-obj" "-mrelax-all" "-mincremental-linker-compatible" "-disable-free" "-main-file-name" "open.c" "-mrelocation-model" "static" "-mthread-model" "posix" "-mdisable-fp-elim" "-fmath-errno" "-masm-verbose" "-mconstructor-aliases" "-target-cpu" "pentium4"...etc. I have specified from where to get the libraries with -isystem "path_to_lib". The command was something like clang -cc1 -isystem "path" --analyze.... – UnguruBulan Jan 23 '18 at 14:37