0

I am trying out https://github.com/eranpeer/FakeIt with gtest to write sample unit test cases. I am executing the code on my Mac with clang, which throws the following linker error.

bash-3.2$ make
g++ -std=c++11 -g -L/usr/local/lib -lgtest -lgtest_main -lpthread -I./include -I../../src -I/usr/local/include/gtest/include -I../../FakeIt/include -I../../FakeIt/config/gtest -I../src/sdk-x.x.x/ -I../src/bcmplus/ -o testAll ./src/main.cpp ./src/BcmRegMock.cpp ./src/BcmPlusTest.cpp
Undefined symbols for architecture x86_64:
  "_mock_bcm_reg", referenced from:
      _bcmRegRead in BcmRegMock-585f58.o
      _bcmRegWrite in BcmRegMock-585f58.o
      BcmPlusTest_Clear_Registers_Test::TestBody() in BcmPlusTest-3d3b5f.o
      BcmPlusTest::SetUp() in BcmPlusTest-3d3b5f.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: *** [testAll] Error 1

BcmRegMock.cpp:

#include <BcmRegMock.h>

int bcmRegRead(int addr, int * value) {
    return mock_bcm_reg.get().bcmRegRead(addr, value);
}

int bcmRegWrite(int addr, int value) {
    return mock_bcm_reg.get().bcmRegWrite(addr, value);
}

In BcmRegMock.h:

extern "C" {
    #include "bcm_reg.h" // Production Code (in C)
}
#include <gtest/gtest.h>
#include "fakeit.hpp"
struct _bcm_reg {
    virtual ~_bcm_reg() {};
    virtual int bcmRegRead(int addr, int * value) = 0;
    virtual int bcmRegWrite(int addr, int value) = 0;
};
extern fakeit::Mock<_bcm_reg> mock_bcm_reg;

BrcmSdkFakeMethods.h :

Fake(Method(mock_bcm_reg, bcmRegRead));
Fake(Method(mock_bcm_reg, bcmRegWrite));

BcmPlusTest.cpp :

#include <gtest/gtest.h>
#include <fakeit.hpp>
#include "BcmRegMock.h"


using namespace fakeit;

class BcmPlusTest : public ::testing::Test {
 protected:
  virtual void SetUp() {
      #include "BrcmSdkFakeMethods.h"
  }

  virtual void TearDown() {
    // Code here will be called immediately after each test
    // (right before the destructor).
  }
};

TEST_F(BcmPlusTest, Clear_Registers) {

    // Verify bcmRegWrite was invoked at least once. 
    Verify(Method(mock_bcm_reg, bcmRegWrite));

}

There are no undefined references as such. Not sure, if I am missing inclusion of any libs here.

user207421
  • 305,947
  • 44
  • 307
  • 483
Shibir Basak
  • 101
  • 7
  • Order matters! In this case, order of libraries and object/source files listed when building. If source- or object-file A depends on library B, then A must come *before* B on the command line when linking. That's why it's common to put all linker libraries *last* on the command line, where it's definitely after any source or object files that uses the library. – Some programmer dude Apr 23 '19 at 09:53
  • Not directly related but: you should not use -lpthread. Please use -pthread instead with gcc. – Klaus Apr 23 '19 at 09:53

0 Answers0