问题描述
您好,我使用的库已全局覆盖新的/删除。但是我有一个问题,这个库,问题是,它必须手动初始化在主函数。
Hi I'm using a library that has globally overridden new/delete. But I have a problem with this library, the problem is that it has to be manually initialized in the main function.
现在我试图使用另一个库初始化几个函数之前main被调用,不幸的是这个库在这些函数中使用了新的。所以我得到错误,因为使用重写的新/删除关键字的内存管理器尚未初始化。
Now I'm trying to use another library that initializes a few functions before main is called, unfortunately this library uses new within these functions. So I get errors because the memory manager that uses the overridden new/delete keywords are not initialized yet.
我真的想使用默认内存管理器,因为我想以向此库添加单元测试。使用内存使用我想要测试也使用我的单元测试库的内存没有什么意义。
I'd really like to use the default memory manager because I want to add unit testing to this library. It would not make much sense to use the memory used my the library I want to test also used by my Unit Testing library.
所以我的问题是如果可能忽略全局覆盖新的/删除当包括第二个库,只是使用默认的新/删除?
So my question is if it's possible to ignore global overridden new/delete when including the second library and just use the default new/delete?
我使用visual studio 2010在Windows 7与标准的C ++编译器。
I'm using visual studio 2010 on Windows 7 with the standard C++ compiler.
推荐答案
我不认为这是可能的,没有修改库本身。我想这是关于一个静态库(在一个dll,覆盖的新/删除将由dll中的函数指向。)
I do not think this is possible without modifying the library itself. I guess it is about a static library (inside a dll, the overridden new/delete will be pointed by the functions inside the dll.)
可以删除一个obj文件通过使用命令(可视命令提示符)从静态库:
You can remove an obj file from a static library by using the command (Visual command prompt):
LIB /REMOVE:obj_to_remove /OUT:removed.lib input.lib
要找出要删除的对象,首先运行:
To find out what obj to remove, first run:
DUMPBIN /ARCHIVEMEMBERS input.lib
您将看到诸如
Archive member name at 14286: /0 compilation.dir\objfile1.obj
14286
'标识'obj文件。要查看每个符号的位置,请运行:
14286
'identifies' the obj file. To see where each symbol is, run:
DUMPBIN /LINKERMEMBER:1 input.lib > members.txt
并查找新的/删除。 members.txt
将包含每个符号的标记名称以及此符号所在的obj的标识符。例如
and look for the new/delete. members.txt
will contains the mangled names of each symbols and an identifier of the obj in which this symbol is. For instance
14286 ?_Rank@?$_Arithmetic_traits@C@std@@2HB
14286
告诉你符号所在的obj'identifier'。如果您无法找到新的/删除,您可以运行:
The 14286
is telling you the obj 'identifier' in which the symbol lies. If you have trouble finding new/delete, you can run:
DUMPBIN /SYMBOLS input.lib > sym.txt
将刷新到 sym.txt
每个符号的标记名和未标记名。
which will flush into sym.txt
the mangled and unmangled names for each symbol.
最后,使用上面的 LIB
命令删除obj文件,替换 obj_to_remove
compilation.dir \objfile1.obj
在
中链接 removed.lib
。
At the end, remove the obj file with the LIB
command above by replacing obj_to_remove
by compilation.dir\objfile1.obj
in our example, and link against removed.lib
.
现在,如果你不幸运,你需要的其他符号可能在与新/删除相同的目标文件中。在这种情况下,您可以使用类似这种 a>(例如将新
重命名为 dew
和 delete
nelete
。)
Now, if you are not lucky, other symbols you need may be in the same object file as the new/delete. In that case, you can "hack" the lib using something like this (say renaming new
to dew
and delete
to nelete
.)
这篇关于忽略全局覆盖新/删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!