我正在用gtest尝试https://github.com/eranpeer/FakeIt来编写样本单元测试用例。我正在使用clang在Mac上执行代码,这将引发以下链接器错误。
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);
}
在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));
}
没有这样的未定义引用。不知道,如果我在这里没有包含任何库。