我目前有一个控制台项目,它创建一个.exe文件; 我希望它也创建一个.lib文件,以便编译为DLL的其他项目能够从原始项目中调用函数.
我知道这是可能的,但我找不到怎么做.如何告诉链接器还链接.lib?
我正在尝试使用本机单元测试项目在 Visual Studios 2012 中创建单元测试。
这是我的测试:
TEST_METHOD(CalculationsRoundTests)
{
int result = Calculations::Round(1.0);
Assert::AreEqual(1, result);
}
Run Code Online (Sandbox Code Playgroud)
导出类:
#ifdef EXPORT_TEST_FUNCTIONS
#define MY_CALCULATIONS_EXPORT __declspec(dllexport)
#else
#define MY_CALCULATIONS_EXPORT
#endif
...
class CALCULATIONS_EXPORT Calculations {
...
public:
static int Round(const double& x);
Run Code Online (Sandbox Code Playgroud)
函数本身:
int Calculations::Round(const double& x)
{
int temp;
if (floor(x) + 0.5 > x)
temp = floor(x);
else
temp = ceil(x);
return int(temp);
}
Run Code Online (Sandbox Code Playgroud)
但是,测试几乎总是失败,错误代码为 c0000005(访问冲突)。第一次使用 x 或可能在函数中声明的任何其他变量时,测试将失败。