7

据我了解,我可以简单地catch.hpp放入我的项目目录,将其添加到项目中,然后开始编写测试。一旦我尝试使用本教程中概述的一些宏,我就会遇到几个,或者准确地说是 11 个链接器错误。

#include "catch.hpp"

unsigned int Factorial( unsigned int number ) {
    return number <= 1 ? number : Factorial(number-1)*number;
}

TEST_CASE( "Factorials are computed", "[factorial]" ) {
    REQUIRE( Factorial(1) == 1 );
    REQUIRE( Factorial(2) == 2 );
    REQUIRE( Factorial(3) == 6 );
    REQUIRE( Factorial(10) == 3628800 );
}

我可以从第一个示例中创建一个文件并使用 g++ 编译它,但是当我将相同的代码粘贴到我的 VS2010 项目中然后尝试运行它时,我遇到了链接器错误。不幸的是,此时我对 C++ 还不够熟悉,无法自己解码错误,在查看了此处的单个文件后,我无法辨别问题所在。

Error   1   error LNK2019: unresolved external symbol "public: __thiscall Catch::AutoReg::AutoReg(void (__cdecl*)(void),struct Catch::SourceLineInfo const &,struct Catch::NameAndDesc const &)" (??0AutoReg@Catch@@QAE@P6AXXZABUSourceLineInfo@1@ABUNameAndDesc@1@@Z) referenced in function "void __cdecl `anonymous namespace'::`dynamic initializer for 'autoRegistrar27''(void)" (??__EautoRegistrar27@?A0x3911e331@@YAXXZ)    C:\Users\Mark\documents\visual studio 2010\Projects\FirstCPPApplication\FirstCPPApplication\FirstCPPApplication.obj 1   1   FirstCPPApplication
Error   2   error LNK2019: unresolved external symbol "public: __thiscall Catch::ExpressionResultBuilder::ExpressionResultBuilder(enum Catch::ResultWas::OfType)" (??0ExpressionResultBuilder@Catch@@QAE@W4OfType@ResultWas@1@@Z) referenced in function __catch$?____C_A_T_C_H____T_E_S_T____27@@YAXXZ$0   C:\Users\Mark\documents\visual studio 2010\Projects\FirstCPPApplication\FirstCPPApplication\FirstCPPApplication.obj 1   1   FirstCPPApplication
Error   3   error LNK2019: unresolved external symbol "class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __cdecl Catch::translateActiveException(void)" (?translateActiveException@Catch@@YA?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@XZ) referenced in function __catch$?____C_A_T_C_H____T_E_S_T____27@@YAXXZ$0   C:\Users\Mark\documents\visual studio 2010\Projects\FirstCPPApplication\FirstCPPApplication\FirstCPPApplication.obj 1   1   FirstCPPApplication
Error   4   error LNK2019: unresolved external symbol "public: class Catch::ExpressionResultBuilder & __thiscall Catch::ExpressionResultBuilder::endExpression(enum Catch::ResultDisposition::Flags)" (?endExpression@ExpressionResultBuilder@Catch@@QAEAAV12@W4Flags@ResultDisposition@2@@Z) referenced in function "void __cdecl ____C_A_T_C_H____T_E_S_T____27(void)" (?____C_A_T_C_H____T_E_S_T____27@@YAXXZ)   C:\Users\Mark\documents\visual studio 2010\Projects\FirstCPPApplication\FirstCPPApplication\FirstCPPApplication.obj 1   1   FirstCPPApplication
Error   5   error LNK2019: unresolved external symbol "public: __thiscall Catch::AssertionInfo::AssertionInfo(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,struct Catch::SourceLineInfo const &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,enum Catch::ResultDisposition::Flags)" (??0AssertionInfo@Catch@@QAE@ABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@ABUSourceLineInfo@1@0W4Flags@ResultDisposition@1@@Z) referenced in function "void __cdecl ____C_A_T_C_H____T_E_S_T____27(void)" (?____C_A_T_C_H____T_E_S_T____27@@YAXXZ) C:\Users\Mark\documents\visual studio 2010\Projects\FirstCPPApplication\FirstCPPApplication\FirstCPPApplication.obj 1   1   FirstCPPApplication
Error   6   error LNK2019: unresolved external symbol "struct Catch::IContext & __cdecl Catch::getCurrentContext(void)" (?getCurrentContext@Catch@@YAAAUIContext@1@XZ) referenced in function "struct Catch::IResultCapture & __cdecl Catch::getResultCapture(void)" (?getResultCapture@Catch@@YAAAUIResultCapture@1@XZ)    C:\Users\Mark\documents\visual studio 2010\Projects\FirstCPPApplication\FirstCPPApplication\FirstCPPApplication.obj 1   1   FirstCPPApplication
Error   7   error LNK2019: unresolved external symbol "public: class Catch::ExpressionResultBuilder & __thiscall Catch::ExpressionResultBuilder::setOp(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (?setOp@ExpressionResultBuilder@Catch@@QAEAAV12@ABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) referenced in function "private: class Catch::ExpressionResultBuilder & __thiscall Catch::ExpressionLhs<int const &>::captureExpression<0,int>(int const &)" (??$captureExpression@$0A@H@?$ExpressionLhs@ABH@Catch@@AAEAAVExpressionResultBuilder@1@ABH@Z) C:\Users\Mark\documents\visual studio 2010\Projects\FirstCPPApplication\FirstCPPApplication\FirstCPPApplication.obj 1   1   FirstCPPApplication
Error   8   error LNK2019: unresolved external symbol "public: class Catch::ExpressionResultBuilder & __thiscall Catch::ExpressionResultBuilder::setRhs(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (?setRhs@ExpressionResultBuilder@Catch@@QAEAAV12@ABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) referenced in function "private: class Catch::ExpressionResultBuilder & __thiscall Catch::ExpressionLhs<int const &>::captureExpression<0,int>(int const &)" (??$captureExpression@$0A@H@?$ExpressionLhs@ABH@Catch@@AAEAAVExpressionResultBuilder@1@ABH@Z)   C:\Users\Mark\documents\visual studio 2010\Projects\FirstCPPApplication\FirstCPPApplication\FirstCPPApplication.obj 1   1   FirstCPPApplication
Error   9   error LNK2019: unresolved external symbol "public: class Catch::ExpressionResultBuilder & __thiscall Catch::ExpressionResultBuilder::setLhs(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (?setLhs@ExpressionResultBuilder@Catch@@QAEAAV12@ABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) referenced in function "private: class Catch::ExpressionResultBuilder & __thiscall Catch::ExpressionLhs<int const &>::captureExpression<0,int>(int const &)" (??$captureExpression@$0A@H@?$ExpressionLhs@ABH@Catch@@AAEAAVExpressionResultBuilder@1@ABH@Z)   C:\Users\Mark\documents\visual studio 2010\Projects\FirstCPPApplication\FirstCPPApplication\FirstCPPApplication.obj 1   1   FirstCPPApplication
Error   10  error LNK2019: unresolved external symbol "public: class Catch::ExpressionResultBuilder & __thiscall Catch::ExpressionResultBuilder::setResultType(bool)" (?setResultType@ExpressionResultBuilder@Catch@@QAEAAV12@_N@Z) referenced in function "private: class Catch::ExpressionResultBuilder & __thiscall Catch::ExpressionLhs<int const &>::captureExpression<0,int>(int const &)" (??$captureExpression@$0A@H@?$ExpressionLhs@ABH@Catch@@AAEAAVExpressionResultBuilder@1@ABH@Z)  C:\Users\Mark\documents\visual studio 2010\Projects\FirstCPPApplication\FirstCPPApplication\FirstCPPApplication.obj 1   1   FirstCPPApplication
Error   11  error LNK2019: unresolved external symbol "public: __thiscall Catch::AutoReg::~AutoReg(void)" (??1AutoReg@Catch@@QAE@XZ) referenced in function "void __cdecl `anonymous namespace'::`dynamic atexit destructor for 'autoRegistrar27''(void)" (??__FautoRegistrar27@?A0x3911e331@@YAXXZ)    C:\Users\Mark\documents\visual studio 2010\Projects\FirstCPPApplication\FirstCPPApplication\FirstCPPApplication.obj 1   1   FirstCPPApplication
Error   12  error LNK1120: 11 unresolved externals  C:\Users\Mark\documents\visual studio 2010\Projects\FirstCPPApplication\Debug\FirstCPPApplication.exe   FirstCPPApplication
4

