【摘要】在《C++ Templates》(David Vandevoorde, Nicolai M.Josuttis)一书的6.6.1节中,介绍了一个过滤编译STL代码的错误信息的小工具:STLFilt(由 Leor Zolman 编写)。由于STL代码的错误和警告信息非常繁杂冗长(尤其在VC6.0中),不利于程序员阅读,所以这个工具显得很有用。不过它的安装和配置却很繁琐。工具包中提供了很多个 readme 英文文档来指导用户进行安装和配置,但是必须要关注的配置文件细节等等,仍然需要耗费很大精力(我花了好几十分钟)。如果给另一台电脑安装,尽管已经有所熟悉,但是又要耗费一定的精力。因此我写这篇文章,开发了一个简易的小工具来替用户完成繁琐的复制和配置工作。
为什么作者没有提供一个简易的安装程序呢?作者在说明(QUICKSTART.txt)中是这样说的:
==========================================================================
Q: Why isn't there a nice, simple-to-use SETUP.EXE facility for this tool?
A: I'm not confident enough with "setup" utilities to make certain that one I provide doesn't have the potential to scramble the guts of
your computer. If you do the installation manually, that absolutely guarantees you'll have the ability to UN-do what you've done if you want, no fancy uninstaller tools required. If you are a practicing or aspiring software developer, you probably won't find these direction overly complicated. If you're NOT, you probably wouldn't be here trying to install a package for filtering STL-related C++ error messages in the first place.
==========================================================================
作者说,(他担心把把你的电脑搞乱?)如果这一切配置工作都是由用户手工完成的,那么用户就能够完全的“UNDO”这个步骤。如果你是一个专业的程序员,你应该发现这个步骤不是很复杂。如果你觉得这个步骤太麻烦了,那么多半你也不会出现在这里并且试图去安装他。
但我花了一点耐心按照QuickStart.txt中的指导,一步步配置好以后,我觉得这个过程是可以用一个可视化工具来简化的,所以我现在来做了这个工作。我只给我的VC6.0配置了STLFilt工具,从说明来看,STLFilt还可以多平台使用(这里的多平台是针对多种IDE,编译工具而言),不过就目前来看我写的工具只是默认的配置成为VC6.0使用,因为在VC6中的STL警告和错误尤其多,有时产生数百个warning,重新编译一次消耗的时间很长,所以这个矛盾在VC6上尤其突出,在VS2005上暂时还没发现问题这么严重和突出。所以我写的自动化安装工具是针对VC6的。
安装条件,你已经安装过 VC6.0 。对于其他VS版本我没有认真看过配置的步骤,所以如果你希望配置到其他IDE上,请自行阅读 stlfilt 的原有说明文档,然后自己改进我提供的工具。
安装 STLFilt For VC6.0 的主要步骤如下:
(1)下载 ActivePerl 进行安装。
这个工具是用来运行 Perl 脚本的(它是免费的)。当前最新的用于x86的版本下载地址:
ActivePerl-5.12.1.1201-MSWin32-x86-292674.msi
(2)运行 stlfilt 文件夹下的 STLFiltSetup.exe(我写的小工具) ,点击安装。
stlfilt 和 我的安装工具的下载地址:
http://files.cnblogs.com/hoodlum1980/stlfilt_setup.rar
操作界面如下所示:
(3)在VC6.0中的 Tools 菜单中添加自定义工具:
3.1) STLFilt 切换开关(可以在菜单中自由打开或者关闭STLFilt):
点击 Tools - Customize 菜单,在Tools标签页新建一项,名称输入"STLFilt ON/OFF";
3.1.1 命令(Command): ($InstallDir)STLFilt_hoodlum1980.BAT
3.1.2 参数(Arguments):保留为空
3.1.3 勾选使用输出窗口(Use Output Window);
备注:STLFilt_hoodlum1980.BAT 是我在 STLFilt 的 STLFilt.BAT 的基础上改造而成,自动根据当前状态切换状态(无须显示指定新的状态)。
($InstallDir) 指的是在安装STLFilt工具中选择的 STLFilt 的安装目录。
3.2) MFiltTool (把生成日志(*.plg)的内容过滤后输出到输出窗口)。
备注:由于它是为没有配置的环境也能使用 STLFilt 而准备的,而我们这里已经配置过了,所以这个工具可以不添加。
添加方法和上面类似,名称输入"MFiltTool“
3.2.1 命令(Command): ($InstallDir)MFiltTool.bat
3.2.2 参数(Arguments):$(WkspDir)\$(WkspName).plg
3.2.3 勾选使用输出窗口(Use Output Window);
注意事项说明:
(1)为了防止弄乱VC6的CL.EXE版本,建议按照程序自动给出的安装方式执行安装或卸载。为了更加稳妥起见,可以在安装前单独备份CL.EXE,以防止意外。
(2)安装工具在卸载时,会把复制的文件删除,并恢复CL.EXE;但是不包含以下动作例如:卸载ActivePerl,删除 VC6 的 Tool 菜单下的 STLFilt 相关命令等;这些动作如有需要,应该由用户自行完成。
经过以上步骤,现在STLFilt就可以使用了,需要卸载的时候再次运行这个工具,点击卸载即可。在菜单中使用STLFilt ON/OFF命令可以自由切换状态。在ON状态,STL的编译信息被过滤后显示到输出窗口,在OFF状态,相当于没有安装STLFilt一样。因此我们可以在使用STL时开启这个选项,在没有使用STL的项目中关闭它。此外STLFilt还提供了一个UI小工具(即我在桌面上创建了快捷方式的那个EXE程序),也可以通过右键上下文菜单切换STLFilt状态,并且可以对内存中的文本进行过滤处理。由于我们已经配置好了,所以通过 Tools 中的菜单即可操作,这个工具也基本可以不去管他(不需要运行)。
下面我通过一个简单的小例子测试下STLFilt的效果,在VC6中新建一个Console程序,输入代码如下:
#include < map >
#include < string >
using namespace std;
int main( int argc, char * argv[])
{
map < string , int > _Map;
_Map.insert( " hello " , 100 );
return 0 ;
}
在 STLFilt 关闭状态下,编译这段代码,VC6的输出信息如下:
Compiling...
****** {BD Software Proxy CL v2. 50 } STL Message Decryption is Off ******
STL_DEMO_3.cpp
d:\program files\microsoft visual studio\vc98\include\xtree( 118 ) : warning C4786: ' std::_Tree<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::pair<std::basic_string<char,std::char_traits<char>,std::allocator<char> > const ,
int > ,std::map < std::basic_string < char ,std::char_traits < char > ,std::allocator < char > > , int ,std::less < std::basic_string < char ,std::char_traits < char > ,std::allocator < char > > > ,std::allocator < int > > ::_Kfn,std::less < std::basic_string < char ,std::char_traits < cha
r > ,std::allocator < char > > > ,std::allocator < int > > ' : identifier was truncated to ' 255 ' characters in the debug information
d:\program files\microsoft visual studio\vc98\include\map( 46 ) : see reference to class template instantiation ' std::_Tree<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::pair<std::basic_string<char,std::char_traits<
char > ,std::allocator < char > > const , int > ,std::map < std::basic_string < char ,std::char_traits < char > ,std::allocator < char > > , int ,std::less < std::basic_string < char ,std::char_traits < char > ,std::allocator < char > > > ,std::allocator < int > > ::_Kfn,std::less < std::ba
sic_string < char ,std::char_traits < char > ,std::allocator < char > > > ,std::allocator < int > > ' being compiled
E:\MyProjects\STL\STL_DEMO_3\STL_DEMO_3.cpp( 11 ) : see reference to class template instantiation ' std::map<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,int,std::less<std::basic_string<char,std::char_traits<char>,std::a
llocator < char > > > ,std::allocator < int > > ' being compiled
d:\program files\microsoft visual studio\vc98\include\xtree( 118 ) : warning C4786: ' std::_Tree<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::pair<std::basic_string<char,std::char_traits<char>,std::allocator<char> > const ,
int > ,std::map < std::basic_string < char ,std::char_traits < char > ,std::allocator < char > > , int ,std::less < std::basic_string < char ,std::char_traits < char > ,std::allocator < char > > > ,std::allocator < int > > ::_Kfn,std::less < std::basic_string < char ,std::char_traits < cha
r > ,std::allocator < char > > > ,std::allocator < int > > ::const_iterator ' : identifier was truncated to ' 255 ' characters in the debug information
d:\program files\microsoft visual studio\vc98\include\map( 46 ) : see reference to class template instantiation ' std::_Tree<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::pair<std::basic_string<char,std::char_traits<
char > ,std::allocator < char > > const , int > ,std::map < std::basic_string < char ,std::char_traits < char > ,std::allocator < char > > , int ,std::less < std::basic_string < char ,std::char_traits < char > ,std::allocator < char > > > ,std::allocator < int > > ::_Kfn,std::less < std::ba
sic_string < char ,std::char_traits < char > ,std::allocator < char > > > ,std::allocator < int > > ' being compiled
E:\MyProjects\STL\STL_DEMO_3\STL_DEMO_3.cpp( 11 ) : see reference to class template instantiation ' std::map<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,int,std::less<std::basic_string<char,std::char_traits<char>,std::a
llocator < char > > > ,std::allocator < int > > ' being compiled
d:\program files\microsoft visual studio\vc98\include\xtree( 118 ) : warning C4786: ' std::_Tree<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::pair<std::basic_string<char,std::char_traits<char>,std::allocator<char> > const ,
int > ,std::map < std::basic_string < char ,std::char_traits < char > ,std::allocator < char > > , int ,std::less < std::basic_string < char ,std::char_traits < char > ,std::allocator < char > > > ,std::allocator < int > > ::_Kfn,std::less < std::basic_string < char ,std::char_traits < cha
r > ,std::allocator < char > > > ,std::allocator < int > > ::iterator ' : identifier was truncated to ' 255 ' characters in the debug information
d:\program files\microsoft visual studio\vc98\include\map( 46 ) : see reference to class template instantiation ' std::_Tree<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::pair<std::basic_string<char,std::char_traits<
char > ,std::allocator < char > > const , int > ,std::map < std::basic_string < char ,std::char_traits < char > ,std::allocator < char > > , int ,std::less < std::basic_string < char ,std::char_traits < char > ,std::allocator < char > > > ,std::allocator < int > > ::_Kfn,std::less < std::ba
sic_string < char ,std::char_traits < char > ,std::allocator < char > > > ,std::allocator < int > > ' being compiled
E:\MyProjects\STL\STL_DEMO_3\STL_DEMO_3.cpp( 11 ) : see reference to class template instantiation ' std::map<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,int,std::less<std::basic_string<char,std::char_traits<char>,std::a
llocator < char > > > ,std::allocator < int > > ' being compiled
d:\program files\microsoft visual studio\vc98\include\xtree( 118 ) : warning C4786: ' std::_Tree<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::pair<std::basic_string<char,std::char_traits<char>,std::allocator<char> > const ,
int > ,std::map < std::basic_string < char ,std::char_traits < char > ,std::allocator < char > > , int ,std::less < std::basic_string < char ,std::char_traits < char > ,std::allocator < char > > > ,std::allocator < int > > ::_Kfn,std::less < std::basic_string < char ,std::char_traits < cha
r > ,std::allocator < char > > > ,std::allocator < int > > ::_Node ' : identifier was truncated to ' 255 ' characters in the debug information
d:\program files\microsoft visual studio\vc98\include\map( 46 ) : see reference to class template instantiation ' std::_Tree<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::pair<std::basic_string<char,std::char_traits<
char > ,std::allocator < char > > const , int > ,std::map < std::basic_string < char ,std::char_traits < char > ,std::allocator < char > > , int ,std::less < std::basic_string < char ,std::char_traits < char > ,std::allocator < char > > > ,std::allocator < int > > ::_Kfn,std::less < std::ba
sic_string < char ,std::char_traits < char > ,std::allocator < char > > > ,std::allocator < int > > ' being compiled
E:\MyProjects\STL\STL_DEMO_3\STL_DEMO_3.cpp( 11 ) : see reference to class template instantiation ' std::map<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,int,std::less<std::basic_string<char,std::char_traits<char>,std::a
llocator < char > > > ,std::allocator < int > > ' being compiled
d:\program files\microsoft visual studio\vc98\include\xtree( 118 ) : warning C4786: ' std::_Tree<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::pair<std::basic_string<char,std::char_traits<char>,std::allocator<char> > const ,
int > ,std::map < std::basic_string < char ,std::char_traits < char > ,std::allocator < char > > , int ,std::less < std::basic_string < char ,std::char_traits < char > ,std::allocator < char > > > ,std::allocator < int > > ::_Kfn,std::less < std::basic_string < char ,std::char_traits < cha
r > ,std::allocator < char > > > ,std::allocator < int > > ::const_iterator ' : identifier was truncated to ' 255 ' characters in the debug information
d:\program files\microsoft visual studio\vc98\include\map( 46 ) : see reference to class template instantiation ' std::_Tree<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::pair<std::basic_string<char,std::char_traits<
char > ,std::allocator < char > > const , int > ,std::map < std::basic_string < char ,std::char_traits < char > ,std::allocator < char > > , int ,std::less < std::basic_string < char ,std::char_traits < char > ,std::allocator < char > > > ,std::allocator < int > > ::_Kfn,std::less < std::ba
sic_string < char ,std::char_traits < char > ,std::allocator < char > > > ,std::allocator < int > > ' being compiled
E:\MyProjects\STL\STL_DEMO_3\STL_DEMO_3.cpp( 11 ) : see reference to class template instantiation ' std::map<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,int,std::less<std::basic_string<char,std::char_traits<char>,std::a
llocator < char > > > ,std::allocator < int > > ' being compiled
E:\MyProjects\STL\STL_DEMO_3\STL_DEMO_3.cpp( 12 ) : error C2664: ' class std::_Tree<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,struct std::pair<class std::basic_string<char,struct std::char_traits<char>,clas
s std::allocator < char > > const , int > , struct std::map < class std::basic_string < char , struct std::char_traits < char > , class std::allocator < char > > , int , struct std::less < class std::basic_string < char , struct std::char_traits < char > , class std::allocator < char > >
> , class std::allocator < int > > ::_Kfn, struct std::less < class std::basic_string < char , struct std::char_traits < char > , class std::allocator < char > > > , class std::allocator < int > > ::iterator __thiscall std::map < class std::basic_string < char , struct std::char_t
raits < char > , class std::allocator < char > > , int , struct std::less < class std::basic_string < char , struct std::char_traits < char > , class std::allocator < char > > > , class std::allocator < int > > ::insert( class std::_Tree < class std::basic_string < char , struct std::cha
r_traits < char > , class std::allocator < char > > , struct std::pair < class std::basic_string < char , struct std::char_traits < char > , class std::allocator < char > > const , int > , struct std::map < class std::basic_string < char , struct std::char_traits < char > , class std::al
locator < char > > , int , struct std::less < class std::basic_string < char , struct std::char_traits < char > , class std::allocator < char > > > , class std::allocator < int > > ::_Kfn, struct std::less < class std::basic_string < char , struct std::char_traits < char > , class std::a
llocator < char > > > , class std::allocator < int > > ::iterator, const struct std::pair < class std::basic_string < char , struct std::char_traits < char > , class std::allocator < char > > const , int > & ) ' : cannot convert parameter 1 from ' char [ 6 ] ' to ' class std::_Tree
< class std::basic_string < char , struct std::char_traits < char > , class std::allocator < char > > , struct std::pair < class std::basic_string < char , struct std::char_traits < char > , class std::allocator < char > > const , int > , struct std::map < class std::basic_string < cha
r, struct std::char_traits < char > , class std::allocator < cha
r > > , int , struct std::less < class std::basic_string < char , struct std::char_traits < char > , class std::allocator < char > > > , class std::allocator < int > > ::_Kfn, struct std::less < class std::basic_string < char , struct std::char_traits < char > , class std::allocator < ch
ar > > > , class std::allocator < int > > ::iterator '
No constructor could take the source type, or constructor overload resolution was ambiguous
Error executing cl.exe.
STL_DEMO_3.exe - 1 error(s), 5 warning(s)
现在我们把STLFilt开启,重新编译,产生的输出如下:
-------------------- Configuration: STL_DEMO_3 - Win32 Debug --------------------
Compiling...
****** {BD Software Proxy CL v2. 50 } STL Message Decryption is ON ! ******
StdAfx.cpp
Compiling...
****** {BD Software Proxy CL v2. 50 } STL Message Decryption is ON ! ******
STL_DEMO_3.cpp
E:\MyProjects\STL\STL_DEMO_3\STL_DEMO_3.cpp( 12 ): error C2664: ' map<string,int>::iter map<string,int>::insert(map<string,int>::iter,const pair<string,int> &) ' : cannot convert parameter 1 from ' char [6] ' to ' map<string,int>::iter '
No constructor could take the source type, or constructor overload resolution was ambiguous
Error executing cl.exe.
STL_DEMO_3.exe - 1 error(s), 0 warning(s)
我们可以看到,开启 STLFilt 以后,错误信息变得清晰明了,简单易读。备注,要使其通过编译,需要把Main函数中的第二行代码修改为:
_Map.insert(make_pair<string, int>("hello", 100));
关于安装工具的原理性说明,安装工具启动后, 检测windows目录下是否有 "Proxy-CL.ini" 存在,以此判断 STLFIlt 是否安装过。安装工具从注册表中查询信息获取到 VC6.0 和 Perl 的 路径,虽然用户可以手工输入这些路径,但是除非确有人工干预的必要,建议用户使用安装工具自动获取到的值。
STLFilt 工具的原理性说明,STLFilt 使用一个代理CL.EXE 替换 VC6 原来的CL.EXE,在编译时,代理CL检测STLFilt的开关状态,把原始CL产生的输出信息重定向到 Perl 中按照指定的规律过滤,然后把过滤后的文本输出到VC的输出窗口。
STLFilt Setup工具的源代码下载连接:
http://files.cnblogs.com/hoodlum1980/StlFiltSetup_src.rar
本文中提到的相关网址:
(1)STLFilt 网页:http://www.bdsoft.com/tools/stlfilt.html
(2)ActivePerl 下载:http://www.activestate.com/activeperl/downloads
(3)STLFilt 声明(节选自QUICKSTART.txt):
======================================================
BD Software STL Error Message Decryptor for Visual C++
======================================================
Written by Leor Zolman leor@bdsoft.com
BD Software http://www.bdsoft.com/
Decryptor web page: http://www.bdsoft.com/tools/stlfilt.html
******************************************************************
* To participate in a forum with other filter users, come visit the
* STLFilt section of the BD Software Message Board at:
* http://bdsoft.proboards34.com/
******************************************************************
-----------THE END------------
-- By hoodlum1980 2010-8-18