googlemock - 如何处理"多重定义"问题?

googlemock - how do I deal with "multiple definition" problems?

本文关键字:定义 问题 处理 何处理 googlemock      更新时间:2023-10-16

在我的CMakeLists.txt文件中,我有:

include_directories("${algorithm}/include")
add_executable(
        test_runner
        test_runner.cpp
        test_file1.cpp
        test_file2.cpp
        test_file2.cpp
)
target_link_libraries(
        test_runner
        gtest_main)
gtest_discover_tests(test_runner)

不幸的是,这不起作用,因为这些文件都从"${algorithm}/include"导入algorithm.c

我不知道在这里做什么。我怎样才能有多个test_files,这些都依赖于algorithm.ctest_runner可以发现?

我是被迫将所有test_files放入一个文件中,还是为每个文件制作单独的可执行文件?那将是极其不幸的。

有没有其他选择?我期待涉及标题的东西?还是algorithms.c需要是我导入的库?

您可以将源文件的包含包装到具有适当包含保护的其他头文件中。像这样:

// File: algorithm_test.h
#ifndef ALGORITHM_TEST_H
#define ALGORITHM_TEST_H
#include <algorithm.c>
#endif /* ALGORITHM_TEST_H */

之后,您的测试(test_file1.cpptest_file2.cpp等(可能包含此标头,而不是直接包含源文件:

// File: test_file1.cpp
#include <algorithm_test.h>
... define test methods ...