简体   繁体   English

Clang静态分析器找不到stdio.h

[英]Clang static analyzer can't find stdio.h

I'm trying to use Clang static analyzer on a very simple program: 我正在尝试在一个非常简单的程序上使用Clang静态分析器:

#include <stdio.h>
main ()
{
    printf("Hello, world !");
}

When i do 当我做

clang helloworld.c clang helloworld.c

It compiles the program successfully. 它成功编译了程序。


When i do 当我做

clang -cc1 -analyze -analyzer-checker=unix helloworld.c clang -cc1 -analyze -analyzer-checker = unix helloworld.c

it raises an error: 它引发了一个错误:

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

clang --analyze -Xanalyzer -analyzer-checker=unix helloworld.c clang --analyze -Xanalyzer -analyzer-checker = unix helloworld.c

doesn't print anything. 不打印任何东西。


What is the problem and how can i fix it? 有什么问题,我该如何解决? I assume static analyzer doesn't see the header files though the compiler can use them. 我假设静态分析器没有看到头文件,尽管编译器可以使用它们。 Please, help me. 请帮我。

Sometimes the checker is not able to read the default include path. 有时检查程序无法读取默认包含路径。 So you might want to pass it as an argument. 所以你可能想把它作为一个参数传递。 You can find the exact include path clang looks at using this command: 您可以使用以下命令找到确切的包含路径clang:

clang -E -x c - -v < /dev/null

and then your final query will become: 然后你的最终查询将成为:

clang -I<path to include> --analyze -Xanalyzer -analyzer-checker=unix helloworld.c

Solution using -cc1 flag: 使用-cc1标志的解决方案:

See what include paths the clang is receiving. 查看clang正在接收的路径。 The flag -v is the key option. 标志-v是关键选项。 The quick way of using it is the following (as given by @Nishant) along with the sample include paths it prints, 使用它的快速方法如下(由@Nishant给出)以及样本包括它打印的路径,

$ clang -E -x c - -v < /dev/null
...
#include <...> search starts here:
/usr/local/include
/home/codeman/.itsoflife/local/packages-live/llvm-clang6/build/lib/clang/6.0.1/include
/usr/include/x86_64-linux-gnu
/usr/include
...

On my machine, the simple use of the following command works seamlessly, 在我的机器上,简单使用以下命令可以无缝地工作,

$ clang --analyze -Xanalyzer -analyzer-checker=debug.DumpCFG main.c

however the following form fails, as you pointed, 但是,正如您所指出的那样,以下形式失败了

$ clang -cc1 -analyze -analyzer-checker=debug.DumpCFG main.c

For this second command (with -cc1 ) you can create an environment variable say MY_INCLUDES with the necessary includes. 对于第二个命令(使用-cc1 ),您可以使用必要的包含创建一个环境变量,例如MY_INCLUDES Paste the code below (with necessary include paths as per your system) into ~/.bashrc or ~/.zshrc depending on if you are using bash or zsh . 将下面的代码(根据您的系统包含必要的包含路径)粘贴到~/.bashrc~/.zshrc具体取决于您使用的是bash还是zsh (don't forget to source ~/.bashrc or source ~/.zshrc ) (不要忘记使用source ~/.bashrcsource ~/.zshrc

export MY_INCLUDES="-I/usr/local/include -I/home/codeman/.itsoflife/local/packages-live/llvm-clang6/build/lib/clang/6.0.1/include -I/usr/include/x86_64-linux-gnu -I/usr/include"

Now on bash use, 现在关于bash使用,

$ clang -cc1 $MY_INCLUDES -analyze -analyzer-checker=debug.DumpCFG main.c

on zsh use, 在zsh上使用,

$ clang -cc1 ${=MY_INCLUDES} -analyze -analyzer-checker=debug.DumpCFG main.c

Note the use of MY_INCLUDES after -cc1 but before the main.c file. 注意使用的MY_INCLUDES-cc1但之前main.c文件。 Moreover, on zsh one has to use the = prefix with the env variable or else its considered a single string (for details see this answer ). 此外,在zsh上,必须使用带有env变量的=前缀,否则将其视为单个字符串(有关详细信息, 请参阅此答案 )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM