如何在 visual studio 代码 MinGW 项目中包含 google 测试
How to include google test in visual studio code MinGW project
我正在 Windows 上进行 Visual Studio 代码项目,我想使用 Google 测试。只有当我尝试构建项目时,我才能包含头文件,我得到多个未定义的引用,可以在下面的错误列表中看到这些引用。
我已经将 Google Test 安装到 C:\googleTest。然后我 运行 在文件夹中执行以下命令:
cmake .
cmake --build .
navigated to the googletest\googletest\gtest.sln
And build this with the help of Visual Studio i build the solutions by pressing build all under build menu.
我尝试过的:
我在 task.json 参数中添加了以下内容。
"-l",
"gtest",
"-l",
"gtest_main"
导致此错误:
> Executing task: ""C:\Program Files\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64\bin\g++.exe" -g "C:\Users\melvin.wiegman\OneDrive - Fourtress\Cursus\Design Patterns in Modern C++\Singleton\TestProblemSingleton\*.cpp" -o "C:\Users\melvin.wiegman\OneDrive - Fourtress\Cursus\Design Patterns in Modern C++\Singleton\TestProblemSingleton\.vscode\myProgram.exe" -I C:/Boost/include/boost-1_73 -I C:/GoogleTest/googletest/googletest/include -I C:/GoogleTest/googletest/bin -l gtest -l gtest_main" <
C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lgtest
C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lgtest_main
collect2.exe: error: ld returned 1 exit status
The terminal process "C:\WINDOWS\System32\cmd.exe /d /c ""C:\Program Files\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64\bin\g++.exe" -g "C:\Users\melvin.wiegman\OneDrive - Fourtress\Cursus\Design Patterns in Modern C++\Singleton\TestProblemSingleton\*.cpp" -o "C:\Users\melvin.wiegman\OneDrive - Fourtress\Cursus\Design Patterns in Modern C++\Singleton\TestProblemSingleton\.vscode\myProgram.exe" -I C:/Boost/include/boost-1_73 -I C:/GoogleTest/googletest/googletest/include -I C:/GoogleTest/googletest/bin -l gtest -l gtest_main"" terminated with exit code: 1.
它似乎无法正确找到 google 测试库,但我不知道这是因为我忘记了某处的包含还是我不知道的步骤?
有人可以告诉我如何正确地将 google 测试添加到我的项目中并向我解释我在包含方面做错了什么吗?
错误列表:
C:\Users\MELVIN~1.WIE\AppData\Local\Temp\cc0Ew4Pg.o: In function `RECORDFINDERTESTS_SINGLETONTOTALPOPTEST_Test::TestBody()':
C:/Users/melvin.wiegman/OneDrive - Fourtress/Cursus/Design Patterns in Modern C++/Singleton/TestProblemSingleton/main.cpp:61: undefined reference to `testing::Message::Message()'
C:/Users/melvin.wiegman/OneDrive - Fourtress/Cursus/Design Patterns in Modern C++/Singleton/TestProblemSingleton/main.cpp:61: undefined reference to `testing::internal::AssertHelper::AssertHelper(testing::TestPartResult::Type, char const*, int, char const*)'
C:/Users/melvin.wiegman/OneDrive - Fourtress/Cursus/Design Patterns in Modern C++/Singleton/TestProblemSingleton/main.cpp:61: undefined reference to `testing::internal::AssertHelper::operator=(testing::Message const&) const'
C:/Users/melvin.wiegman/OneDrive - Fourtress/Cursus/Design Patterns in Modern C++/Singleton/TestProblemSingleton/main.cpp:61: undefined reference to `testing::internal::AssertHelper::~AssertHelper()'
C:/Users/melvin.wiegman/OneDrive - Fourtress/Cursus/Design Patterns in Modern C++/Singleton/TestProblemSingleton/main.cpp:61: undefined reference to `testing::internal::AssertHelper::~AssertHelper()'
C:\Users\MELVIN~1.WIE\AppData\Local\Temp\cc0Ew4Pg.o: In function `main':
C:/Users/melvin.wiegman/OneDrive - Fourtress/Cursus/Design Patterns in Modern C++/Singleton/TestProblemSingleton/main.cpp:66: undefined reference to `testing::InitGoogleTest(int*, char**)'
C:\Users\MELVIN~1.WIE\AppData\Local\Temp\cc0Ew4Pg.o: In function `__static_initialization_and_destruction_0':
C:/Users/melvin.wiegman/OneDrive - Fourtress/Cursus/Design Patterns in Modern C++/Singleton/TestProblemSingleton/main.cpp:56: undefined reference to `testing::internal::GetTestTypeId()'
C:/Users/melvin.wiegman/OneDrive - Fourtress/Cursus/Design Patterns in Modern C++/Singleton/TestProblemSingleton/main.cpp:56: undefined reference to `testing::internal::MakeAndRegisterTestInfo(char const*, char const*, char const*, char const*, testing::internal::CodeLocation, void const*, void (*)(), void (*)(), testing::internal::TestFactoryBase*)'
C:\Users\MELVIN~1.WIE\AppData\Local\Temp\cc0Ew4Pg.o: In function `RUN_ALL_TESTS()':
C:/GoogleTest/googletest/googletest/include/gtest/gtest.h:2486: undefined reference to `testing::UnitTest::GetInstance()'
C:/GoogleTest/googletest/googletest/include/gtest/gtest.h:2486: undefined reference to `testing::UnitTest::Run()'
C:\Users\MELVIN~1.WIE\AppData\Local\Temp\cc0Ew4Pg.o: In function `testing::internal::SuiteApiResolver<testing::Test>::GetSetUpCaseOrSuite(char const*, int)':
C:/GoogleTest/googletest/googletest/include/gtest/internal/gtest-internal.h:529: undefined reference to `testing::internal::IsTrue(bool)'
C:/GoogleTest/googletest/googletest/include/gtest/internal/gtest-internal.h:529: undefined reference to `testing::internal::GTestLog::GTestLog(testing::internal::GTestLogSeverity, char const*, int)'
C:/GoogleTest/googletest/googletest/include/gtest/internal/gtest-internal.h:529: undefined reference to `testing::internal::GTestLog::~GTestLog()'
C:/GoogleTest/googletest/googletest/include/gtest/internal/gtest-internal.h:529: undefined reference to `testing::internal::GTestLog::~GTestLog()'
C:\Users\MELVIN~1.WIE\AppData\Local\Temp\cc0Ew4Pg.o: In function `testing::internal::SuiteApiResolver<testing::Test>::GetTearDownCaseOrSuite(char const*, int)':
C:/GoogleTest/googletest/googletest/include/gtest/internal/gtest-internal.h:550: undefined reference to `testing::internal::IsTrue(bool)'
C:/GoogleTest/googletest/googletest/include/gtest/internal/gtest-internal.h:550: undefined reference to `testing::internal::GTestLog::GTestLog(testing::internal::GTestLogSeverity, char const*, int)'
C:/GoogleTest/googletest/googletest/include/gtest/internal/gtest-internal.h:550: undefined reference to `testing::internal::GTestLog::~GTestLog()'
C:/GoogleTest/googletest/googletest/include/gtest/internal/gtest-internal.h:550: undefined reference to `testing::internal::GTestLog::~GTestLog()'
C:\Users\MELVIN~1.WIE\AppData\Local\Temp\cc0Ew4Pg.o: In function `testing::AssertionResult testing::internal::CmpHelperEQ<int, int>(char const*, char const*, int const&, int const&)':
C:/GoogleTest/googletest/googletest/include/gtest/gtest.h:1546: undefined reference to `testing::AssertionSuccess()'
C:\Users\MELVIN~1.WIE\AppData\Local\Temp\cc0Ew4Pg.o: In function `testing::AssertionResult testing::internal::CmpHelperEQFailure<int, int>(char const*, char const*, int const&, int const&)':
C:/GoogleTest/googletest/googletest/include/gtest/gtest.h:1529: undefined reference to `testing::internal::EqFailure(char const*, char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, bool)'
C:\Users\MELVIN~1.WIE\AppData\Local\Temp\cc0Ew4Pg.o:main.cpp:(.rdata$_ZTV44RECORDFINDERTESTS_SINGLETONTOTALPOPTEST_Test[_ZTV44RECORDFINDERTESTS_SINGLETONTOTALPOPTEST_Test]+0x20): undefined reference to `testing::Test::SetUp()'
C:\Users\MELVIN~1.WIE\AppData\Local\Temp\cc0Ew4Pg.o:main.cpp:(.rdata$_ZTV44RECORDFINDERTESTS_SINGLETONTOTALPOPTEST_Test[_ZTV44RECORDFINDERTESTS_SINGLETONTOTALPOPTEST_Test]+0x28): undefined reference to `testing::Test::TearDown()'
C:\Users\MELVIN~1.WIE\AppData\Local\Temp\cc0Ew4Pg.o: In function `RECORDFINDERTESTS_SINGLETONTOTALPOPTEST_Test::~RECORDFINDERTESTS_SINGLETONTOTALPOPTEST_Test()':
C:/Users/melvin.wiegman/OneDrive - Fourtress/Cursus/Design Patterns in Modern C++/Singleton/TestProblemSingleton/main.cpp:56: undefined reference to `testing::Test::~Test()'
C:\Users\MELVIN~1.WIE\AppData\Local\Temp\cc0Ew4Pg.o: In function `RECORDFINDERTESTS_SINGLETONTOTALPOPTEST_Test::RECORDFINDERTESTS_SINGLETONTOTALPOPTEST_Test()':
C:/Users/melvin.wiegman/OneDrive - Fourtress/Cursus/Design Patterns in Modern C++/Singleton/TestProblemSingleton/main.cpp:56: undefined reference to `testing::Test::Test()'
collect2.exe: error: ld returned 1 exit status
The terminal process "C:\WINDOWS\System32\cmd.exe /d /c ""C:\Program Files\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64\bin\g++.exe" -g "C:\Users\melvin.wiegman\OneDrive - Fourtress\Cursus\Design Patterns in Modern C++\Singleton\TestProblemSingleton\*.cpp" -o "C:\Users\melvin.wiegman\OneDrive - Fourtress\Cursus\Design Patterns in Modern C++\Singleton\TestProblemSingleton\.vscode\myProgram.exe" -I C:/Boost/include/boost-1_73 -I C:/GoogleTest/googletest/googletest/include -I C:/GoogleTest/googletest/bin"" terminated with exit code: 1.
我的项目文件:
main.cpp
#include <iostream>
#include <vector>
#include <string>
#include <map>
#include <fstream>
#include <boost/lexical_cast.hpp>
#define GTEST_LANG_CXX 1
#include <gtest/gtest.h>
using namespace std;
class SingletonDatabase
{
SingletonDatabase()
{
cout << "INIT DATABASE\n";
ifstream ifs("capticals.txt");
string s, s2;
while (getline(ifs, s))
{
getline(ifs, s2);
int pop = boost::lexical_cast<int>(s2);
capitals[s] = pop;
}
}
map<string, int> capitals;
public:
SingletonDatabase(SingletonDatabase const&) = delete;
void operator=(SingletonDatabase const&) = delete;
static SingletonDatabase& get()
{
static SingletonDatabase db;
return db;
}
int get_population(const string& name)
{
return capitals[name];
}
};
struct SingletonRecordFinder
{
int total_population(vector<string> names)
{
int result{0};
for (auto& name : names)
{
result += SingletonDatabase::get().get_population(name);
}
return result;
}
};
TEST(RECORDFINDERTESTS, SINGLETONTOTALPOPTEST)
{
SingletonRecordFinder rf;
vector<string> names = { "Germany", "Tokyo"};
int tp = rf.total_population(names);
EXPECT_EQ(25000000+140000000, tp);
}
int main(int ac, char* av[])
{
testing::InitGoogleTest(&ac,av);
return RUN_ALL_TESTS();
}
task.json:
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "C/C++: g++.exe build active file",
"command": "C:\Program Files\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64\bin\g++.exe",
"args": [
"-g",
"${workspaceFolder}\*.cpp",
"-o",
"${fileDirname}\myProgram.exe",
"-I",
"C:/Boost/include/boost-1_73",
"-I",
"C:/GoogleTest/googletest/googletest/include",
"-I",
"C:/GoogleTest/googletest/bin",
"-L",
"C:/GoogleTest/googletest/lib/Debug"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
c_cpp_properties.json:
{
"configurations": [
{
"name": "GCC",
"includePath": [
"${workspaceFolder}/**",
"C:/GoogleTest/googletest/**"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"windowsSdkVersion": "10.0.18362.0",
"compilerPath": "C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.26.28801/bin/Hostx64/x64/cl.exe",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "msvc-x64"
}
],
"version": 4
}
launch.json:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "g++.exe - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\myProgram.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "C:\Program Files\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64\bin\gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: g++.exe build active file"
}
]
}
类似于 -I(大写“eye”)表示在哪里可以找到包含文件,您需要一个 -L 表示在哪里可以找到库文件。
在 评论后我终于明白了:
我用mingw编译器以正确的方式重新编译了google测试库,如下图所示:
GoogleTest MinGW compiled
然后我修改了我的文件如下。
在 launch.json 文件中,我将 preLaunchTask 引用到我的“mingw 构建”,这使得在首次启动调试器时将执行此构建任务。请务必将您的 program.exe 正确引用到在构建任务中创建的 launch.json。这将 运行 调试器使用给定的可执行文件。
我还将我的 task.json 和所需的参数添加到 link myprogram 到构建 google 测试库和包含目录。
我现在可以使用 CTRL + SHIFT + B 构建,运行 使用 F5 调试程序。这与项目中包含的 google 测试库一模一样!
文件:
main.cpp
#include <iostream>
#include <vector>
#include <string>
#include <map>
#include <fstream>
#include <boost/lexical_cast.hpp>
#define GTEST_LANG_CXX 1
#include <gtest/gtest.h>
using namespace std;
class SingletonDatabase
{
SingletonDatabase()
{
cout << "INIT DATABASE\n";
ifstream ifs("capticals.txt");
string s, s2;
while (getline(ifs, s))
{
getline(ifs, s2);
int pop = boost::lexical_cast<int>(s2);
capitals[s] = pop;
}
}
map<string, int> capitals;
public:
SingletonDatabase(SingletonDatabase const&) = delete;
void operator=(SingletonDatabase const&) = delete;
static SingletonDatabase& get()
{
static SingletonDatabase db;
return db;
}
int get_population(const string& name)
{
return capitals[name];
}
};
struct SingletonRecordFinder
{
int total_population(vector<string> names)
{
int result{0};
for (auto& name : names)
{
result += SingletonDatabase::get().get_population(name);
}
return result;
}
};
TEST(RECORDFINDERTESTS, SINGLETONTOTALPOPTEST)
{
SingletonRecordFinder rf;
vector<string> names = { "Germany", "Tokyo"};
int tp = rf.total_population(names);
EXPECT_EQ(25000000+140000000, tp);
}
int main(int ac, char* av[])
{
testing::InitGoogleTest(&ac,av);
return RUN_ALL_TESTS();
}
TASK.JSON
{
"version": "2.0.0",
"tasks": [
{
"label": "mingw build",
"type": "shell",
"command": "C:\Program Files\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64\bin\g++.exe",
"args": [
"-g",
"${workspaceFolder}\*.cpp",
"-o",
"${fileDirname}\myProgram.exe",
"-I",
"C:/Boost/include/boost-1_73",
"-I",
"C:/googletest/googletest/include",
"-L",
"C:/googletest/googletest/lib",
"-lgtest",
"-lgtest_main"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": "build"
}
]
}
LAUNCH.JSON
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "C/C++: g++.exe build active file",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/myProgram.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "C:\Program Files\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64\bin\gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "mingw build"
}
]
}
C_CPP_PROPERTIES.json
{
"configurations": [
{
"name": "GCC",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"windowsSdkVersion": "10.0.18362.0",
"compilerPath": "C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/g++.exe",
"cStandard": "c17",
"cppStandard": "c++17",
"intelliSenseMode": "g++-x64"
}
],
"version": 4
}
我正在 Windows 上进行 Visual Studio 代码项目,我想使用 Google 测试。只有当我尝试构建项目时,我才能包含头文件,我得到多个未定义的引用,可以在下面的错误列表中看到这些引用。
我已经将 Google Test 安装到 C:\googleTest。然后我 运行 在文件夹中执行以下命令:
cmake .
cmake --build .
navigated to the googletest\googletest\gtest.sln
And build this with the help of Visual Studio i build the solutions by pressing build all under build menu.
我尝试过的: 我在 task.json 参数中添加了以下内容。
"-l",
"gtest",
"-l",
"gtest_main"
导致此错误:
> Executing task: ""C:\Program Files\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64\bin\g++.exe" -g "C:\Users\melvin.wiegman\OneDrive - Fourtress\Cursus\Design Patterns in Modern C++\Singleton\TestProblemSingleton\*.cpp" -o "C:\Users\melvin.wiegman\OneDrive - Fourtress\Cursus\Design Patterns in Modern C++\Singleton\TestProblemSingleton\.vscode\myProgram.exe" -I C:/Boost/include/boost-1_73 -I C:/GoogleTest/googletest/googletest/include -I C:/GoogleTest/googletest/bin -l gtest -l gtest_main" <
C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lgtest
C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lgtest_main
collect2.exe: error: ld returned 1 exit status
The terminal process "C:\WINDOWS\System32\cmd.exe /d /c ""C:\Program Files\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64\bin\g++.exe" -g "C:\Users\melvin.wiegman\OneDrive - Fourtress\Cursus\Design Patterns in Modern C++\Singleton\TestProblemSingleton\*.cpp" -o "C:\Users\melvin.wiegman\OneDrive - Fourtress\Cursus\Design Patterns in Modern C++\Singleton\TestProblemSingleton\.vscode\myProgram.exe" -I C:/Boost/include/boost-1_73 -I C:/GoogleTest/googletest/googletest/include -I C:/GoogleTest/googletest/bin -l gtest -l gtest_main"" terminated with exit code: 1.
它似乎无法正确找到 google 测试库,但我不知道这是因为我忘记了某处的包含还是我不知道的步骤?
有人可以告诉我如何正确地将 google 测试添加到我的项目中并向我解释我在包含方面做错了什么吗?
错误列表:
C:\Users\MELVIN~1.WIE\AppData\Local\Temp\cc0Ew4Pg.o: In function `RECORDFINDERTESTS_SINGLETONTOTALPOPTEST_Test::TestBody()':
C:/Users/melvin.wiegman/OneDrive - Fourtress/Cursus/Design Patterns in Modern C++/Singleton/TestProblemSingleton/main.cpp:61: undefined reference to `testing::Message::Message()'
C:/Users/melvin.wiegman/OneDrive - Fourtress/Cursus/Design Patterns in Modern C++/Singleton/TestProblemSingleton/main.cpp:61: undefined reference to `testing::internal::AssertHelper::AssertHelper(testing::TestPartResult::Type, char const*, int, char const*)'
C:/Users/melvin.wiegman/OneDrive - Fourtress/Cursus/Design Patterns in Modern C++/Singleton/TestProblemSingleton/main.cpp:61: undefined reference to `testing::internal::AssertHelper::operator=(testing::Message const&) const'
C:/Users/melvin.wiegman/OneDrive - Fourtress/Cursus/Design Patterns in Modern C++/Singleton/TestProblemSingleton/main.cpp:61: undefined reference to `testing::internal::AssertHelper::~AssertHelper()'
C:/Users/melvin.wiegman/OneDrive - Fourtress/Cursus/Design Patterns in Modern C++/Singleton/TestProblemSingleton/main.cpp:61: undefined reference to `testing::internal::AssertHelper::~AssertHelper()'
C:\Users\MELVIN~1.WIE\AppData\Local\Temp\cc0Ew4Pg.o: In function `main':
C:/Users/melvin.wiegman/OneDrive - Fourtress/Cursus/Design Patterns in Modern C++/Singleton/TestProblemSingleton/main.cpp:66: undefined reference to `testing::InitGoogleTest(int*, char**)'
C:\Users\MELVIN~1.WIE\AppData\Local\Temp\cc0Ew4Pg.o: In function `__static_initialization_and_destruction_0':
C:/Users/melvin.wiegman/OneDrive - Fourtress/Cursus/Design Patterns in Modern C++/Singleton/TestProblemSingleton/main.cpp:56: undefined reference to `testing::internal::GetTestTypeId()'
C:/Users/melvin.wiegman/OneDrive - Fourtress/Cursus/Design Patterns in Modern C++/Singleton/TestProblemSingleton/main.cpp:56: undefined reference to `testing::internal::MakeAndRegisterTestInfo(char const*, char const*, char const*, char const*, testing::internal::CodeLocation, void const*, void (*)(), void (*)(), testing::internal::TestFactoryBase*)'
C:\Users\MELVIN~1.WIE\AppData\Local\Temp\cc0Ew4Pg.o: In function `RUN_ALL_TESTS()':
C:/GoogleTest/googletest/googletest/include/gtest/gtest.h:2486: undefined reference to `testing::UnitTest::GetInstance()'
C:/GoogleTest/googletest/googletest/include/gtest/gtest.h:2486: undefined reference to `testing::UnitTest::Run()'
C:\Users\MELVIN~1.WIE\AppData\Local\Temp\cc0Ew4Pg.o: In function `testing::internal::SuiteApiResolver<testing::Test>::GetSetUpCaseOrSuite(char const*, int)':
C:/GoogleTest/googletest/googletest/include/gtest/internal/gtest-internal.h:529: undefined reference to `testing::internal::IsTrue(bool)'
C:/GoogleTest/googletest/googletest/include/gtest/internal/gtest-internal.h:529: undefined reference to `testing::internal::GTestLog::GTestLog(testing::internal::GTestLogSeverity, char const*, int)'
C:/GoogleTest/googletest/googletest/include/gtest/internal/gtest-internal.h:529: undefined reference to `testing::internal::GTestLog::~GTestLog()'
C:/GoogleTest/googletest/googletest/include/gtest/internal/gtest-internal.h:529: undefined reference to `testing::internal::GTestLog::~GTestLog()'
C:\Users\MELVIN~1.WIE\AppData\Local\Temp\cc0Ew4Pg.o: In function `testing::internal::SuiteApiResolver<testing::Test>::GetTearDownCaseOrSuite(char const*, int)':
C:/GoogleTest/googletest/googletest/include/gtest/internal/gtest-internal.h:550: undefined reference to `testing::internal::IsTrue(bool)'
C:/GoogleTest/googletest/googletest/include/gtest/internal/gtest-internal.h:550: undefined reference to `testing::internal::GTestLog::GTestLog(testing::internal::GTestLogSeverity, char const*, int)'
C:/GoogleTest/googletest/googletest/include/gtest/internal/gtest-internal.h:550: undefined reference to `testing::internal::GTestLog::~GTestLog()'
C:/GoogleTest/googletest/googletest/include/gtest/internal/gtest-internal.h:550: undefined reference to `testing::internal::GTestLog::~GTestLog()'
C:\Users\MELVIN~1.WIE\AppData\Local\Temp\cc0Ew4Pg.o: In function `testing::AssertionResult testing::internal::CmpHelperEQ<int, int>(char const*, char const*, int const&, int const&)':
C:/GoogleTest/googletest/googletest/include/gtest/gtest.h:1546: undefined reference to `testing::AssertionSuccess()'
C:\Users\MELVIN~1.WIE\AppData\Local\Temp\cc0Ew4Pg.o: In function `testing::AssertionResult testing::internal::CmpHelperEQFailure<int, int>(char const*, char const*, int const&, int const&)':
C:/GoogleTest/googletest/googletest/include/gtest/gtest.h:1529: undefined reference to `testing::internal::EqFailure(char const*, char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, bool)'
C:\Users\MELVIN~1.WIE\AppData\Local\Temp\cc0Ew4Pg.o:main.cpp:(.rdata$_ZTV44RECORDFINDERTESTS_SINGLETONTOTALPOPTEST_Test[_ZTV44RECORDFINDERTESTS_SINGLETONTOTALPOPTEST_Test]+0x20): undefined reference to `testing::Test::SetUp()'
C:\Users\MELVIN~1.WIE\AppData\Local\Temp\cc0Ew4Pg.o:main.cpp:(.rdata$_ZTV44RECORDFINDERTESTS_SINGLETONTOTALPOPTEST_Test[_ZTV44RECORDFINDERTESTS_SINGLETONTOTALPOPTEST_Test]+0x28): undefined reference to `testing::Test::TearDown()'
C:\Users\MELVIN~1.WIE\AppData\Local\Temp\cc0Ew4Pg.o: In function `RECORDFINDERTESTS_SINGLETONTOTALPOPTEST_Test::~RECORDFINDERTESTS_SINGLETONTOTALPOPTEST_Test()':
C:/Users/melvin.wiegman/OneDrive - Fourtress/Cursus/Design Patterns in Modern C++/Singleton/TestProblemSingleton/main.cpp:56: undefined reference to `testing::Test::~Test()'
C:\Users\MELVIN~1.WIE\AppData\Local\Temp\cc0Ew4Pg.o: In function `RECORDFINDERTESTS_SINGLETONTOTALPOPTEST_Test::RECORDFINDERTESTS_SINGLETONTOTALPOPTEST_Test()':
C:/Users/melvin.wiegman/OneDrive - Fourtress/Cursus/Design Patterns in Modern C++/Singleton/TestProblemSingleton/main.cpp:56: undefined reference to `testing::Test::Test()'
collect2.exe: error: ld returned 1 exit status
The terminal process "C:\WINDOWS\System32\cmd.exe /d /c ""C:\Program Files\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64\bin\g++.exe" -g "C:\Users\melvin.wiegman\OneDrive - Fourtress\Cursus\Design Patterns in Modern C++\Singleton\TestProblemSingleton\*.cpp" -o "C:\Users\melvin.wiegman\OneDrive - Fourtress\Cursus\Design Patterns in Modern C++\Singleton\TestProblemSingleton\.vscode\myProgram.exe" -I C:/Boost/include/boost-1_73 -I C:/GoogleTest/googletest/googletest/include -I C:/GoogleTest/googletest/bin"" terminated with exit code: 1.
我的项目文件:
main.cpp
#include <iostream>
#include <vector>
#include <string>
#include <map>
#include <fstream>
#include <boost/lexical_cast.hpp>
#define GTEST_LANG_CXX 1
#include <gtest/gtest.h>
using namespace std;
class SingletonDatabase
{
SingletonDatabase()
{
cout << "INIT DATABASE\n";
ifstream ifs("capticals.txt");
string s, s2;
while (getline(ifs, s))
{
getline(ifs, s2);
int pop = boost::lexical_cast<int>(s2);
capitals[s] = pop;
}
}
map<string, int> capitals;
public:
SingletonDatabase(SingletonDatabase const&) = delete;
void operator=(SingletonDatabase const&) = delete;
static SingletonDatabase& get()
{
static SingletonDatabase db;
return db;
}
int get_population(const string& name)
{
return capitals[name];
}
};
struct SingletonRecordFinder
{
int total_population(vector<string> names)
{
int result{0};
for (auto& name : names)
{
result += SingletonDatabase::get().get_population(name);
}
return result;
}
};
TEST(RECORDFINDERTESTS, SINGLETONTOTALPOPTEST)
{
SingletonRecordFinder rf;
vector<string> names = { "Germany", "Tokyo"};
int tp = rf.total_population(names);
EXPECT_EQ(25000000+140000000, tp);
}
int main(int ac, char* av[])
{
testing::InitGoogleTest(&ac,av);
return RUN_ALL_TESTS();
}
task.json:
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "C/C++: g++.exe build active file",
"command": "C:\Program Files\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64\bin\g++.exe",
"args": [
"-g",
"${workspaceFolder}\*.cpp",
"-o",
"${fileDirname}\myProgram.exe",
"-I",
"C:/Boost/include/boost-1_73",
"-I",
"C:/GoogleTest/googletest/googletest/include",
"-I",
"C:/GoogleTest/googletest/bin",
"-L",
"C:/GoogleTest/googletest/lib/Debug"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
c_cpp_properties.json:
{
"configurations": [
{
"name": "GCC",
"includePath": [
"${workspaceFolder}/**",
"C:/GoogleTest/googletest/**"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"windowsSdkVersion": "10.0.18362.0",
"compilerPath": "C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.26.28801/bin/Hostx64/x64/cl.exe",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "msvc-x64"
}
],
"version": 4
}
launch.json:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "g++.exe - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\myProgram.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "C:\Program Files\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64\bin\gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: g++.exe build active file"
}
]
}
类似于 -I(大写“eye”)表示在哪里可以找到包含文件,您需要一个 -L 表示在哪里可以找到库文件。
在
我用mingw编译器以正确的方式重新编译了google测试库,如下图所示:
GoogleTest MinGW compiled
然后我修改了我的文件如下。
在 launch.json 文件中,我将 preLaunchTask 引用到我的“mingw 构建”,这使得在首次启动调试器时将执行此构建任务。请务必将您的 program.exe 正确引用到在构建任务中创建的 launch.json。这将 运行 调试器使用给定的可执行文件。
我还将我的 task.json 和所需的参数添加到 link myprogram 到构建 google 测试库和包含目录。
我现在可以使用 CTRL + SHIFT + B 构建,运行 使用 F5 调试程序。这与项目中包含的 google 测试库一模一样!
文件:
main.cpp
#include <iostream>
#include <vector>
#include <string>
#include <map>
#include <fstream>
#include <boost/lexical_cast.hpp>
#define GTEST_LANG_CXX 1
#include <gtest/gtest.h>
using namespace std;
class SingletonDatabase
{
SingletonDatabase()
{
cout << "INIT DATABASE\n";
ifstream ifs("capticals.txt");
string s, s2;
while (getline(ifs, s))
{
getline(ifs, s2);
int pop = boost::lexical_cast<int>(s2);
capitals[s] = pop;
}
}
map<string, int> capitals;
public:
SingletonDatabase(SingletonDatabase const&) = delete;
void operator=(SingletonDatabase const&) = delete;
static SingletonDatabase& get()
{
static SingletonDatabase db;
return db;
}
int get_population(const string& name)
{
return capitals[name];
}
};
struct SingletonRecordFinder
{
int total_population(vector<string> names)
{
int result{0};
for (auto& name : names)
{
result += SingletonDatabase::get().get_population(name);
}
return result;
}
};
TEST(RECORDFINDERTESTS, SINGLETONTOTALPOPTEST)
{
SingletonRecordFinder rf;
vector<string> names = { "Germany", "Tokyo"};
int tp = rf.total_population(names);
EXPECT_EQ(25000000+140000000, tp);
}
int main(int ac, char* av[])
{
testing::InitGoogleTest(&ac,av);
return RUN_ALL_TESTS();
}
TASK.JSON
{
"version": "2.0.0",
"tasks": [
{
"label": "mingw build",
"type": "shell",
"command": "C:\Program Files\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64\bin\g++.exe",
"args": [
"-g",
"${workspaceFolder}\*.cpp",
"-o",
"${fileDirname}\myProgram.exe",
"-I",
"C:/Boost/include/boost-1_73",
"-I",
"C:/googletest/googletest/include",
"-L",
"C:/googletest/googletest/lib",
"-lgtest",
"-lgtest_main"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": "build"
}
]
}
LAUNCH.JSON
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "C/C++: g++.exe build active file",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/myProgram.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "C:\Program Files\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64\bin\gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "mingw build"
}
]
}
C_CPP_PROPERTIES.json
{
"configurations": [
{
"name": "GCC",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"windowsSdkVersion": "10.0.18362.0",
"compilerPath": "C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/g++.exe",
"cStandard": "c17",
"cppStandard": "c++17",
"intelliSenseMode": "g++-x64"
}
],
"version": 4
}