반응형

 

STEP 1. libgtest-dev 다운로드

cd ~
mkdir bin; cd bin;
git clone https://github.com/google/googletest.git

cd googletest        # Main directory of the cloned repository.
mkdir build          # Create a directory to hold the build output.
cd build
cmake ..             # Generate native build scripts for GoogleTest.

 

The above command also includes GoogleMock by default. And so, if you want to build only GoogleTest, you should replace the last command with

 

cmake .. -DBUILD_GMOCK=OFF

 

If you are on a *nix system, you should now see a Makefile in the current directory. Just type make to build GoogleTest. And then you can simply install GoogleTest if you are a system administrator.

 

make

# (Optional) 이 부분은 할 필요 없습니다.
sudo make install    # Install in /usr/local/ by default

 

If you use Windows and have Visual Studio installed, a gtest.sln file and several .vcproj files will be created. You can then build them using Visual Studio.

 

(출처:  https://github.com/google/googletest/blob/main/googletest/README.md)

 

 

 

STEP 2-1. (실험) "/usr/bin/ld: cannot find -lgmock", "/usr/bin/ld: cannot find -lgmock_main" 에러 발생하게 하기 -  Makefile 작성

# Compiler and flags
CXX = g++
CXXFLAGS = -std=c++17 -Wall -Wextra -g -fprofile-arcs -ftest-coverage -I$(SRC_DIR) -I$(GTEST_INC_DIR) -I$(GMOCK_INC_DIR)
LDFLAGS = -lgmock -lgmock_main -lgtest -lgtest_main -pthread

# Directories
SRC_DIR = src
TEST_DIR = test
BUILD_DIR = build
REPORT_DIR = report


# Files
SRC_FILES = $(wildcard $(SRC_DIR)/*.cpp)
TEST_FILES = $(wildcard $(TEST_DIR)/*.cpp)
OBJECTS = $(SRC_FILES:$(SRC_DIR)/%.cpp=$(BUILD_DIR)/%.o)
TEST_OBJECTS = $(TEST_FILES:$(TEST_DIR)/%.cpp=$(BUILD_DIR)/%.o)

GTEST_INC_DIR = /home/kai/bin/googletest/googletest/include
GMOCK_INC_DIR = /home/kai/bin/googletest/googlemock/include

# Google Test location (adjust based on your installation)
GTEST_DIR = /home/kai/bin/googletest/build/lib
#/usr/src/gtest
GTEST_LIB = -lgtest -lgtest_main -pthread

# Output
TEST_EXEC = $(BUILD_DIR)/test_executable

# Default target
all: test coverage

# Compile source files
$(BUILD_DIR)/%.o: $(SRC_DIR)/%.cpp
        mkdir -p $(BUILD_DIR)
        $(CXX) $(CXXFLAGS) -c $< -o $@

# Compile test files
$(BUILD_DIR)/%.o: $(TEST_DIR)/%.cpp
        mkdir -p $(BUILD_DIR)
        $(CXX) $(CXXFLAGS) -c $< -o $@

# Link test executable
$(TEST_EXEC): $(OBJECTS) $(TEST_OBJECTS)
        $(CXX) $(CXXFLAGS) $^ -o $@ $(LDFLAGS)

# Run tests
test: $(TEST_EXEC)
        ./$(TEST_EXEC)

# Generate coverage report
coverage: test
        mkdir -p $(REPORT_DIR)
        lcov --capture --directory $(BUILD_DIR) --output-file $(REPORT_DIR)/coverage.info
        genhtml $(REPORT_DIR)/coverage.info --output-directory $(REPORT_DIR)

# Clean up
clean:
        rm -rf $(BUILD_DIR) $(REPORT_DIR)

# Phony targets
.PHONY: all test coverage clean

 

 

출력화면

...
g++ -std=c++17 -Wall -Wextra -g -fprofile-arcs -ftest-coverage -Isrc -I/home/kai/bin/googletest/googletest/include -I/home/kai/bin/googletest/googlemock/include build/example.o build/main.o build/example_test.o -o build/test_executable -lgmock -lgmock_main -lgtest -lgtest_main -pthread
/usr/bin/ld: cannot find -lgmock
/usr/bin/ld: cannot find -lgmock_main
/usr/bin/ld: cannot find -lgtest
/usr/bin/ld: cannot find -lgtest_main
collect2: error: ld returned 1 exit status
make: *** [Makefile:47: build/test_executable] Error 1

 

 

 

 

STEP 2-2. (실험) "/usr/bin/ld: cannot find -lgmock", "/usr/bin/ld: cannot find -lgmock_main" 에러 발생하게 하기 -  Makefile 작성

 

-I$(GTEST_INC_DIR) -I$(GMOCK_INC_DIR) 를 빼고 빌드해보기

CXX = g++
CXXFLAGS = -std=c++17 -Wall -Wextra -g -fprofile-arcs -ftest-coverage -I$(SRC_DIR)
LDFLAGS = -lgmock -lgmock_main -lgtest -lgtest_main -pthread -L$(GTEST_DIR)
...
...

 

 

출력화면

mkdir -p build
g++ -std=c++17 -Wall -Wextra -g -fprofile-arcs -ftest-coverage -Isrc -c src/example.cpp -o build/example.o
mkdir -p build
g++ -std=c++17 -Wall -Wextra -g -fprofile-arcs -ftest-coverage -Isrc -c src/main.cpp -o build/main.o
mkdir -p build
g++ -std=c++17 -Wall -Wextra -g -fprofile-arcs -ftest-coverage -Isrc -c test/example_test.cpp -o build/example_test.o
test/example_test.cpp:2:10: fatal error: gtest/gtest.h: No such file or directory
    2 | #include <gtest/gtest.h>
      |          ^~~~~~~~~~~~~~~
compilation terminated.

 

 

 

STEP 3. (실험) STEP 2-1.에 발생한 에러 고치기 - Makefile 작성

Before:

...
CXXFLAGS = -std=c++17 -Wall -Wextra -g -fprofile-arcs -ftest-coverage -I$(SRC_DIR) -I$(GTEST_INC_DIR) -I$(GMOCK_INC_DIR)
LDFLAGS = -lgmock -lgmock_main -lgtest -lgtest_main -pthread

 

After:

..
CXXFLAGS = -std=c++17 -Wall -Wextra -g -fprofile-arcs -ftest-coverage -I$(SRC_DIR) -I$(GTEST_INC_DIR) -I$(GMOCK_INC_DIR)
LDFLAGS = -lgmock -lgmock_main -lgtest -lgtest_main -pthread -L$(GTEST_DIR)

 

 

출력화면

mkdir -p build
g++ -std=c++17 -Wall -Wextra -g -fprofile-arcs -ftest-coverage -Isrc -I/home/kai/bin/googletest/googletest/include -I/home/kai/bin/googletest/googlemock/include -c src/example.cpp -o build/example.o
mkdir -p build
g++ -std=c++17 -Wall -Wextra -g -fprofile-arcs -ftest-coverage -Isrc -I/home/kai/bin/googletest/googletest/include -I/home/kai/bin/googletest/googlemock/include -c src/main.cpp -o build/main.o
mkdir -p build
g++ -std=c++17 -Wall -Wextra -g -fprofile-arcs -ftest-coverage -Isrc -I/home/kai/bin/googletest/googletest/include -I/home/kai/bin/googletest/googlemock/include -c test/example_test.cpp -o build/example_test.o
g++ -std=c++17 -Wall -Wextra -g -fprofile-arcs -ftest-coverage -Isrc -I/home/kai/bin/googletest/googletest/include -I/home/kai/bin/googletest/googlemock/include build/example.o build/main.o build/example_test.o -o build/test_executable -lgmock -lgmock_main -lgtest -lgtest_main -pthread -L/home/kai/bin/googletest/build/lib
./build/test_executable
Value: 10
Add 5: 15
mkdir -p report
lcov --capture --directory build --output-file report/coverage.info
Capturing coverage data from build
Found gcov version: 9.4.0
Using intermediate gcov format
Scanning build for .gcda files ...
Found 3 data files in build
Processing example_test.gcda
Processing example.gcda
Processing main.gcda
Finished .info-file creation
genhtml report/coverage.info --output-directory report
Reading data file report/coverage.info
Found 18 entries.
Found common filename prefix "/home/kai/bin/googletest/googletest/include"
Writing .css and .png files.
Generating output.
Processing file /home/kai/98.Study/gcov-makefile-01/src/example.cpp
Processing file /home/kai/98.Study/gcov-makefile-01/src/main.cpp
Processing file /home/kai/98.Study/gcov-makefile-01/test/example_test.cpp
Processing file gtest/gtest-assertion-result.h
Processing file gtest/gtest.h
Processing file gtest/gtest-printers.h
Processing file gtest/internal/gtest-internal.h
Processing file gtest/internal/gtest-port.h
Processing file /usr/include/c++/9/tuple
Processing file /usr/include/c++/9/bits/basic_string.tcc
Processing file /usr/include/c++/9/bits/stl_iterator_base_types.h
Processing file /usr/include/c++/9/bits/stl_iterator_base_funcs.h
Processing file /usr/include/c++/9/bits/basic_string.h
Processing file /usr/include/c++/9/bits/char_traits.h
Processing file /usr/include/c++/9/bits/unique_ptr.h
Processing file /usr/include/c++/9/bits/move.h
Processing file /usr/include/c++/9/ext/type_traits.h
Processing file /usr/include/c++/9/ext/new_allocator.h
Writing directory view page.
Overall coverage rate:
  lines......: 47.3% (80 of 169 lines)
  functions..: 35.8% (34 of 95 functions)

 

 

 

STEP 4. (실험) STEP 3에서 $(LDFLAGS) 빼고 빌드해보기

 

Before:

# Compiler and flags
CXX = g++
CXXFLAGS = -std=c++17 -Wall -Wextra -g -fprofile-arcs -ftest-coverage -I$(SRC_DIR) -I$(GTEST_INC_DIR) -I$(GMOCK_INC_DIR)
LDFLAGS = -lgmock -lgmock_main -lgtest -lgtest_main -pthread -L$(GTEST_DIR)
...
# Link test executable
$(TEST_EXEC): $(OBJECTS) $(TEST_OBJECTS)
        $(CXX) $(CXXFLAGS) $^ -o $@ $(LDFLAGS)
...

 

 

After:

# Compiler and flags
CXX = g++
CXXFLAGS = -std=c++17 -Wall -Wextra -g -fprofile-arcs -ftest-coverage -I$(SRC_DIR) -I$(GTEST_INC_DIR) -I$(GMOCK_INC_DIR)
LDFLAGS = -lgmock -lgmock_main -lgtest -lgtest_main -pthread -L$(GTEST_DIR)
...
# Link test executable
$(TEST_EXEC): $(OBJECTS) $(TEST_OBJECTS)
        $(CXX) $(CXXFLAGS) $^ -o $@
...

 

 

 

출력화면

mkdir -p build
g++ -std=c++17 -Wall -Wextra -g -fprofile-arcs -ftest-coverage -Isrc -I/home/kai/bin/googletest/googletest/include -I/home/kai/bin/googletest/googlemock/include -c src/example.cpp -o build/example.o
mkdir -p build
g++ -std=c++17 -Wall -Wextra -g -fprofile-arcs -ftest-coverage -Isrc -I/home/kai/bin/googletest/googletest/include -I/home/kai/bin/googletest/googlemock/include -c src/main.cpp -o build/main.o
mkdir -p build
g++ -std=c++17 -Wall -Wextra -g -fprofile-arcs -ftest-coverage -Isrc -I/home/kai/bin/googletest/googletest/include -I/home/kai/bin/googletest/googlemock/include -c test/example_test.cpp -o build/example_test.o
g++ -std=c++17 -Wall -Wextra -g -fprofile-arcs -ftest-coverage -Isrc -I/home/kai/bin/googletest/googletest/include -I/home/kai/bin/googletest/googlemock/include build/example.o build/main.o build/example_test.o -o build/test_executable
/usr/bin/ld: build/example_test.o: in function `ExampleTest_ConstructorAndGetValue_Test::TestBody()':
/home/kai/98.Study/gcov-makefile-01/test/example_test.cpp:7: undefined reference to `testing::Message::Message()'
/usr/bin/ld: /home/kai/98.Study/gcov-makefile-01/test/example_test.cpp:7: undefined reference to `testing::internal::AssertHelper::AssertHelper(testing::TestPartResult::Type, char const*, int, char const*)'
/usr/bin/ld: /home/kai/98.Study/gcov-makefile-01/test/example_test.cpp:7: undefined reference to `testing::internal::AssertHelper::operator=(testing::Message const&) const'
/usr/bin/ld: /home/kai/98.Study/gcov-makefile-01/test/example_test.cpp:7: undefined reference to `testing::internal::AssertHelper::~AssertHelper()'
/usr/bin/ld: /home/kai/98.Study/gcov-makefile-01/test/example_test.cpp:7: undefined reference to `testing::internal::AssertHelper::~AssertHelper()'
/usr/bin/ld: build/example_test.o: in function `ExampleTest_AddMethod_Test::TestBody()':
/home/kai/98.Study/gcov-makefile-01/test/example_test.cpp:13: undefined reference to `testing::Message::Message()'
/usr/bin/ld: /home/kai/98.Study/gcov-makefile-01/test/example_test.cpp:13: undefined reference to `testing::internal::AssertHelper::AssertHelper(testing::TestPartResult::Type, char const*, int, char const*)'
/usr/bin/ld: /home/kai/98.Study/gcov-makefile-01/test/example_test.cpp:13: undefined reference to `testing::internal::AssertHelper::operator=(testing::Message const&) const'
/usr/bin/ld: /home/kai/98.Study/gcov-makefile-01/test/example_test.cpp:13: undefined reference to `testing::internal::AssertHelper::~AssertHelper()'
/usr/bin/ld: /home/kai/98.Study/gcov-makefile-01/test/example_test.cpp:14: undefined reference to `testing::Message::Message()'
/usr/bin/ld: /home/kai/98.Study/gcov-makefile-01/test/example_test.cpp:14: undefined reference to `testing::internal::AssertHelper::AssertHelper(testing::TestPartResult::Type, char const*, int, char const*)'
/usr/bin/ld: /home/kai/98.Study/gcov-makefile-01/test/example_test.cpp:14: undefined reference to `testing::internal::AssertHelper::operator=(testing::Message const&) const'
/usr/bin/ld: /home/kai/98.Study/gcov-makefile-01/test/example_test.cpp:14: undefined reference to `testing::internal::AssertHelper::~AssertHelper()'
/usr/bin/ld: /home/kai/98.Study/gcov-makefile-01/test/example_test.cpp:13: undefined reference to `testing::internal::AssertHelper::~AssertHelper()'
/usr/bin/ld: /home/kai/98.Study/gcov-makefile-01/test/example_test.cpp:14: undefined reference to `testing::internal::AssertHelper::~AssertHelper()'
/usr/bin/ld: build/example_test.o: in function `__static_initialization_and_destruction_0(int, int)':
/home/kai/98.Study/gcov-makefile-01/test/example_test.cpp:5: undefined reference to `testing::internal::GetTestTypeId()'
/usr/bin/ld: /home/kai/98.Study/gcov-makefile-01/test/example_test.cpp:5: undefined reference to `testing::internal::MakeAndRegisterTestInfo(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, char const*, char const*, char const*, testing::internal::CodeLocation, void const*, void (*)(), void (*)(), testing::internal::TestFactoryBase*)'
/usr/bin/ld: /home/kai/98.Study/gcov-makefile-01/test/example_test.cpp:11: undefined reference to `testing::internal::GetTestTypeId()'
/usr/bin/ld: /home/kai/98.Study/gcov-makefile-01/test/example_test.cpp:11: undefined reference to `testing::internal::MakeAndRegisterTestInfo(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, char const*, char const*, char const*, testing::internal::CodeLocation, void const*, void (*)(), void (*)(), testing::internal::TestFactoryBase*)'
/usr/bin/ld: build/example_test.o: in function `testing::internal::SuiteApiResolver<testing::Test>::GetSetUpCaseOrSuite(char const*, int)':
/home/kai/bin/googletest/googletest/include/gtest/internal/gtest-internal.h:513: undefined reference to `testing::internal::IsTrue(bool)'
/usr/bin/ld: /home/kai/bin/googletest/googletest/include/gtest/internal/gtest-internal.h:513: undefined reference to `testing::internal::GTestLog::GTestLog(testing::internal::GTestLogSeverity, char const*, int)'
/usr/bin/ld: /home/kai/bin/googletest/googletest/include/gtest/internal/gtest-internal.h:513: undefined reference to `testing::internal::GTestLog::~GTestLog()'
/usr/bin/ld: /home/kai/bin/googletest/googletest/include/gtest/internal/gtest-internal.h:513: undefined reference to `testing::internal::GTestLog::~GTestLog()'
/usr/bin/ld: build/example_test.o: in function `testing::internal::SuiteApiResolver<testing::Test>::GetTearDownCaseOrSuite(char const*, int)':
/home/kai/bin/googletest/googletest/include/gtest/internal/gtest-internal.h:534: undefined reference to `testing::internal::IsTrue(bool)'
/usr/bin/ld: /home/kai/bin/googletest/googletest/include/gtest/internal/gtest-internal.h:534: undefined reference to `testing::internal::GTestLog::GTestLog(testing::internal::GTestLogSeverity, char const*, int)'
/usr/bin/ld: /home/kai/bin/googletest/googletest/include/gtest/internal/gtest-internal.h:534: undefined reference to `testing::internal::GTestLog::~GTestLog()'
/usr/bin/ld: /home/kai/bin/googletest/googletest/include/gtest/internal/gtest-internal.h:534: undefined reference to `testing::internal::GTestLog::~GTestLog()'
/usr/bin/ld: build/example_test.o: in function `testing::AssertionResult testing::internal::CmpHelperEQ<int, int>(char const*, char const*, int const&, int const&)':
/home/kai/bin/googletest/googletest/include/gtest/gtest.h:1395: undefined reference to `testing::AssertionSuccess()'
/usr/bin/ld: build/example_test.o: in function `testing::AssertionResult testing::internal::CmpHelperEQFailure<int, int>(char const*, char const*, int const&, int const&)':
/home/kai/bin/googletest/googletest/include/gtest/gtest.h:1379: 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)'
/usr/bin/ld: build/example_test.o:(.data.rel.ro._ZTV26ExampleTest_AddMethod_Test[_ZTV26ExampleTest_AddMethod_Test]+0x20): undefined reference to `testing::Test::SetUp()'
/usr/bin/ld: build/example_test.o:(.data.rel.ro._ZTV26ExampleTest_AddMethod_Test[_ZTV26ExampleTest_AddMethod_Test]+0x28): undefined reference to `testing::Test::TearDown()'
/usr/bin/ld: build/example_test.o: in function `ExampleTest_AddMethod_Test::~ExampleTest_AddMethod_Test()':
/home/kai/98.Study/gcov-makefile-01/test/example_test.cpp:11: undefined reference to `testing::Test::~Test()'
/usr/bin/ld: build/example_test.o:(.data.rel.ro._ZTV39ExampleTest_ConstructorAndGetValue_Test[_ZTV39ExampleTest_ConstructorAndGetValue_Test]+0x20): undefined reference to `testing::Test::SetUp()'
/usr/bin/ld: build/example_test.o:(.data.rel.ro._ZTV39ExampleTest_ConstructorAndGetValue_Test[_ZTV39ExampleTest_ConstructorAndGetValue_Test]+0x28): undefined reference to `testing::Test::TearDown()'
/usr/bin/ld: build/example_test.o: in function `ExampleTest_ConstructorAndGetValue_Test::~ExampleTest_ConstructorAndGetValue_Test()':
/home/kai/98.Study/gcov-makefile-01/test/example_test.cpp:5: undefined reference to `testing::Test::~Test()'
/usr/bin/ld: build/example_test.o:(.data.rel.ro._ZTI26ExampleTest_AddMethod_Test[_ZTI26ExampleTest_AddMethod_Test]+0x10): undefined reference to `typeinfo for testing::Test'
/usr/bin/ld: build/example_test.o:(.data.rel.ro._ZTI39ExampleTest_ConstructorAndGetValue_Test[_ZTI39ExampleTest_ConstructorAndGetValue_Test]+0x10): undefined reference to `typeinfo for testing::Test'
/usr/bin/ld: build/example_test.o: in function `ExampleTest_AddMethod_Test::ExampleTest_AddMethod_Test()':
/home/kai/98.Study/gcov-makefile-01/test/example_test.cpp:11: undefined reference to `testing::Test::Test()'
/usr/bin/ld: build/example_test.o: in function `ExampleTest_ConstructorAndGetValue_Test::ExampleTest_ConstructorAndGetValue_Test()':
/home/kai/98.Study/gcov-makefile-01/test/example_test.cpp:5: undefined reference to `testing::Test::Test()'
collect2: error: ld returned 1 exit status
make: *** [Makefile:46: build/test_executable] Error 1
반응형