GTest CMake主要定义 [英] GTest CMake multiple definition of main
问题描述
我正在尝试学习如何使用 gtest
在c ++中创建单元测试.我编写了一个简单的库进行测试,并在其中创建了阶乘函数
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
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
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
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
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();
}
我按照 github 页上的文档将gtest添加到了项目中
I added gtest to my project following the docs on the github page
test/CMakeLists.txt.in
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
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)
当我尝试运行测试时,出现错误,提示'main'的多个定义
.它还说main的第一个定义是在 test/main.cpp
中,但是在我将main函数添加到此文件之前,它说 main
首先是在 googletest-src/googletest/src/gtest_main.cc
,它是 gtest
库.对于这个项目,我可以从 src/main
中删除 main
,因为它没有任何作用,但是在实际应用程序中,这不是一个选择.我假设我必须进行一些CMake配置才能忽略gtest main或类似的东西,但是我不知道如何.谁能帮我吗?预先感谢!
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!
这是我的项目级CMakeLists
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)
推荐答案
您声明了两个主要函数,我要做的是:
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
对于测试编译,我声明一个TESTING宏,然后确保该编译器仅看到一个主要功能.对于编译应用程序,它看不到测试主程序.
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.
这篇关于GTest CMake主要定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!