持续完善 Pinda.cn 秒建营销活动 通过低代码 零代码的模式快速创建营销活动,欢迎使用 。

pclint入门

1.   安装

将压缩包比如pclint8.zip拷贝到c:\,解压后重命名目录为c:\pclint

 

版本

Q:如何查看版本?

A:readme.txt

PC-lint for C/C++ Version 8.00e

2.   开始使用

2.1. 不用任何配置,直接使用

2.1.1.    准备待被检查的文件:

先在E:\建一个文件:main.cpp,内容为:

void main()

{  

}

 

2.1.2.    最简单的用法

在命令行下输入:D:\>C:\PCLint\Lint-nt E:\main.cpp

C:\PCLint>C:\PCLint\Lint-nt E:\main.cpp

PC-lint for C/C++ (NT) Ver. 8.00q, Copyright Gimpel Software 1985-2004

 

--- Module:   E:\main.cpp

_

}

 

E:\main.cpp  3  Info 783: Line does not end with new-line

 

C:\PCLint>

PCLINT已经工作了,是不是简单的难以让人相信。

2.1.3.    最简单的标准用法

修改被检查的文件E:\main.cpp,内容修改为:

#include <stdio.h>

 

void main()

{

    printf("hello pclint\n");  

}

再次在命令行下输入:D:\>C:\PCLint\Lint-nt E:\main.cpp

D:\>lint

PC-lint for C/C++ (NT) Ver. 8.00q, Copyright Gimpel Software 1985-2004

 

--- Module:   E:\main.cpp

         _

#include <stdio.h>

E:\main.cpp  1  Error 322: Unable to open include file 'stdio.h'

 

D:\>

报错误,“不能找到头文件stdio.h

当然PCLINT不可能也不应该知道到哪里去找stdio.hstdio.h所在的路径需要我们告诉它。

如何告诉呢?

通常的做法是在xxx.lnt文件中指定,然后指定使用这个xxx.lnt文件。

最简单的是使用 c:\pclint\std.lnt 这个文件。

 

2.2. std.lnt的配置:

2.2.1.    std.lnt的默认内容

C:\PCLint\std.lnt默认内容为:

//NOTES: DON'T delete the comment string

//author+++

//au-sm.lnt au-ds.lnt au-misra.lnt

//author---

 

//compiler+++

co-gnu3.lnt

//compiler---

 

//library+++

lib-stl.lnt lib-w32.lnt lib-wnt.lnt

//library---

 

//alignment+++

-si4 -sp4

//alignment---

 

3gTdrnc.lnt

 

2.2.2.    修改std.lnt

运行C:\PCLint\CONFIG.EXE(或者直接手工修改std.lnt

最后生成的std.lnt内容为:

//  Microsoft C and Visual C++ 6.x, -si4 -sp4,

//  Standard lint options

 

 

co-msc60.lnt

 

// options.lnt  -si4 -sp4

-i"E:\Program Files\Microsoft Visual Studio\VC98\Include"

-i"E:\Program Files\Microsoft Visual Studio\VC98\MFC\INCLUDE"

-i"E:\Program Files\Microsoft Visual Studio\VC98\ATL\INCLUDE"

 

暂时将options.lnt 这行注释掉。

注:如果生成的std.lnt内容与上面不一样,可以手工修改成上面那样。

 

2.3. 使用std.lnt

再次在命令行下输入:D:\> C:\PCLint\Lint-nt -i"C:\PCLint" -u std.lnt e:\main.cpp

C:\PCLint\Lint-nt -i"C:\PCLint" -u std.lnt e:\main.cpp

-i"C:\PCLint" -u std.lnt 指明使用std.lnt这个选项文件,至于std.lnt在什么目录,通过-i"C:\PCLint"告知。

 

至此,我们已经学会了PCLINT的简单用法及两个选项:-i,-u

-i 指明包含路径

-u 指明使用哪些.lnt文件

 

3.   其它开发工具下的配置

前面我们掌握了命令行方式下PCLINT的使用方法,下面介绍在VC下面的配置及使用。

3.1. VC下的配置

新建一自定义工具项,名称为:PCLINT

其中的Arguments为:-i"C:\PCLint" -u std.lnt env-vc6.lnt "$(FileName)$(FileExt)"

Q:怎么知道要输入这样的参数了?

A:从帮助中得知的,见文件env-vc6.lnt

Q: env-vc6.lnt有什么作用?

A:指定输出错误报告的格式,这个选项可以去掉,比如修改为:

Arguments为:-i"C:\PCLint" -u std.lnt "$(FileName)$(FileExt)"

3.2. 示例

3.2.1.    testForPclint1

新建一个最简单的Win32 Console Application,工程名testForPclint1,其中的main.cpp为:

void main()

{  

}

点击Tools菜单下面的PCLINT菜单项:

PC-lint for C/C++ (NT) Ver. 8.00q, Copyright Gimpel Software 1985-2004

 

--- Module:   main.cpp

}

 

