问题
I am trying to learn how to use gtest
for creating unit testing in c++. I wrote a simple library to test, where I created a factorial function
src/main.cpp
#include <iostream>
int factorial(int n) {
if (n == 1)
return 1;
return n * factorial(n - 1);
}
// Default main function
int main() {
std::cout << "Hello, World!" << std::endl;
return 0;
}
src/main.h
#ifndef GTEST_LEARN_MAIN_H
#define GTEST_LEARN_MAIN_H
int factorial(int n);
#endif //GTEST_LEARN_MAIN_H
src/CMakeLists.txt
add_executable(gtest_learn main.cpp main.h)
add_library(factorial_lib STATIC main.cpp main.h)
Then I created a unit test where I am testing the factorial function
test/main.cpp
#include "main.h"
#include "gtest/gtest.h"
TEST(MyTestSuite,MyTest){
EXPECT_EQ(factorial(4),24);
}
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
I added gtest to my project following the docs on the github page
test/CMakeLists.txt.in
cmake_minimum_required(VERSION 2.8.2)
project(googletest-download NONE)
include(ExternalProject)
ExternalProject_Add(googletest
GIT_REPOSITORY https://github.com/google/googletest.git
GIT_TAG master
SOURCE_DIR "${CMAKE_CURRENT_BINARY_DIR}/googletest-src"
BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/googletest-build"
CONFIGURE_COMMAND ""
BUILD_COMMAND ""
INSTALL_COMMAND ""
TEST_COMMAND ""
)
test/CMakeLists.txt
cmake_minimum_required(VERSION 3.15)
project(tests)
# Download and unpack googletest at configure time
configure_file(CMakeLists.txt.in googletest-download/CMakeLists.txt)
execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" .
RESULT_VARIABLE result
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download )
if(result)
message(FATAL_ERROR "CMake step for googletest failed: ${result}")
endif()
execute_process(COMMAND ${CMAKE_COMMAND} --build .
RESULT_VARIABLE result
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download )
if(result)
message(FATAL_ERROR "Build step for googletest failed: ${result}")
endif()
# Prevent overriding the parent project's compiler/linker
# settings on Windows
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
# Add googletest directly to our build. This defines
# the gtest and gtest_main targets.
add_subdirectory(${CMAKE_CURRENT_BINARY_DIR}/googletest-src
${CMAKE_CURRENT_BINARY_DIR}/googletest-build
EXCLUDE_FROM_ALL)
# The gtest/gtest_main targets carry header search path
# dependencies automatically when using CMake 2.8.11 or
# later. Otherwise we have to add them here ourselves.
if (CMAKE_VERSION VERSION_LESS 2.8.11)
include_directories("${gtest_SOURCE_DIR}/include")
endif()
add_executable(tests main.cpp)
target_link_libraries(tests gtest_main)
target_link_libraries(tests factorial_lib)
add_test(NAME example_test COMMAND tests)
When I am trying to run my test, I get an error saying multiple definition of 'main'
. It also says the first definition of main was in test/main.cpp
, but before I added the main function in this file, it said that main
was first declared in googletest-src/googletest/src/gtest_main.cc
, which is the gtest
library. For this project I could remove the main
from src/main
since it serves no purpose, but in a real application that wouldn't be an option. I assume I have to make some CMake config to ignore the gtest main or something like that, but I don't know how. Can anyone help me out? Thanks in advance!
Here is my project-level CMakeLists
cmake_minimum_required(VERSION 3.15)
project(gtest_learn)
set(CMAKE_CXX_STANDARD 14)
include_directories(src)
add_subdirectory(src)
add_subdirectory(test)
回答1:
You have two main functions declared, what I do is:
#ifndef TESTING
// Default main function
int main() {
std::cout << "Hello, World!" << std::endl;
return 0;
}
#endif
#ifdef TESTING
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
#endif
For test compilation I declare a TESTING macro, which then ensures, that the compiler sees only one main function. For compiling the application, it does not see the test main.
来源:https://stackoverflow.com/questions/61609265/gtest-cmake-multiple-definition-of-main