2 回答 2

7

长话短说:将您的单个项目解决方案拆分为 3 个,使用.libold exe,将项目添加到适当的路径,更新链接器,一切顺利。

AE Drew 的回答帮助解决了我的问题,但我想为问题的标题提供更完整的解决方案,以防有人来自谷歌,特别是如果他们正在从初学者的角度寻找简单的细分。

如果您的解决方案中只有一个.exe为您构建的项目,CATCH 将不起作用:您的解决方案需要 3 个项目(据我所知)才能测试您的代码。

  • Source_project, 主代码库项目(您的应用程序的源代码所在的位置)

  • Test_project, 测试套件项目(其中REQUIRES测试出现在上述问题中)

  • Main_project, 您实际调用和运行源代码库中的代码的项目。

为了将您的原始单项目解决方案转换为您可以在两个测试中使用并正确运行的东西,您需要构建一个.lib或一个.dll以便其他项目(包括其他人的项目)可以使用相同的代码。改变这个答案,只需要

在解决方案资源管理器中右键单击您的项目,然后选择 > 属性...。在“配置属性”->“常规”下,有一个名为“配置类型”的 > 选项。如果将其更改为 [Static Library > (.lib)],您的项目将.lib在构建时生成 [ ]。

然后在第三个项目中main_project,你有一个 Win32 控制台应用程序的准系统设置,它只包含一个看起来像这样的文件:

