'LNK2005 linkage error when running c++ unit test
I am relatively new to c++ and I am not sure how I can resolve this error. I have a namespace QueryUtils
which I am including it into another class QuerySyntaxProcessing
. Below are the definitions for the following
QueryUtils.cpp
#pragma once
#include <stdio.h>
#include <string>
#include <vector>
#include <algorithm>
namespace QueryUtils
{
static const std::string SELECT_KEYWORD = "Select" + ' ';
static const std::string SUCHTHAT_KEYWORD = ' ' + "Such that";
size_t split(const std::string& txt, std::vector<std::string>& strs, char ch)
{
size_t pos = txt.find(ch);
size_t initialPos = 0;
strs.clear();
// Decompose statement
while (pos != std::string::npos) {
strs.push_back(txt.substr(initialPos, pos - initialPos));
initialPos = pos + 1;
pos = txt.find(ch, initialPos);
}
// Add the last one
strs.push_back(txt.substr(initialPos, std::min(pos, txt.size()) - initialPos + 1));
return strs.size();
}
std::string getStringBetween(const std::string& s, const std::string& start_delim, const std::string& stop_delim)
{
unsigned first_delim_pos = s.find(start_delim);
unsigned end_pos_of_first_delim = first_delim_pos + start_delim.length();
unsigned last_delim_pos = s.find(stop_delim);
return s.substr(end_pos_of_first_delim,
last_delim_pos - end_pos_of_first_delim);
}
};
QuerySyntaxProcessing.cpp
#include "QuerySyntaxProcessing.h"
#include <ctype.h>
#include <string>
#include <vector>
#include <iostream>
#include <map>
#include "QueryUtils.cpp"
QuerySyntaxProcessing::QuerySyntaxProcessing()
{
entityMap["stmt"] = DesignEntities::NORMAL_STATEMENT;
entityMap["variable"] = DesignEntities::VARIABLE;
designEntities = { "stmt", "read", "print", "call", "while", "if", "assign", "variable", "constant", "procedure" };
}
QuerySyntaxProcessing::~QuerySyntaxProcessing()
{
}
I am omitting more details in QuerySyntaxProcessing.cpp
as the methods are too long. However, I am using the namespace methods of QueryUtils
in the functions for this class. I do not get any errors when building the project.
QuerySyntaxTests.cpp
#include "stdafx.h"
#include <vector>
#include <string>
#include "QuerySyntaxProcessing.h"
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
namespace UnitTesting
{
TEST_CLASS(TestPQLSytnax)
{
public:
TEST_METHOD(SynonymTest) {
QuerySyntaxProcessing* queryProcessing = new QuerySyntaxProcessing();
vector<string> multipleVars = { "v", "a", "cd" };
vector<string> singleVars = { "hello" };
vector<string> wrongVars = { "54v" };
Assert::AreEqual(queryProcessing->isSynonym(multipleVars), true);
Assert::AreEqual(queryProcessing->isSynonym(singleVars), true);
Assert::AreEqual(queryProcessing->isSynonym(wrongVars), false);
}
};
}
When I try to build the unit tests for QuerySyntaxProcessing.cpp
, I get an error LNK 2005.
The error message appears when even trying to initialize the constructor for *QuerySyntaxProcessing
instance. I am not double including QueryUtils.cpp
header in any of the other headers. So not very sure what is wrong.
Error LNK2005 "class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __cdecl QueryUtils::getStringBetween(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (?getStringBetween@QueryUtils@@YA?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@ABV23@00@Z) already defined in QueryUtilsTest.obj UnitTesting C:\Users\yicho\team16-win-spa-19s2\Team00\Code00\UnitTest
ing\SPA.lib(QuerySyntaxProcessing.obj) 1
Thank you all for your help..
Solution 1:[1]
In my case, you should't have the corresponding .cpp file for the unit test header. In your case, it's QuerySyntaxProcessing.cpp
, you shouldn't have it. So all the implement should be written in the .h (It's not difficult with morden C++ standards).
I guess there are some global or static varibles defined in TEST_CLASS, TEST_METHOD or something like that.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Zhang |