如何使用GNU Make项目设置Google Test?

时间:2013-04-04 14:13:31

标签: c++ g++ makefile googletest

由于Google Test网页基本上没有文档 - 我该怎么做? 我到目前为止做了什么:

  • 我从项目页面下载了googletest 1.6,并在其中添加了./configure && make
  • 我将-Igtest/include -Lgtest/lib添加到我的编译器/链接器标志
  • 我写了一个小样本测试:

    #include "gtest/gtest.h"
    
    int main(int argc, char **args)
    {
      return 0;
    }
    
    TEST(someTest,testOne)
    {
      ASSERT_EQ(5,5);
    }
    

    这编译很好,但链接器似乎根本没有被逗乐。我以

    的风格收到大量错误信息
      

    test / main.o:在函数someTest_testOne_Test::TestBody()': main.cpp:(.text+0x96): undefined reference to中测试:: internal :: AssertHelper :: AssertHelper(testing :: TestPartResult :: Type,char const *,int,char const *)'

现在我忘记了做什么?

4 个答案:

答案 0 :(得分:1)

我在sudo apt-get install libgtest-dev的系统上安装了Google Test,而我正在使用的治具没有main(),可以通过以下方式构建:

g++ unitTest.cpp -o unitTest /usr/src/gtest/src/gtest_main.cc /usr/src/gtest/src/gtest-all.cc -I /usr/include -I /usr/src/gtest -L /usr/local/lib -lpthread

答案 1 :(得分:0)

最好的示例Makefile是the one distributed with Google Test。它会根据您是否要使用Google的gtest_main.a功能或您自己的功能,向您展示如何将gtest.amain()与您的二进制文件相关联。

答案 2 :(得分:0)

在我向项目makefile添加内容之前,我想弄清楚它实际运行的是什么命令。所以这里是我用来手动构建sample1单元测试的命令列表。

g ++ -c -I ../ include sample1.cc

g ++ -c -I ../ include sample1_unittest.cc

g ++ -pthread -o s1_ut sample1.o sample1_unittest.o ../lib/.libs/libgtest.a ../ lib / .libs / libgtest_main.a

注意:如果您收到一堆与pthread相关的链接器错误,则在第3个命令中忘记了-pthread。如果得到一堆与C ++运行时库相关的链接器错误,则键入gcc而不是g ++。

答案 3 :(得分:0)

仅作为参考,我有一个dockerg++正常工作的gtest系统设置。我在这里提供所有文件供以后参考:

Dockerfile

FROM gcc:9.2.0

WORKDIR /usr/src/app