#include "stdafx.h"
#include "HEADER_FROM_FIRST_PROJECT.h"

int _tmain(int argc, _TCHAR* argv[])
{
   METHOD_FROM_FIRST_PROJECT();
   return 0;
}

您从 导入头文件的位置Source_project,我们创建的第一个项目(以前是一个exe,但现在正在构建到一个lib)。但这还不会运行,您需要将Source_project' 目录添加到此项目的其他目录中,方法是右键单击解决方案资源管理器中的项目,在 Properties > Configuration Properties > C/C++ > General > Additional Include Directories 下,添加source_project的文件夹添加到列表中。这将允许这些#include行自动在该目录中搜索适当的文件,从而使您不必编写../source_project/HEADER_FROM_FIRST_PROJECT.h.

最后,最后一步是确保链接器能够真正找到您尝试包含的对象。而且由于我们正在构建一个 .lib,我们可以很简单地做到这一点。在与之前相同的设置对话框中,Projects Properties 对话框,我们将导航到 Configuration Properties > Linker > Input > Additional Dependencies 并添加 source_project.lib文件的相对路径,看起来像../Debug/source_project.lib.

请注意,如果.dllexe项目中有任何 s,则需要将其移至新的main_project.

另请注意,这对我来说都是全新的,一些条款和信息肯定是不正确的。

于 2013-10-06T05:22:34.960 回答
6

只需浏览您提供的链接,我认为您可能需要在文件顶部添加这一行。

#define CATCH_CONFIG_MAIN  // This tells Catch to provide a main() - only do this in one cpp file

看起来您尝试链接的函数在#if defined CATCH_CONFIG_MAIN .... #endif预处理器指令中。

于 2013-08-02T05:29:22.040 回答