Francesco Borzi
Francesco Borzi

Reputation: 61774

Undefined symbols for architecture x86_64 when writing unit tests with gtest and clang 12

I'm trying to add unit tests for some functions defined in this file:

My test file looks like this:

#include "gtest/gtest.h"
#include "Formulas.h"
#include "SharedDefines.h"

using namespace acore::Honor;
using namespace acore::XP;

TEST(FormulasTest, hk_honor_at_level)
{
    EXPECT_EQ(hk_honor_at_level(80), 124);
    // some more checks here...
}

TEST(FormulasTest, GetGrayLevel)
{
    EXPECT_EQ(GetGrayLevel(0), 0);
    // some more checks here...
}

TEST(FormulasTest, GetColorCode)
{
    EXPECT_EQ(GetColorCode(60, 80), XP_RED);
    // some more checks here...
}

TEST(FormulasTest, GetZeroDifference)
{
    EXPECT_EQ(GetZeroDifference(1), 5);
    // some more checks here...
}

TEST(FormulasTest, BaseGain)
{
    // PROBLEM HERE
    EXPECT_EQ(BaseGain(60, 1, CONTENT_1_60), 0);
}

the problem arises as soon as I call the function BaseGain defined inside Formulas.h:

Undefined symbols for architecture x86_64:
  "_LoginDatabase", referenced from:
      Log::outDB(LogTypes, char const*) in libcommon.a(Log.cpp.o)
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[3]: *** [src/test/unit_tests] Error 1
make[2]: *** [src/test/CMakeFiles/unit_tests.dir/all] Error 2
make[1]: *** [src/test/CMakeFiles/unit_tests.dir/rule] Error 2
make: *** [unit_tests] Error 2

In particular, the problem is gone if I remove the call to sLog->outError:

 sLog->outError("BaseGain: Unsupported content level %u", content);

from BaseGain.

However, this issue happens only in the unit tests, the rest of the project source code compiles just fine.

To compile the project including unit tests, pass the -DBUILD_TESTING=1 parameter to the cmake command.

EDIT: I've already tried adding common to the target_link_libraries of src/test/CMakeLists.txt but this hasn't solved the issue for me.

Upvotes: 0

Views: 443

Answers (1)

Stefano Borzì
Stefano Borzì

Reputation: 2438

You can solve by adding:

LoginDatabaseWorkerPool LoginDatabase;

to your FormulaTest.cpp.

Upvotes: 0

Related Questions