Wpis z mikrobloga

#programowanie #cpp #pytaniedoeksperta #programista15k

Chce sprawdzić jak działa Catch2 dla C++ w Debian

przykład:

#include <catch2/catch_test_macros.hpp>

static int Factorial( int number ) {
return number <= 1 ? number : Factorial( number - 1 ) * number; // fail
// return number <= 1 ? 1 : Factorial( number - 1 ) * number; // pass
}

TEST_CASE( "Factorial of 0 is 1 (fail)", "[single-file]" ) {
REQUIRE( Factorial(0) == 1 );
}

TEST_CASE( "Factorials of 1 and higher are computed (pass)", "[single-file]" ) {
REQUIRE( Factorial(1) == 1 );
REQUIRE( Factorial(2) == 2 );
REQUIRE( Factorial(3) == 6 );
REQUIRE( Factorial(10) == 3628800 );
}

catch2 jest w /usr/include zainstalowane z pakietu deb
Kompiluje:

g++ -std=c++14 -Wall -I /usr/include/catch2 -o 010-TestCase 010-TestCase.cpp && 010-TestCase --success
i poszło...

/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/8/../../../x86_64-linux-gnu/Scrt1.o: in function start':
(.text+0x20): undefined reference to main'
/usr/bin/ld: /tmp/ccFU1LF2.o: in function
CATCH2
INTERNALTEST0()':
010-TestCase.cpp:(.text+0x9a): undefined reference to Catch::AssertionHandler::AssertionHandler(Catch::StringRef, Catch::SourceLineInfo const&, Catch::StringRef, Catch::ResultDisposition::Flags)'
itd.

Co ja źle robię? Co źle rozumiem?
  • 7
  • Odpowiedz
@defoxe: -I /usr/include/catch2 tym zakomunikowałeś gdzie są same headery, ale nigdzie nie dałeś informacji skąd ma sobie dolinkować "mięso" do finalnej binarki. takie coś by wystarczyło jakbyś miał bibliotekę header-only. imo ten -I /usr/include/catch2/ jest niepotrzebny w ogóle, powinien tam z automatu zerkać
  • Odpowiedz
TEST_CASE( "Factorials of 1 and higher are computed (pass)", "[single-file]" ) {

REQUIRE( Factorial(1) == 1 );

REQUIRE( Factorial(2) == 2 );

REQUIRE( Factorial(3) == 6 );

REQUIRE( Factorial(10) == 3628800 );

}
  • Odpowiedz