0

I've started out learning how to use Catch2 for testing my C++ code and am trying to set up a simple test.

My folder structure consists of three files all in the same folder:

catch.cpp //this is the catch_amalgamated.cpp file from GitHub
catch.hpp //this is the catch_amalgamated.hpp file from GitHub
test.cpp  //this is my test file

All I have written in test.cpp is:

#define CATCH_CONFIG_MAIN
#include "catch.hpp"

When I try to compile test.cpp I get the following error which I think indicates that there is no main() function found(?):

C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/lib/../lib/libmingw32.a(lib64_libmingw32_a-crt0_c.o):crt0_c.c:(.text.startup+0x2e): undefined reference to `WinMain'
collect2.exe: error: ld returned 1 exit status

When I add a nominal main() function, the file compiles successfully, but as I understand it #define CATCH_CONFIG_MAIN is supposed to create the main() function for you so something clearly isn't working.

Can anyone shed any light on this?

askman
  • 447
  • 4
  • 14

2 Answers2

2

The error was occurring because the default branch that google takes you to is the development branch of catch2, so I was using version 3 files and following version 2 documentation (as it has not been updated yet). Once I downloaded the v.2 file everything started working fine.

askman
  • 447
  • 4
  • 14
0

If you want to keep using catch2 v3, you'd have to link against Catch2WithMain instead of Catch2

so something like that:

target_link_libraries(unit_tests PRIVATE
    Catch2::Catch2WithMain
)

Source https://github.com/catchorg/Catch2/blob/devel/docs/migrate-v2-to-v3.md

Mehdi
  • 1,370
  • 3
  • 15
  • 26