Link error while building a C++ Unit test project in VS.NET 2010
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.开发者_如何学C
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?
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
精彩评论