3

我正在尝试将Catch单元测试集成到我的项目中,但对于当前可用的来说它失败了

Catch v1.10.0
Generated: 2017-08-26 15:16:46.676990

例子:

test.cpp

#include "catch.hpp"

#define CATCH_CONFIG_MAIN


TEST_CASE("CATCH TEST"){
    REQUIRE(1 == 1);
}
g++ test.cpp -o test.exe -std=c++11

错误:

Undefined symbols for architecture x86_64:
  "Catch::ResultBuilder::endExpression(Catch::DecomposedExpression const&)", referenced from:
      Catch::BinaryExpression<int const&, (Catch::Internal::Operator)0, int const&>::endExpression() const in test-b77427.o
  "Catch::ResultBuilder::setResultType(bool)", referenced from:
      Catch::BinaryExpression<int const&, (Catch::Internal::Operator)0, int const&>::endExpression() const in test-b77427.o
  "Catch::ResultBuilder::useActiveException(Catch::ResultDisposition::Flags)", referenced from:
      ____C_A_T_C_H____T_E_S_T____0() in test-b77427.o
  "Catch::ResultBuilder::react()", referenced from:
      ____C_A_T_C_H____T_E_S_T____0() in test-b77427.o
  "Catch::ResultBuilder::ResultBuilder(char const*, Catch::SourceLineInfo const&, char const*, Catch::ResultDisposition::Flags, char const*)", referenced from:
      ____C_A_T_C_H____T_E_S_T____0() in test-b77427.o
  "Catch::ResultBuilder::~ResultBuilder()", referenced from:
      ____C_A_T_C_H____T_E_S_T____0() in test-b77427.o
  "Catch::SourceLineInfo::SourceLineInfo(char const*, unsigned long)", referenced from:
      ____C_A_T_C_H____T_E_S_T____0() in test-b77427.o
      ___cxx_global_var_init in test-b77427.o
  "Catch::isDebuggerActive()", referenced from:
      ____C_A_T_C_H____T_E_S_T____0() in test-b77427.o
  "Catch::AutoReg::AutoReg(void (*)(), Catch::SourceLineInfo const&, Catch::NameAndDesc const&)", referenced from:
      ___cxx_global_var_init in test-b77427.o
  "Catch::AutoReg::~AutoReg()", referenced from:
      ___cxx_global_var_init in test-b77427.o
  "Catch::toString(int)", referenced from:
      Catch::ExpressionLhs<int const&>::reconstructExpression(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&) const in test-b77427.o
      Catch::BinaryExpression<int const&, (Catch::Internal::Operator)0, int const&>::reconstructExpression(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&) const in test-b77427.o
  "Catch::ResultBuilder::shouldDebugBreak() const", referenced from:
      ____C_A_T_C_H____T_E_S_T____0() in test-b77427.o
  "_main", referenced from:
     implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit
g++ --version
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk/usr/include/c++/4.2.1
Apple LLVM version 8.1.0 (clang-802.0.42)
Target: x86_64-apple-darwin16.7.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
4

1 回答 1

9

就目前而言,您的程序没有main功能。如果你编译它,链接器会抱怨缺少主函数。由于您正在使用 catch 框架编写单元测试,因此您希望该框架main为您生成一个函数。

catch 框架查看是否CATCH_CONFIG_MAIN已定义,如果是,它会main为您生成一个函数,该函数运行您定义的所有测试。因为编译是一个线性过程,宏CATCH_CONFIG_MAIN必须在包含之前catch.hpp定义。否则,库将永远不会“看到”这个宏。

#define CATCH_CONFIG_MAIN
#include "catch.hpp"

TEST_CASE("CATCH TEST"){
    REQUIRE(1 == 1);
}

Live example

于 2017-08-28T05:29:54.250 回答