赞
踩
转载自:http://blog.csdn.net/feng_ma_niu/article/details/40631521。也进行了一些修改
首先,提供一下PCLint的相关链接:
官网:http://www.gimpel.com/html/index.htm
最新9.0L安装包:http://download.csdn.net/detail/winking324/8162819
百度网盘:http://pan.baidu.com/s/1i3tIqLv
接下来,简单介绍一下PCLint到底是一个什么样的工具。
PCLint是GIMPELSOFTWARE公司研发的C/C++软件代码静态分析工具,可以说,PCLint是一种更加严格的编译器。其主要分为PCLint和FlexeLint,PCLint主要应用于Windows平台,以二进制可执行文件提供,而FlexeLint应用于其他平台,例如Linux,以源代码形式发布。
PCLint在全球拥有广泛的客户群,许多大型的软件研发组织都把PCLint检查作为代码走查的第一道工序。PCLint不但能够对程式进行全局分析,识别没有被适当检验的数组下标,报告未被初始化的变量,警告使用空指针连同冗余的代码,还能够有效地提出许多程序在空间利用、运行效率上的改进点。
总结下来,PCLint就是不通过执行程序进行代码分析,发现代码中潜在的Bug。通过个人实践来看,PCLint可以发现非逻辑上的80%左右的Bug,绝对是C++开发以及测试人员一个强大的助手。
安装包主要包含如下两部分:
第一个 Update 为升级包,第二个 Gimpel_PC_Lint_9.zip 为 PCLint 的安装包。
1. 解压 PCLint 的安装包,并进行安装,直接 Next 到最后一步,选择执行配置程序,并点击 Finish 。如果这里没有选择执行配置程序,可以通过执行安装目录下的 CONFIG.exe 来执行配置程序。
2. 接下来弹出 PCLint 的配置程序,点击下一步开始配置。
3. 说明执行完 PCLint 配置后,会生成执行脚本以及 PCLint 的配置文件( *.lnt ),继续下一步。
4. 选择生成路径,安装路径即可,以及文件名称,然后下一步。
5. 选择编译器, VisualStudio 现在最高只有 2008 ,所以只好选择这个了,选择后点击下一步。
6. 选择平台,根据自己的需要,选择对应的平台,这里选择 32bit ,点击下一步。
7. 选择依赖的库,对于 VisualStudio 开发来说,主要包括 Active Template Library(ATL) , Microsoft FoundationClass Library , Standard Template Library , Windows 32-bit 这四部分,选择后下一步。
8. 选择一些特殊的优化建议,例如说代码安全性和效率上的。这里按照最大原则进行选择,有: Scott Meyers(Effective C++ More Effective C++ and Effective C++ 3rdEdition) , Dan Saks , MISRA 2004 ,然后下一步( PS : Effective C++ 这两本巨作真心的给力, C++ 入门必读经典)。
9. 指定当前使用库的头文件所在位置,选择 Create –i options ,并下一步,否则执行 PCLint 时会因为找不到头文件而失败。
10. 选择头文件路径,以分号或者换行( Ctrl + Enter )区分,这里仅添加 Visual Studio 的标准头文件。由于不同的公司或者组织,其文件组织方式可能不一样,又或者包含了其他的各种库,所以有需要可以在这里进行添加,或者执行 PCLint 时出现错误,修改 *.lnt 文件添加即可。
11. 提示是否打开新的配置,选择否。
12. 创建 OPTIONS.LNT 文件,就是简单几个问题,由于我这边显示内容不全,所以选择 No ,然后下一步,如果显示全的话可以分别看一下,就是一些实例代码,然后选择一下即可。
13. 选择对应的开发环境,这里最高只有 VC9 ,所以对应选择这个即可。
14. 添加 PCLint 到系统环境变量中。
15. 经过漫长的配置设定,总算完成了。
经过上面复杂的安装及配置后,接着就需要更新一下 PCLint 程序,使其支持更多的 C++ 检查项等。
1. 解压压缩包中 Update 目录下所有文件到 PCLint 的安装路径,例如我的 PCLint 安装在 D 盘,则对应文件路径应该为:
D:\lint\l9-a-b.lp
…
D:\lint\lpatch.exe
D:\lint\Update.bat
2. 双击 Update.bat ,进行 PCLint 的更新,更新成功后的结果如图。
最后一行,版本号应该为 9.00L 。如果不正确,请进行路径或者文件是否正确的检查。
另外,说明一下更新的方法,首先,要确定当前 PCLint 的版本号,方法为使用 cmd 定位在 PCLint 的安装路径下,执行 lint-nt.exe–v 即可。其次,去官网上下载最新的更新程序 lpatch.exe 以及更新包(例如 l9-a-b.lp 等),将其拷贝到安装路径下,执行命令lpatch.exe l9-a-b.lp 即可。
PCLint主要使用命令行模式来进行文件的解析,这个GCC的编译器过程非常相似。其命令行格式为:
lint options file1 [ file2 file3... ]
既然将PCLint可以当做一个编译器,则options即为编译选项,file即为要编译的文件,对于C++来说,主要是*.cpp文件,由于*.h作为包含文件,是不会进行单独进行编译的,所以这一点需要注意。
如果对一个工程的文件进行检查时,则需要生成文件列表来进行,类似于Makefile文件,所以需要注意一下。
接下来是各个工具的具体配置方法,具体详细的各个参数的信息,还请查阅安装目录下的手册。
这个过程就是上面说到的生成文件列表,在Visual Studio中,主要按照工程(或目录)来进行创建。
1. 选择工具,然后创建外部扩展工具。
2. 新建一个外部扩展工具,名称PCLintCreation。
参数如下:
Title:PCLintCreation
Command:安装路径\lint-nt.exe
Arguments:-v -os("$(TargetName).lnt") "$(ProjectFileName)"
Initial directory: $(ProjectDir)
注意: $(ProjectDir)这些宏是vs自定义的,如果需要查看vs中自定义了哪些这种宏,可以打开msdn,然后输入一个常见的宏,比如ProjectName,就可以很方便地查询到定义了哪些宏。
3. 同样,可以根据前文SourceMonitor配置时,进行快捷键的分配。
4. 执行添加的PCLintCreation,会在对应的工程文件路径下,生成一个对应的*.lnt文件,内容如下:
5. 需要注意的是 ,当该工程下文件发生变化时,需要重新执行此任务,保证文件列表的正确性。
根据上一步扫描的文件,进行PCLint检查。
1. 选择工具,然后创建外部扩展工具,这个跟上部分的第一步相同。
2. 新建外部工具,名称PCLintCheck。
参数如下:
Title:PCLintCheck
Command:D:\lint\lint-nt.exe
Arguments:-i"D:\lint"std.lnt env-vc12.lnt "$(TargetName).lnt"
Initial directory:$(ProjectDir)
需要注意的有几点 ,第一,由于PCLint对于C++11的支持可能没有Visual Studio 2013支持的那么广泛,所以会检测失败,所以不建议在Visual Studio 2013及以上版本进行使用。第二,第一个lint文件,std.lnt即为上篇文章中配置生成的文件,env-vc12.lnt为VC12(Visual Studio 2013)对应的配置文件,"$(TargetName).lnt"为上一步对工程扫描的文件列表。第三,如果提示缺少*.lnt文件,则将对应的文件拷贝到PCLint安装路径下即可。
注意如果是vs2008,那么需要使用env-vc9.lnt,env-vc9.lnt文件就是普通的文本文件,使用文本编辑工具即可查看它的内容。
如果出现类似Error 307: Can't open indirect file 'env-vc9.lnt'的错误,只需要到安装目录D:\lint下看看有没有‘env-vc9.lnt'文件,如果没有一般在D:\lint\lnt下是一定有的,大家只需要将该文件拷贝一份到D:\lint目录下即可。还可以重启vs试试看。
3. 执行PCLint,会在Output窗口中输出对应的Info,Warn,Error和Fatal等信息,这样根据信息查看对应的代码,并进行改正。
如果仅仅是对单个文件进行检查,则不需要配置前两步即可,但这个步骤仅对于单个文件执行检查是有效的,不方便配置到Ant任务中。但是相对于整个工程的检查,效率比较高,所以适合快速检查文件,并修改问题,具体配置如下。
1. 选择工具,然后创建外部扩展工具,这个跟前面相同。
2. 新建外部工具,名称PCLintCheckItem。
参数如下:
Title:PCLintCheckItem
Command:D:\lint\lint-nt.exe
Arguments:-i"D:\lint"std.lnt env-vc12.lnt "$(ItemFileName)$(ItemExt)"
Initial directory:$(ItemDir)
3. 执行PCLint,会对当前文件进行检查,并输出结果到Output窗口中,例如:
第五歩中配置参数时都使用到了 env-vc12.lnt这个文件,由于这篇文章是转载的,而作者是以vs2013为例写的教程,所以他使用的是env-vc12.lnt这个文件,而我的编译工具是vs2008,所以使用的是env-vc9.lnt。至于具体某个版本的vs到底应该用哪个版本的env-vc.Int文件,方法很简单,打开env-vc.Int,文件的第一行就是这个Int文件是给哪个版本的vs使用的。
这个env-vc9.lnt文件内其实就是上面第五歩配置的过程,下面是这个文件的内容。
- /*
- env-vc9.lnt: environment parameters for Microsoft's Visual C++ 9.0
- If you are using Microsoft Visual Studio 2008 and you wish to invoke
- PC-lint from that environment then add one or more commands to the
- Tools menu as follows.
- Simple Check
- ------------
-
- For example, to add a simple facility to lint the current file you
- may do the following:
- 1. From the Tools Menu choose "External Tools ..."
- 2. Click the "Add" button.
- 3. You will now be able to enter the fields of this Tool.
- Modify them so that they approximate the following:
- Title: PC-lint (Simple Check)
- Command: c:\lint\lint-nt.exe
- Arguments: -i"c:\lint" std.lnt env-vc9.lnt "$(ItemFileName)$(ItemExt)"
- Initial Directory: $(ItemDir)
- X_Use Output Window __Prompt for arguments __Close on exit
- Please note that you will have to change the "Command:" path if the
- PC-lint Installation Directory is anything other than c:\lint and
- you will have to change the "Arguments:" line if the Configuration
- Directory is anything other than c:\lint
- 4. Select OK to return to the main environment.
- This will result in the Tools menu containing the additional item
- "PC-lint (Simple Check)". Checking 'X' on 'Use Output Window' is
- important because in this way you can advance from error to error
- using the F8 key (Shift F8 to reverse).
- Strings of the form $(...) are called macros and can be typed in
- directly as shown or can be selected from a menu by clicking
- a right arrow in the dialog box. $(ItemFileName) refers to the
- file name of the currently edited file without its path and without
- its extension. $(ItemExt) is its extension. $(ItemDir) represents
- the file's directory.
- You will probably want to advance your new tool upward into the initial
- position of all tools while you are testing and modifying the command.
- You can do this by using the "Move Up" button that appears on the External
- Tools dialog.
- The benefits of using "Initial Directory" are that file-names in lint
- error messages will not be so long, and, also, this directory can contain
- a std.lnt that overrides the global std.lnt in the Configuration Directory.
- This Simple Check is fine to check stand-alone modules but to check
- projects or to unit check modules that are in projects we need to
- go a bit further ...
- Project Creation
- ----------------
- To lint an entire project we will need the names of all the modules
- in the project. Visual Studio keeps these names (as well as some
- appropriate options such as define options (-d...) and include options
- (-i...) in a file named NAME.vcproj in the current project directory.
- NAME is the name of the project and is identified by the macro
- $(TargetName). PC-lint can read the .vcproj file and generate the
- appropriate .lnt file. We recommend creating a tool for this purpose.
- For this tool follow the steps 1-4 doing exactly the same thing as
- above except in step 3, the information entered should be:
- Title: PC-lint (Project Creation)
- Command: c:\lint\lint-nt.exe
- Arguments: -v -os("$(TargetName).lnt") "$(ProjectFileName)"
- Init. Dir.: $(ProjectDir)
- __Use Output Window __Prompt for arguments x_Close on exit
- You will need to have an active project before this will work.
- If you don't already have one you can obtain an active project
- from the Solutions Explorer. You then click the newly added
- "PC-lint (Project Creation)" tool on the tools menu to create
- NAME.lnt.
- The file created is an ASCII file and we recommend that you open it
- within the IDE and examine it for any obvious flaws. This is your
- chance to make any necessary modifications to the file as the process
- of conversion may be less than perfect.
- Project Check
- -------------
- Interestingly, by opening up the NAME.lnt file created above and
- running the Simple Check described earlier you have the equivalent
- of a full project check. However, we prefer to create a special
- Project Check tool.
- Now that we have a project file we can create a new tool called
- "PC-lint (project check)". For this tool again follow steps 1-4 doing
- exactly the same thing as above except in step 3, the information
- entered should be:
- Title: PC-lint (Project Check)
- Command: c:\lint\lint-nt.exe
- Arguments: -i"c:\lint" std.lnt env-vc9.lnt "$(TargetName).lnt"
- Init. Dir.: $(ProjectDir)
- X_Use Output Window __Prompt for arguments __Close on exit
- Unit Check
- ----------
- You can almost do a unit check on any single module by using the
- Simple Check scheme suggested above. The only problems are that you
- will need a -u option and you will not have the benefit of any -d or
- -i options that have been placed into NAME.lnt created in the Project
- Creation step. For this reason we suggest the following tool for
- doing a unit check of any module that is part of a project and for
- which a .lnt project file has been generated.
- Title: PC-lint (Unit Check)
- Command: c:\lint\lint-nt.exe
- Arguments: -i"c:\lint" std.lnt env-vc9.lnt --u "$(TargetName).lnt" "$(ItemPath)"
- Init. Dir.: $(ProjectDir)
- X_Use Output Window __Prompt for arguments __Close on exit
- Note that $(ItemPath) will provide a complete path name and in the
- absence of a project.lnt file it would cause full path names to
- appear in messages. But a side effect of using the project file
- with the --u option means that we adopt the shorter names used
- in the project file.
- Suppressing Messages
- ----------- --------
- Suppressing messages is normally done by adding message suppression
- options to a file. For example, -e550 will suppress message 550.
- There are numerous other options to suppress messages.
- As the documentation indicates, the file
- c:\lint\options.lnt
- (where c:\lint\ is the Configuration Directory) is the presumed container
- of your overall suppression policy. (Note: options.lnt is referenced
- by std.lnt). Add a message suppression here and
- you will affect all linting employing that configuration.
- To suppress messages for a particular project (or for all projects
- within a given project directory) you may do the following:
- Create a file std.lnt that is contained in the project directory.
- Make it refer back to the std.lnt in the Configuration Directory.
- Then add additional message suppression options or indeed any options
- you want. For example it might contain:
- c:\lint\std.lnt // reference to original std.lnt
- -e550 // project-specific option
- In this way suppression is limited to a particular project.
- Tool Bar
- --------
-
- You also have the option of creating a PC-lint toolbar within your Visual
- C++ IDE. First, create one or more tools as described above. You
- will need to know the number(s) of the tool(s) you want to place on
- the tool bar. This can only be done by the painful and laborious
- task of counting. Using the list provided by "Tools"/"External Tools",
- jot down the numbers (starting with 1 at the top) of all the tools
- to be added to the tool bar. We recommend placing all the PC-lint tools
- on a single tool bar. Then select Customize from the Tools menu.
- Select the Toolbars tab and click the New... button.
- Give the Toolbar a name (E.g., PC-lint) in the dialog box provided and
- click "OK". Confirm that the new toolbar is now floating on the
- desktop and that a check has been placed in the check box next to the
- new toolbar name. Then click on the Commands tab and in the Categories
- box, scroll down to and click "Tools". In the Commands box, scroll down
- to the appropriate external command number(s) corresponding to the
- PC-lint commands you jotted down earlier. Drag the commands from the
- Commands box to the toolbar. Their numeric name will change to the
- logical name.
- If you want to add a button image to the toolbar, you can choose one
- via the Modify Selection button. Click Close and you now have your
- own PC-lint for C/C++ button. (Note: If you change the location of
- the PC-lint menu item on the Tools menu, you will change the subscript
- and you will need to change the button(s) on the toolbar.)
- */
-
-
- -"format=%(%F(%l):%) error %n: (%t -- %m)" // Messages will contain
- // file information (%F), the line number (%l), the
- // message number (%n), message type (%t) and message text (%m).
-
- -hF2 // Make sure we ALWAYS provide file information ('F') and use 2
- // lines (one for the source line in error and one for the message).
-
- -width(0) // don't break messages at any particular width
- -t4 // Presume that tabs are every 4 stops
- +e900 // issue a message at termination.
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。