conan gtest demo

related error

  • undefined reference to `testing::internal::FormatFileLocation[abi:cxx11](char const*, int)’
  • undefined reference to `testing::internal::FormatFileLocation[abi:cxx11](char const*, int)’
  • undefined reference to `testing::internal::**Log**

workaround

The problem is that conan does download/compile gtest binaries by default with libstdc++ even if my compiler (gcc 6.3) uses libstdc++11 by default. Thus there is a mismatch between libstdc++ and libstdc++11.

To workaround this issue you have to explicit tell conan to compile with libstdc++11:

Solution 1

1
conan install .. --build missing -s compiler=gcc -s compiler.version=6.3 -s compiler.libcxx=libstdc++11

Solution 2

1
compiler.libcxx=libstdc++ --> compiler.libcxx=libstdc++11

example:

1
2
3
4
5
6
7
8
9
10
11
12
13
cat ~/.conan/profiles/default 
[settings]
os=Linux
os_build=Linux
arch=x86_64
arch_build=x86_64
compiler=gcc
compiler.version=9
compiler.libcxx=libstdc++11
build_type=Release
[options]
[build_requires]
[env]

Reference page

demo project

conanfile.txt

1
2
3
4
5
6
[requires]
gtest/1.10.0

[generators]
cmake

CMakeLists.txt

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
cmake_minimum_required(VERSION 3.12)
project(gmock CXX)
#set(CMAKE_CXX_FLAGS "-std=c++11 -g -O0")
SET(CMAKE_CXX_STANDARD 11)
SET(CMAKE_CXX_STANDARD_REQUIRED ON)

include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup()


set(target mock01)
message(STATUS "CXX FLAGS: ${CMAKE_CXX_FLAGS}")
message("-- Conan libs: ${CONAN_LIBS}")

add_executable (${target} ${target}.cpp)

target_link_libraries (${target}
${CONAN_LIBS}
)

mock01.cpp

ignore the test shit

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66

#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <iostream>
using ::testing::AtLeast;
using ::testing::NiceMock;
using ::testing::StrictMock;
using ::testing::Return;

class Turtle {
public:
Turtle() {};
virtual ~Turtle() {}
virtual void PenUp() = 0;
virtual void PenDown() = 0;
virtual int Count() = 0;
#if 0
virtual void Forward(int distance) = 0;
virtual void Turn(int degrees) = 0;
virtual void GoTo(int x, int y) = 0;
virtual int GetX() const = 0;
virtual int GetY() const = 0;
#endif
};


class MockTurtle : public Turtle {
public:
MockTurtle() {};
~MockTurtle() {};
MOCK_METHOD(void, PenUp, (), (override));

MOCK_METHOD(void, PenDown, (), (override));
MOCK_METHOD(int, Count, (), (override));
#if 0
MOCK_METHOD(void, Forward, (int distance), (override));
MOCK_METHOD(void, Turn, (int degrees), (override));
MOCK_METHOD(void, GoTo, (int x, int y), (override));
MOCK_METHOD(int, GetX, (), (const, override));
MOCK_METHOD(int, GetY, (), (const, override));
#endif
};

TEST(PainterTest, CanDrawSomething) {
#if 0
MockTurtle turtle;
EXPECT_CALL(turtle, PenDown)
.Times(AtLeast(2));
#else
NiceMock<MockTurtle> turtle;
//StrictMock<MockTurtle> turtle;
//MockTurtle turtle;
#endif
//Painter painter(&turtle);
ON_CALL(turtle, Count()).WillByDefault(Return(1));
turtle.PenDown();
turtle.PenDown();
std::cout << "Count: " << turtle.Count() << std::endl;
//EXPECT_TRUE(turtle.PenDown());
}

int main(int argc, char **argv)
{
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}