【问题标题】:GTest CMake multiple definition of mainGTest CMake 多重定义 main
【发布时间】:2020-08-19 21:14:19
【问题描述】:

我正在尝试学习如何使用gtest 在 C++ 中创建单元测试。我写了一个简单的库来测试,在那里我创建了一个阶乘函数

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)

然后我创建了一个单元测试来测试阶乘函数

测试/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 添加到我的项目中

测试/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      ""
)

测试/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)

当我尝试运行我的测试时,我收到一条错误消息 multiple definition of 'main'。它还说main的第一个定义是在test/main.cpp中,但是在我在这个文件中添加main函数之前,它说main首先在googletest-src/googletest/src/gtest_main.cc中声明,即gtest库。对于这个项目,我可以从src/main 中删除main,因为它没有任何用途,但在实际应用程序中这不是一个选项。我假设我必须进行一些 CMake 配置以忽略 gtest main 或类似的东西,但我不知道如何。谁能帮我吗?提前致谢!

这是我的项目级 CMakeLists

cmake_minimum_required(VERSION 3.15)
project(gtest_learn)

set(CMAKE_CXX_STANDARD 14)


include_directories(src)

add_subdirectory(src)
add_subdirectory(test)

【问题讨论】:

  • 一个可执行文件中不能有两个 main() 函数。如果要运行单元测试,请删除带有"Hello World" 的那个。
  • 所以我不能测试与程序主函数在同一个文件中的函数,就像你可以用其他语言做的那样。好的,谢谢!
  • 还有一个提示:现在你正在编译factorial函数两次:在gtest_learnfactorial_lib。库的目的是让代码只编译一次,因此只在factorial_lib 编译它,然后在需要的地方链接它(测试可执行文件和项目可执行文件)
  • “所以我不能测试与程序主函数在同一个文件中的函数”你可以,但你仍然不能在一个程序中有两个 main 函数,并且在单元测试程序中,main 函数需要运行测试。
  • @R2RT 我编译两次是什么意思?

标签: c++ cmake googletest


【解决方案1】:

你声明了两个主要函数,我要做的是:

#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 宏,然后确保编译器只看到一个主函数。对于编译应用程序,它没有看到 test main。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-04
    • 1970-01-01
    • 2017-06-24
    • 2012-01-27
    • 2017-03-02
    • 2016-02-09
    相关资源
    最近更新 更多