我想告诉VC ++编译器编译所有代码。可以做吗? [英] I want tell the VC++ Compiler to compile all code. Can it be done?
问题描述
我使用VS2005 VC ++非托管C ++。我有VSTS,我试图使用代码覆盖工具来完成关于单元测试的两件事:
I am using VS2005 VC++ for unmanaged C++. I have VSTS and am trying to use the code coverage tool to accomplish two things with regards to unit tests:
- 查看我的引用
- 查看我的测试代码(如果有)的方法未经过单元测试。
设置VSTS代码覆盖率工具(请参阅链接文本)和完成任务#1是直接。然而#2对我来说是一个惊人的挑战。这里是我的测试代码。
Setting up the VSTS code coverage tool (see the link text) and accomplishing task #1 was straightforward. However #2 has been a surprising challenge for me. Here is my test code.
class CodeCoverageTarget
{
public:
std::string ThisMethodRuns() {
return "Running";
}
std::string ThisMethodDoesNotRun() {
return "Not Running";
}
};
#include <iostream>
#include "CodeCoverageTarget.h"
using namespace std;
int main()
{
CodeCoverageTarget cct;
cout<<cct.ThisMethodRuns()<<endl;
}
当上述两个方法都定义在类中时,编译器自动消除ThisMethodDoesNotRun ()从obj文件。如果我将它的定义移出类,那么它被包括在obj文件中,代码覆盖工具显示它根本没有被执行。在大多数情况下,我希望编译器为我执行这种消除,但对于代码覆盖工具,它会失去很大一部分的价值(例如找到未经测试的方法)。我已经尝试了一些事情告诉编译器停止为我聪明,编译所有的东西,但我陷入困境。这将是很好,如果代码覆盖工具补偿这(我想通过扫描源和匹配它与链接器输出),但我没有找到任何建议,它有一个特殊的模式打开。我完全缺少一些简单的在这里或者这是不可能用VC ++编译器+ VSTS代码覆盖工具?
When both methods are defined within the class as above the compiler automatically eliminates the ThisMethodDoesNotRun() from the obj file. If I move it's definition outside the class then it is included in the obj file and the code coverage tool shows it has not been exercised at all. Under most circumstances I want the compiler to do this elimination for me but for the code coverage tool it defeats a significant portion of the value (e.g. finding untested methods). I have tried a number of things to tell the compiler to stop being smart for me and compile everything but I am stumped. It would be nice if the code coverage tool compensated for this (I suppose by scanning the source and matching it up with the linker output) but I didn't find anything to suggest it has a special mode to be turned on. Am I totally missing something simple here or is this not possible with the VC++ compiler + VSTS code coverage tool?
提前感谢
KGB
Thanks in advance, KGB
推荐答案
确保您的功能不被丢弃的方式是导出它们。您可以添加 __ declspec( dllexport)
到你的函数声明。最好将其封装在C预处理器宏中,以便可以将其关闭,因为它是特定于编译器的,并且您可能不希望所有构建都导出符号。导出功能的另一种方法是创建 .DEF
文件。
One way to ensure your functions are not discarded is to export them. You can do this by adding __declspec(dllexport)
to your function declarations. It is best to wrap this in a C preprocessor macro so that you can turn it off, since it is compiler-specific and you might not want all of your builds to export symbols. Another way to export functions is to create a .DEF
file.
如果内联是问题,您也可能成功了 __ declspec(noinline)
。
If inlining is the problem, you might also have success with __declspec(noinline)
.
您的代码是否在静态库中,然后编译为测试EXE / DLL?链接器将自动丢弃静态库中未引用的对象文件。示例:如果静态库包含 a.obj
和 b.obj
和您链接的EXE / DLL它引入了来自 b.obj
但不是 a.obj
的引用符号,然后 a.obj
将不会链接到可执行文件或DLL。但是,在重新阅读您的说明后,这听起来不像这里发生了。
Is your code in a static library which is then compiled into a test EXE/DLL? The linker will automatically discard unreferenced object files that are in static libraries. Example: if the static library contains a.obj
and b.obj
and the EXE/DLL that you're linking it into references symbols from b.obj
but not a.obj
, then the contents of a.obj
will not be linked into the executable or DLL. However, after re-reading your description it doesn't sound like that's what's happening here.
这篇关于我想告诉VC ++编译器编译所有代码。可以做吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!