构建 C++ 时出现链接错误VS.NET 2010 中的单元测试项目

发布于 2024-12-06 05:32:13 字数 2267 浏览 0 评论 0 原文

我正在尝试构建一个非常简单的 C++ 单元测试项目。设置恰好与此 博客 描述。我构建了一个静态库 TestLib.lib 和一个名为 TestProject 的 C++ 单元测试项目。这两个项目都使用平台工具集 v100。

Testlib 仅包含一个类。

BaseClass.h

#pragma once 

class BaseClass
{
public:
    void Method1();
};

BaseClass.cpp

#include "BaseClass.h"
#include <iostream>
#include <list>

using namespace std;

void BaseClass::Method1()
{
    list<int> dummy(0);
    cout << "Hello world";
}

TestProject 只有一个测试用例。

#include "BaseClass.h"
#include <list>
.
.
.
[TestMethod]
void TestMethod1()
{
    BaseClass b;
    b.Method1();
};

看起来如果我在 #include "BaseClass.h" 之后有一个 #include (在 test.cpp 中),我将出现以下链接错误。如果我取出#include ,则根本没有链接错误。

TestLib.lib(BaseClass.obj) : warning LNK4075: ignoring '/EDITANDCONTINUE' due to '/INCREMENTAL:NO' specification
MSVCMRT.lib(locale0_implib.obj) : error LNK2022: metadata operation failed (8013118D) : Inconsistent layout information in duplicated types (std.basic_string<char,std::char_traits<char>,std::allocator<char> >): (0x0200003d).
MSVCMRT.lib(locale0_implib.obj) : error LNK2022: metadata operation failed (8013118D) : Inconsistent layout information in duplicated types (std.basic_string<wchar_t,std::char_traits<wchar_t>,std::allocator<wchar_t> >): (0x02000063).
LINK : fatal error LNK1255: link failed because of metadata errors

如果我在测试程序中再添加一行,链接错误就会消失,如下所示:

#include "BaseClass.h"
#include <list>
.
.
.
[TestMethod]
void TestMethod1()
{
    std::list<int> dummy(0);
    BaseClass b;
    b.Method1();
};

但是,现在,我有两个链接警告。我不确定它们是否与之前的链接错误有关。

TestLib.lib(BaseClass.obj) : warning LNK4075: ignoring '/EDITANDCONTINUE' due to '/INCREMENTAL:NO' specification
LINK : warning LNK4098: defaultlib 'MSVCRTD' conflicts with use of other libs; use /NODEFAULTLIB:library

有人能解释一下为什么吗?我在这里错过了一些明显的东西吗?

I am trying to build a very simple C++ unit test project. The setup is just so happened to be exactly the same as what this blog described. I built a static library TestLib.lib and a C++ unit test project called TestProject. Both projects are using platform toolset v100.

The Testlib contains only one class.

BaseClass.h

#pragma once 

class BaseClass
{
public:
    void Method1();
};

BaseClass.cpp

#include "BaseClass.h"
#include <iostream>
#include <list>

using namespace std;

void BaseClass::Method1()
{
    list<int> dummy(0);
    cout << "Hello world";
}

The TestProject has only one test case.

#include "BaseClass.h"
#include <list>
.
.
.
[TestMethod]
void TestMethod1()
{
    BaseClass b;
    b.Method1();
};

It looks like if I have a #include <list> after #include "BaseClass.h" (in the test.cpp) I will have the following link error. If I take out the #include <list>, I have no link error at all.

TestLib.lib(BaseClass.obj) : warning LNK4075: ignoring '/EDITANDCONTINUE' due to '/INCREMENTAL:NO' specification
MSVCMRT.lib(locale0_implib.obj) : error LNK2022: metadata operation failed (8013118D) : Inconsistent layout information in duplicated types (std.basic_string<char,std::char_traits<char>,std::allocator<char> >): (0x0200003d).
MSVCMRT.lib(locale0_implib.obj) : error LNK2022: metadata operation failed (8013118D) : Inconsistent layout information in duplicated types (std.basic_string<wchar_t,std::char_traits<wchar_t>,std::allocator<wchar_t> >): (0x02000063).
LINK : fatal error LNK1255: link failed because of metadata errors

The link error will be gone if I add one more line to the test program, like this:

#include "BaseClass.h"
#include <list>
.
.
.
[TestMethod]
void TestMethod1()
{
    std::list<int> dummy(0);
    BaseClass b;
    b.Method1();
};

However, now, I have two link warnings. I am not sure whether they are related to the previous link errors.

TestLib.lib(BaseClass.obj) : warning LNK4075: ignoring '/EDITANDCONTINUE' due to '/INCREMENTAL:NO' specification
LINK : warning LNK4098: defaultlib 'MSVCRTD' conflicts with use of other libs; use /NODEFAULTLIB:library

Can anybody explain why? Do I miss something obvious here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

兲鉂ぱ嘚淚 2024-12-13 05:32:13

错误是否仅在编译 Debug 配置时出现?如果是这样,则可能与您的 C++ 运行时库链接有关:

http://social.msdn.microsoft.com/Forums/eu/vclanguage/thread/e5a78770-4d99-40b7-951f-e4466d2744a8

Does the error only show up when compiling the Debug configuration? If so, it may be related to your C++ run-time library linkage:

http://social.msdn.microsoft.com/Forums/eu/vclanguage/thread/e5a78770-4d99-40b7-951f-e4466d2744a8

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文