CATCH Testing Test Kit with Visual Studio 2010

As far as I understand, I can just put catch.hpp in my project directory, add it to the project and start writing tests. I use several, or 11, to be exact, linker errors as soon as I try to use some of the macros described in the tutorial .

 #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 ); } 

I can create one file from the first example and compile it with g ++, but when I just paste the same code into the VS2010 project and then try to run it, I encounter linker errors. Unfortunately, I am not familiar enough with C ++ at this point to decode the errors myself, and after viewing a single file here , I was unable to recognize the problem.

 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 
+6
source share
2 answers

Just by looking at the links you provide, I think you might need to add this line at the top of your file.

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

It looks like the functions you are trying to install are in the preprocessor directives #if defined CATCH_CONFIG_MAIN .... #endif .

+6
source

long story: divide one design solution by 3, use the .lib old exe , add the projects to the appropriate paths, update the linker, and you are good to go.

AE Drew's answer helped solve my problem, but I want to put a more complete solution to the question if anyone from Google, especially if they are looking for a simple breakdown from perspective.

If you had only one project in your solution that built .exe for you, CATCH will not work: your solution needs 3 projects (as far as I know) in order to be able to check your code.

  • Source_project , The main code project (in which the source code for your application is located)

  • Test_project , a test Test_project project (which displays REQUIRES tags as indicated in the above question)

  • Main_project , a project in which you really call and run code from source codebase.

To convert the original solution for one project to something that you can use in both tests and to run correctly, you will need to build .lib or a .dll so that other projects (including other people's projects) can use the same code. By changing this answer , all that is required is

right-click the project in Solution Explorer and select> Properties .... Under Configuration Properties โ†’ General Settings a> called Configuration Type. If you change it to [Static Library> (.lib)], the project will generate [ .lib ] when it is created.

And then in the third Main_project project, where you have the barebone setting from the Win32 Console application, which contains only a file that looks something like:

 #include "stdafx.h" #include "HEADER_FROM_FIRST_PROJECT.h" int _tmain(int argc, _TCHAR* argv[]) { METHOD_FROM_FIRST_PROJECT(); return 0; } 

where you import the header file from Source_project , the first project that we (the one that used to be exe but now builds on lib ). But which is not going to start yet, you need to add the Source_project directory to this additional directory projects by right-clicking the project in Solution Explorer under Properties> Configuration Properties> C / C ++> General> Additional Include Directories, add the Source_project folder to the list . This will allow the #include lines to automatically look for this directory for the corresponding files, which saves you from having to write ../source_project/HEADER_FROM_FIRST_PROJECT.h .

Finally, the last step is to make sure that the linker can actually find the objects you are trying to include. And since we are building .lib , we can do this quite simply. In the same settings dialog box as before this Project Properties dialog box, we will go to the settings Properties> Linker> Input> Additional Dependencies and add the relative path to source_project.lib , which will look something like ../Debug/source_project.lib .

Note that if you have .dll that are in the old exe project, this will need to be moved to the new Main_project .

Also note that this is all very new to me, and some of these terms and information are error related.

+6
source

Source: https://habr.com/ru/post/950868/


All Articles