如何查找C++中的所有全局变量源代码、DLL 或 VC 创建的任何文件编译器?
我正在使我的应用程序线程安全。步骤之一是同步访问或消除全局变量的使用。我正在使用 Visual Studio。我找不到任何好方法来查找代码库中的所有全局变量。创建一个好的文本搜索模式是不可能的,而且我找不到任何有用的工具。大家知道有什么好的办法吗?它可以是源代码分析工具或二进制文件分析器。
I'm making my application thread-safe. One of the steps is to synchronize access or eliminate usages of global variables. I'm using Visual Studio. I can't find any good way to find all global variables in my codebase. It's impossible to create a good text search pattern and I can't find any helpful tool. Do you guys know any good way to do that? It could be a source code analysis tool or a binary file analyzer.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
这可能会有所帮助:
我已经使用 Visual Studio 2010 及更高版本对此进行了检查。
编辑:根据Ajay在评论中的建议,您还可以将项目分组。对于分组项目:
This could help:
I have checked this with Visual Studio 2010 and above.
Edit: As suggested by Ajay in comments, you could also categorize items in groups. For grouping items:
一种选择可能是让链接器生成映射文件(Visual Studio 中的 /MAP)。
您将获得每个二进制文件的 .map 文件,其中包含两个部分:
段表
符号列表(函数和数据)
您可以通过以下方式区分函数和变量:段列表中的“代码”/“数据”指定。
优点:您将获得链接器未删除的所有符号,甚至是库中的符号。
缺点:您将获得链接器未删除的所有符号,甚至是库中的符号。我不知道有什么工具可以自动进行代码/数据分离。
One option might be letting the linker generate a map file (/MAP in Visual Studio).
You will get a .map file for each binary with two sections:
A table of segments
A list of symbols (functions and data)
You can tell apart the functions from variables by the "CODE" / "DATA" designaiton in the segment list.
Advantage: You will get all symbols, even those in libraries, that were not removed by the Linker.
Disadvanatge: You will get all symbols, even those in libraries, that were not removed by the Linker. I don't know of any tool that does the code/data separation automatically.
我知道 http://code.google.com/p/data- race-test/wiki/ThreadSanitizer 程序(google 的产品),可以在 Windows 和编译代码上运行。它是动态检测程序(如 valgrind 或有点像 qemu/virtualbox),它为内存访问添加了一些检查。它将尝试发现一些线程问题。您可以在threadsanitizer的控制下运行您的程序。动态转换和检测代码将会导致速度减慢(速度减慢 20 倍至 50 倍)。但有些问题会被自动检测到。
它还允许您在源代码中注释一些自定义同步函数。
程序的 Wiki 有到其他线程竞争检测器的链接: http://code .google.com/p/data-race-test/wiki/RaceDetectionLinks
I know the http://code.google.com/p/data-race-test/wiki/ThreadSanitizer program (product of google) which can work in Windows and on compiled code. It is dynamic instrumentation program (like valgrind or bit like qemu/virtualbox), which add some checks to memory accesses. It will try to find some threading problems. You can just run your program under control of threadsanitizer. There will be slowdown from dynamic translation and from instrumentation code (up to 20x-50x times slower). But Some problems will be detected automatically.
It also allows you to annotate some custom synchronization functions in source code.
Wiki of program has links to other thread-race detectors: http://code.google.com/p/data-race-test/wiki/RaceDetectionLinks
cppclean 是一个可以帮助您的静态分析工具。来自文档:
下面是一个带有静态局部变量和全局变量的简单示例。
./example.h:
./example.cpp:
打开终端并运行 cppclean,如下所示:
不幸的是,cppclean 有一些 解析问题和错误。然而,这些问题非常罕见,并且影响的比例低于我测试过的所有代码的百分比。
cppclean is a static analysis tool that can help you. From the documentation:
A simple example with a static local variable and a global variable follows.
./example.h:
./example.cpp:
Open a terminal and run cppclean as follows:
Unfortunately, cppclean has some parsing issues and bugs. However, these issues are pretty rare, and affected below a percent of all code I've tested.
也许 dumpbin 工具在这里会有所帮助。您可以使用
/SYMBOLS
键运行它来显示 COFF 符号表并查找外部符号 - 全局变量应该在此列表中。 转储/符号。Maybe
dumpbin
tool will help here. You can run it with/SYMBOLS
key to display the COFF symbol table and look for External symbols - global variables should be in this list. DUMPBIN /SYMBOLS.在
clang-query
中,可以使用列出所有全局变量对于更复杂的示例,要查找指向
my_type
的指针的所有全局变量,这会查找所有
static 变量
static
类的成员变量请参阅正在运行clang-query 仅对输入文件 了解如何限制结果。
请注意,如果全局变量出现在从未实例化的模板中,上面将列出它,即使编译的代码中实际上不存在这样的全局变量。
In
clang-query
all global variables can be listed usingFor a more complicated example, to find all global variables that are pointers to
my_type
,This finds all
static
variables inside functionsstatic
member variables of classesSee Running clang-query only on input files for how to limit the results.
Note that if a global variable occurs in a template that is never instantiated, the above will list it, even though there isn't actually such a global variable in the compiled code.
你们都把这件事搞得太复杂了。
1.
将每个文件的代码(一次一个与其他文件分开)复制到字符串或宽字符串等,然后解析出从“{”到“}”不包含的所有内容。将结果保存到外部文件。第一次之后,然后追加到该文件。
2.
即使在所有解析之后剩下的内容总共有 1,000 行,其中也包含了所有全局变量(取决于您如何创建全局变量)。如果您通过名称空间等创建它们,则返回并解析它们。我怀疑大多数程序员是否会拥有 1,000 个全局变量,但对于某些应用程序来说,这可能就是他们所使用的。如果此时您没有太多结果,请手动编辑结果的文本文件。
我发现这个网站上 90% 以上的答案可能都过于复杂,只会占用 CPU 时间和内存空间。保持简单。
您可能会发现提前加载一个 globals.h 文件并将大部分或全部全局变量保留在那里会很方便。看来是时候做大量的清理工作了。
You all are making this too complicated.
1.
Copy the code of each of your files (one at a time separate from the others) to a string or a wide string or etc. and then parse out everything that is from "{" to "}" uninclusive. Save the result to an exterior file. After the first time, then append to that file.
2.
Even if you have 1,000 lines total of what is left after all that parsing, in that are all of your globals (depending upon how you created the globals). If you created them via a namespace, etc. then go back and parse for that. I doubt that most programmers will have 1,000 globals, but for some applications it might be what they use. If you do not have too many at that point then manually edit that text file of the results.
I have found that maybe 90+ % of the answers on this site are bloated with far too much complexity that just eats up cpu time and memory space. Keep it simple.
You might find it handy to have a globals.h file which you load early and keep most or all of you globals there. It looks like time to do a lot of clean up.