RUN apt-get -qq update \
    && apt-get -qq install --no-install-recommends cmake \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/*

RUN git clone --depth=1 -b master https://github.com/google/googletest.git
RUN mkdir googletest/build

WORKDIR /usr/src/app/googletest/build

RUN cmake .. \
    && make \
    && make install \
    && rm -rf /usr/src/app/googletest

WORKDIR /usr/src/app

COPY . .
RUN mkdir obj

RUN make

CMD [ "./main" ]

Makefile

CXX = g++
CXXFLAGS = -std=c++17 -Wall -I h -I /usr/local/include/gtest/ -c
LXXFLAGS = -std=c++17 -I h -pthread
OBJECTS = ./obj/program.o ./obj/main.o ./obj/program_unittest.o
GTEST = /usr/local/lib/libgtest.a
TARGET = main


$(TARGET): $(OBJECTS)
    $(CXX) $(LXXFLAGS) -o $(TARGET) $(OBJECTS) $(GTEST)
./obj/program.o: ./cpp/program.cpp
    $(CXX) $(CXXFLAGS) ./cpp/program.cpp -o ./obj/program.o
./obj/program_unittest.o: ./cpp/program_unittest.cpp
    $(CXX) $(CXXFLAGS) ./cpp/program_unittest.cpp -o ./obj/program_unittest.o
./obj/main.o: ./cpp/main.cpp
    $(CXX) $(CXXFLAGS) ./cpp/main.cpp -o ./obj/main.o
clean:
    rm -fv $(TARGET) $(OBJECTS)

cpp / maincpp

#include <iostream>
#include "program.h"
#include "gtest/gtest.h"

int main(int argc, char **argv)
{
    ::testing::InitGoogleTest(&argc, argv);
    std::cout << "RUNNING TESTS ..." << std::endl;
    int ret{RUN_ALL_TESTS()};
    if (!ret)
        std::cout << "<<<SUCCESS>>>" << std::endl;
    else
        std::cout << "FAILED" << std::endl;
    return 0;
}

cpp / program.cpp

#include "program.h"

// Returns n! (the factorial of n).  For negative n, n! is defined to be 1.
int Factorial(int n)
{
    int result = 1;
    for (int i = 1; i <= n; i++)
    {
        result *= i;
    }

    return result;
}

// Returns true if and only if n is a prime number.
bool IsPrime(int n)
{
    // Trivial case 1: small numbers
    if (n <= 1)
        return false;

    // Trivial case 2: even numbers
    if (n % 2 == 0)
        return n == 2;

    // Now, we have that n is odd and n >= 3.

    // Try to divide n by every odd number i, starting from 3
    for (int i = 3;; i += 2)
    {
        // We only have to try i up to the square root of n
        if (i > n / i)
            break;

        // Now, we have i <= n/i < n.
        // If n is divisible by i, n is not prime.
        if (n % i == 0)
            return false;
    }

    // n has no integer factor in the range (1, n), and thus is prime.
    return true;
}

cpp / program_unittest.cpp

#include <limits.h>
#include "program.h"
#include "gtest/gtest.h"
namespace
{

// Tests Factorial().

// Tests factorial of negative numbers.
TEST(FactorialTest, Negative)
{
    // This test is named "Negative", and belongs to the "FactorialTest"
    // test case.
    EXPECT_EQ(1, Factorial(-5));
    EXPECT_EQ(1, Factorial(-1));
    EXPECT_GT(Factorial(-10), 0);

}

// Tests factorial of 0.
TEST(FactorialTest, Zero)
{
    EXPECT_EQ(1, Factorial(0));
}

// Tests factorial of positive numbers.
TEST(FactorialTest, Positive)
{
    EXPECT_EQ(1, Factorial(1));
    EXPECT_EQ(2, Factorial(2));
    EXPECT_EQ(6, Factorial(3));
    EXPECT_EQ(40320, Factorial(8));
}

// Tests IsPrime()

// Tests negative input.
TEST(IsPrimeTest, Negative)
{
    // This test belongs to the IsPrimeTest test case.

    EXPECT_FALSE(IsPrime(-1));
    EXPECT_FALSE(IsPrime(-2));
    EXPECT_FALSE(IsPrime(INT_MIN));
}

// Tests some trivial cases.
TEST(IsPrimeTest, Trivial)
{
    EXPECT_FALSE(IsPrime(0));
    EXPECT_FALSE(IsPrime(1));
    EXPECT_TRUE(IsPrime(2));
    EXPECT_TRUE(IsPrime(3));
}

// Tests positive input.
TEST(IsPrimeTest, Positive)
{
    EXPECT_FALSE(IsPrime(4));
    EXPECT_TRUE(IsPrime(5));
    EXPECT_FALSE(IsPrime(6));
    EXPECT_TRUE(IsPrime(23));
}

h / program.h

#ifndef GTEST_PROGRAM_H_
#define GTEST_PROGRAM_H_

// Returns n! (the factorial of n).  For negative n, n! is defined to be 1.
int Factorial(int n);

// Returns true if and only if n is a prime number.
bool IsPrime(int n);

#endif // GTEST_PROGRAM_H_

cpp/program.cpph/program.h文件来自googletest存储库sample 1Dockerfile改编自here