main.cpp(3): error 783: (Info -- Line does not end with new-line)

 

--- Global Wrap-up

 

 error 900: (Note -- Successful completion, 1 messages produced)

Press any key to continue

第三行有个错误:error 783: (Info -- Line does not end with new-line)

意思是说没有以新行结束。

注:如果Arguments为:-i"C:\PCLint" -u std.lnt "$(FileName)$(FileExt)"

则结果为:

PC-lint for C/C++ (NT) Ver. 8.00q, Copyright Gimpel Software 1985-2004

 

--- Module:   main.cpp

_

}

 

main.cpp(3) : Info 783: Line does not end with new-line

Press any key to continue

 

修改main.cpp为:

void main()

{  

}<---即在这里加一个回车

 

再次LINT,结果没有任何告警:

PC-lint for C/C++ (NT) Ver. 8.00q, Copyright Gimpel Software 1985-2004

 

--- Module:   main.cpp

 

--- Global Wrap-up

 

 error 900: (Note -- Successful completion, 0 messages produced)

Press any key to continue

 

3.2.2.    testForPclint2

新建一个最简单的Win32 Console Application,工程名testForPclint2,其中的main.cpp为:

#include "stdio.h"

 

int main(int argc, char* argv[])

{

    printf("hello\n");

}

 

点击Tools菜单下面的PCLINT菜单项:

PC-lint for C/C++ (NT) Ver. 8.00q, Copyright Gimpel Software 1985-2004

 

--- Module:   main.cpp

_

}

main.cpp(6) : Warning 533: function 'main(int, char **)' should return a value

    (see line 3)

main.cpp(3) : Info 830: Location cited in prior message

_

}

main.cpp(6) : Info 715: Symbol 'argv' (line 3) not referenced

main.cpp(3) : Info 830: Location cited in prior message

_

}

main.cpp(6) : Info 818: Pointer parameter 'argv' (line 3) could be declared as

    pointing to const

main.cpp(3) : Info 830: Location cited in prior message

_

}

main.cpp(6) : Info 715: Symbol 'argc' (line 3) not referenced

main.cpp(3) : Info 830: Location cited in prior message

Press any key to continue

4个告警,修改main.cpp为:

#include "stdio.h"

 

int main(int argc, char* argv[])

{

    (void)argc; // pc lint

    (void)argv; // pc lint

    printf("hello\n");

 

    return 0;

}

 

再次LINT,无任何告警了。

PC-lint for C/C++ (NT) Ver. 8.00q, Copyright Gimpel Software 1985-2004

 

--- Module:   main.cpp

Press any key to continue

 

4.   使用批处理方法

为什么要使用批处理?还不是为了使用更简单。

我们在C:\PCLint建一个批处理文件lint.bat,内容为:

@echo off

C:\PCLint\Lint-nt -i"C:\PCLint" -u std.lnt e:\main.cpp

 

为了支持输入参数,修改为:

@echo off

C:\PCLint\Lint-nt -i"C:\PCLint" -u std.lnt %1

 

这时候我们不能简单输入D:\>lint.bat,而必须加上输入参数:

D:\>lint.bat e:\main.cpp

posted @ 2007-10-28 22:47  工具人Kim哥  阅读(4612)  评论(2编辑  收藏  举报
持续完善 Pinda.cn 秒建营销活动 通过低代码 零代码的模式快速创建营销活动,欢迎使用 。