defoxe
#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?
  • Najlepsze
  • Wszystkie komentarze
groman43
@defoxe: Wygląda na to, że zapomniałeś o linkerze xD
MamCieNaHita
@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ć
c.....s
@defoxe: Twój program nie ma punktu startowego jakim jest funkcja main(). Możesz albo ją dodać, albo linkować catch_main do programu.
Saly
Saly
1
@defoxe: wygląda na to, że stara wersja v2 pozwalała na taki kod, v3 już nie jest header only i musisz zalinkować libkę https://github.com/catchorg/Catch2/blob/devel/docs/migrate-v2-to-v3.md#top .
horus666
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 );

}
defoxe
defoxe Autor
0
Buahahaha XD


@horus666: przykład z repozytorium ¯\(ツ)/¯