首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在visual studio code MinGW项目中包含google test

如何在visual studio code MinGW项目中包含google test
EN

Stack Overflow用户
提问于 2020-10-27 20:42:34
回答 2查看 793关注 0票数 0

我正在做一个Windows上的Visual Studio代码项目,我想使用Google Test。我只能包含头文件,当我尝试构建项目时,我得到了多个未定义的引用,可以在下面的错误列表中看到这些引用。

我已经在C:\googleTest中安装了Google Test。然后,我在文件夹中运行了以下命令:

代码语言:javascript
复制
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参数中添加了以下内容。

代码语言:javascript
复制
                "-l",
                "gtest",
                "-l",
                "gtest_main"

这导致了这个错误:

代码语言:javascript
复制
> 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测试库,但我不知道这是因为我在什么地方忘记了一个include,或者可能是我没有意识到的一个步骤?

谁能告诉我如何正确地将google test添加到我的项目中,并向我解释一下我在includes中做错了什么?

错误列表:

代码语言:javascript
复制
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

代码语言:javascript
复制
#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:

代码语言:javascript
复制
{
    "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:

代码语言:javascript
复制
{
    "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:

代码语言:javascript
复制
{
    // 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"
        }
    ]
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-11-02 21:43:22

rveerd's评论之后,我终于弄明白了:

我用mingw编译器以正确的方式重新编译了googletest库,如下图所示:

GoogleTest MinGW compiled

然后我修改了我的文件,如下所示。

在launch.json文件中,我将preLaunchTask引用到我的"mingw build“,这使得在第一次启动调试器时,这个构建任务将被执行。确保正确地将您的program.exe引用到在build任务中创建的launch.json。这将使用给定的可执行文件运行调试器。

我还添加了带有所需参数的task.json,以便将我的程序链接到构建的google测试库和include目录。

我现在可以使用CTRL + SHIFT +B进行构建,并使用F5在调试中运行程序。这与项目中包含的google测试库是一致的!

文件:

main.cpp

代码语言:javascript
复制
#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

代码语言:javascript
复制
{
    "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

代码语言:javascript
复制
{
    // 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

代码语言:javascript
复制
{
    "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
}
票数 0
EN

Stack Overflow用户

发布于 2020-10-27 21:10:06

类似于-I (大写的“眼睛”),它告诉你在哪里可以找到包含文件,你需要一个-L来告诉你在哪里可以找到这些库。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64554687

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档