运行 C++ 单元测试时LNK2005链接错误
LNK2005 linkage error when running c++ unit test
我对c ++相对较新,我不确定如何解决此错误。我有一个命名空间QueryUtils
我将其包含在另一个类QuerySyntaxProcessing
中。以下是以下定义的定义
查询实用程序.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);
}
};
查询语法处理.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()
{
}
由于方法太长,我在QuerySyntaxProcessing.cpp
省略了更多详细信息。但是,我在此类的函数中使用了QueryUtils
的命名空间方法。我在构建项目时没有任何错误。
查询语法测试.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);
}
};
}
当我尝试为QuerySyntaxProcessing.cpp
构建单元测试时,我得到一个错误LNK 2005。
甚至尝试初始化实例的构造函数时*QuerySyntaxProcessing
会出现错误消息。我没有在任何其他标题中包含QueryUtils.cpp
标题。所以不太确定出了什么问题。
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:Usersyichoteam16-win-spa-19s2Team00Code00UnitTest
ing\SPA.lib(QuerySyntaxProcessing.obj( 1
谢谢大家的帮助..
就我而言,您不应该有单元测试标头的相应.cpp文件。在你的情况下,它是QuerySyntaxProcessing.cpp
,你不应该拥有它。所以所有的实现都应该写在.h中(用现代C++标准并不难(。
我想在TEST_CLASS、TEST_METHOD或类似的东西中定义了一些全局或静态变量。
- 静态数据成员的问题-修复链接错误会导致编译器错误
- Visual Studio mkl_link_tool.exe链接错误
- C++ 实现模板单例类时出现链接错误
- 使用类模板的方法链接错误
- MySQL C++连接器链接错误
- VS 2015 链接错误 无法构建依赖于 libcurl 的项目
- 运行 C++ 单元测试时LNK2005链接错误
- 对 CMake 中'cudaRegisterLinkedBinary'链接错误的未定义引用?
- 链接错误,包括我创建的相同头文件 - C++
- 在Embarcadero C++ Builder中生成的DLL未解决的外部链接错误
- 使用标头保护的多个定义链接错误
- 链接错误:未定义对 stdscr 和 wgetch 的引用
- 使用 Vivek 的 Vcam / 捕获源过滤器构建/链接错误
- 升压program_options中的链接错误
- 使用 g++ 预处理器进行替换会导致链接错误
- 在调试配置中编译工作正常,但发布会给出链接错误
- 相邻矩阵设置链接错误
- 包含常量变量并包含在多个文件中的标头的链接错误
- C++链接错误,我理解但无法解决
- 是什么导致macOS Mojave上的GoogleTest链接错误