我在很多年前编写过庞大的代码库。我们正在尝试实施多线程,并且我已经开始清理全局变量(叹息!)
我的策略是将所有全局变量移动到一个类,然后各个线程将使用该类的实例,并且将通过类实例和->
运算符访问全局变量。
首先,我通过查找nm
和B
组对象名称,使用D
编译了一个全局变量列表。列表不完整,并且包含静态变量,我没有获取文件和行号信息。
第二阶段更加混乱,我用classinstance->global_name
模式替换代码库中的所有全局变量。我正在使用cscope Change text string
。问题是,在某些全局变量的情况下,它们的名称也在函数内部使用,因此cscope
也会替换它们。
还有其他方法吗?任何策略,或者请帮忙!
答案 0 :(得分:1)
中途:如果函数使用隐藏全局名称的本地名称,则目标文件将没有未定义的符号。 nm
可以显示那些未定义的符号,然后您知道必须在哪个文件中替换该名称的至少某些实例。
但是,在极少数情况下,如果文件同时使用全局名称,并且在另一个函数中隐藏全局名称,则仍然存在问题。我不确定这是否可以通过--ffunction-sections
来解决;但我认为是这样的:nm
可以显示该部分,因此您会看到foo()
中使用的未定义符号出现在.text.foo
部分中。
答案 1 :(得分:1)
just some suggestions, from my experience:
use eclipse: the C++ indexer is very good, and when dealing with a large project I find it very useful to track variables. shift+ctrl+g (I have forgotten how to access to it from menus!) let you search all the references, ctrl+alt+h (open call hierarchy) the caller-callee trees...
use eclipse: it has good refactoring tools, that is able to rename a variable without touching same-name-different-scope variables. (it often fails in case there are templates involved. I find it good, better than visual studio 2008 counterpart).
use eclipse: I know, it get some time to get started with it, but after you get it, it's very powerful. It can deal easily with the existing makefile based project (file -> new -> project -> makefile project with existing code).
I would consider not to use class members, but accessors: it's possibile that some of them will be shared among threads, and need some locking in order to be properly used. So I would prefer: classinstance->get_global_name()
As a final note, I don't know whether using the eclipse indexer at command-line would be helpful for your task. You can find some examples googling for it.
This question/answer can give you some more hints: any C/C++ refactoring tool based on libclang? (even simplest "toy example" ). In particular I do quote "...C++ is a bitch of a language to transform"