C++ googlemock won't compile in silly example - c++

I'm very new to C++, and I'm trying to use googlemock and googletest. I can't seem to get them to work even in simple examples so I'm sure there must be something simple I'm missing. I would really appreciate some help on this. I have a class Foo which has another class Bar as a dependency so I'm trying to mock this dependency out in tests. I'm sorry in advance, this seems like the simplest Dependency Injection example to me but it still spreads across 9 files! This is Bar:
// lib/bar.h
#ifndef BAR_H
#define BAR_H
class Bar {
public:
Bar(int baz);
virtual ~Bar() {};
int _baz;
};
#endif
With implementation:
// lib/bar.cpp
#include "bar.h"
Bar::Bar(int baz) : _baz(baz) {}
Here is the MockBar header:
// tests/mock_bar.h
#ifndef MOCK_BAR_H
#define MOCK_BAR_H
#include <gmock/gmock.h>
#include "../lib/bar.h"
class MockBar : public Bar {
public:
MockBar();
virtual ~MockBar() {};
};
#endif
And the implementation:
// tests/mock_bar.cpp
#include "mock_bar.h"
MockBar::MockBar() : Bar(0) {
}
So MockBar is just Bar with baz set to 0. Here is Foo:
// lib/foo.h
#ifndef FOO_H
#define FOO_H
#include "bar.h"
class Foo {
public:
Foo(Bar bar);
virtual ~Foo() {};
Bar _bar;
int getBaz();
};
#endif
// lib/foo.cpp
#include "foo.h"
Foo::Foo(Bar bar) : _bar(bar) {
}
int Foo::getBaz() {
return _bar._baz;
}
And here is my test:
// tests/test_foo.h
#ifndef TEST_FOO_H
#define TEST_FOO_H
#include <gtest/gtest.h>
#include "../lib/foo.h"
#include "mock_bar.h"
class FooTest : public ::testing::Test {
public:
FooTest();
Foo subject;
MockBar mock_bar;
virtual ~FooTest() {};
};
#endif
// tests/test_foo.cpp
#include "test_foo.h"
FooTest::FooTest() : mock_bar(), subject(mock_bar) {
}
TEST_F(FooTest, BazTest)
{
ASSERT_TRUE(subject.getBaz() == 0);
}
Finally, the main test function is:
// tests/main.cpp
#include <gtest/gtest.h>
#include <gmock/gmock.h>
#include "test_foo.h"
int main(int argc, char **argv) {
testing::InitGoogleMock(&argc, argv);
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
When I compile this all together with:
g++ tests/main.cpp tests/test_*.cpp tests/mock_*.cpp lib/*.cpp -o test
-lgtest -lpthread -std=c++11
I get the error:
Undefined symbols for architecture x86_64:
"testing::InitGoogleMock(int*, char**)", referenced from:
_main in main-0b53fe.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1
I would really appreciate some help, please let me know if I can be more clear!

You are using parts from gmock but only linking gtest. Thats why InitGoogleMock() is undefined.
Replacing -lgtest with -lgmock should make it.
The reason: gtest is testing framework, gmock the mocking framework.
If you link gmock, its includes also gtest but not the other way round.

Related

c++ Linker error undefined reference to vtable

I'm trying to write a small C++ inheritance program, that will contain virtual functions, headers, and 3 class, A, B:A, C:B, after compiling succeeds, the linker fails in the derived of a derived class, C.o, stating
relocation against `_ZTV26C' in read-only section `.text'
and
undefined reference to `vtable for C'
can anyone see what am i missing?
A.h
#ifndef A_H_
#define A_H_
#include <string>
#include <iostream>
using namespace std;
class A {
protected:
string name;
public:
A(string name);
virtual ~A();
virtual void justATest(){}
void justBecause();
virtual bool derivedTest(){}
};
#endif /* A_H_ */
A.cpp
#include "A.h"
A::A(string name) {this->name.assign(name);}
A::~A() {}
void A::justBecause(){}
B.h
#ifndef B_H_
#define B_H_
#include "A.h"
#include <string>
class B : public A{
public:
B(string name):A(name){}
virtual ~B(){}
bool derivedTest();
};
#endif /* B_H_ */
B.cpp
#include "B.h"
bool B::derivedTest()
{
return true;
}
C.h
#ifndef C_H_
#define C_H_
#include "B.h"
class C : public B{
public:
C(string name) :B(name){}
virtual ~C(){}
void justATest();
};
#endif /* C_H_ */
C.cpp
#include "C.h"
void C::justATest()
{
cout<< this->name;
}
main.cpp
#include "C.h"
int main(int argc, char* argv[]) {
C* c = new C("str");
return 0;
}
the exact make command, and the error message:
make all Building target: demo_proj Invoking: GCC C++ Linker g++ -o "ass5_demo" ./A.o ./B.o ./C.o ./main.o /usr/bin/ld: ./main.o: warning: relocation against _ZTV1C' in read-only section .text._ZN1CC2ENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE[_ZN1CC5ENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE]' /usr/bin/ld: ./main.o: in function C::C(std::__cxx11::basic_string<char, std::char_traits, std::allocator >)': /home/some_user/eclipse-workspace/demo_proj/../C.h:14: undefined reference to vtable for C' /usr/bin/ld: warning: creating DT_TEXTREL in a PIE
After many hours of debugging, and countless tries, with the same error message i found the answer.
This is C++ linker crooked way of telling me there is an unimplemented method in one of my classes.
adding to
c.h
bool derivedTest() override;
and to c.cpp
bool C::derivedTest(){return false;}
p.s another nice way of finding the problem is adding override after each message in the header file, the compiler is much clearer

c++ inheritance issues: undefined reference to 'vtable'

all! I am trying to create a very simple inheritance structure using C++ and header files but (of course) I am having some difficulties.
When I try to compile my main program, I get this error:
In function `Base::Base()':
undefined reference to 'vtable for Base'
In function `Derived::Derived()':
undefined reference to 'vtable for Derived'
All I want is to print is
printed in Derived
but I am having some extreme difficulties.
Here are my program files:
main.cpp
#include <iostream>
#include "Base.h"
#include "Derived.h"
using namespace std;
int main(void) {
Base *bp = new Derived;
bp->show();
return 0;
}
Base.cpp
#include <iostream>
#include "Base.h"
virtual void Base::show() {
cout << "printed in Base";
}
Base.h
#ifndef BASE_H
#define BASE_H
class Base {
public:
virtual void show();
};
#endif
Derived.cpp
#include <iostream>
#include "Derived.h"
using namespace std;
void Derived::show() override {
cout << "printed in Derived";
}
Derived.h
#ifndef DERIVED_H
#define DERIVED_H
class Derived: public Base {
public:
void show() override;
};
#endif
Thank you! And any help is very much appreciated!...extremely.
As pointed out in the comments, by calling g++ main.cpp you are only compiling main.cpp.
You need to compile all files, then link them together. If you do so, you will see that there are compilation issues in your other cpp files as also pointed out in the comments (virtual and override only belong in the header).
So you need to call the following to compile all files:
g++ main.cpp Base.cpp Derived.cpp -o myapp

Inheritance - Symbol undefined Objective-C++

quick question... this is just a misunderstanding on inheritance my end so this should be quick to fix (I'm using objective-c++). I'll take the question off if it's already been solved... I couldn't find it hence my post:
I have the following:
Base.h file
#ifndef BASE_H
#define BASE_H
class Base {
public:
// Virtual Constructor
Base() {};
}
#endif
Derived.h file
// Derived.h
#ifndef DERIVED_H
#define DERIVED_H
#include "Base.h"
class Derived : public Base {
public:
Derived();
}
#endif
Derived.cpp file
// Derived.cpp
#include "Derived.h"
Derived::Derived() {
// Do construction here!
}
Wrapper.mm file
// Wrapper.mm
#include "Derived.h"
- (id)init {
if (self = [super init]) {
Base *b = new Derived(); // Cannot compile?
}
}
I'm getting a undefined error for "Derived::Derived()" referenced from -[Wrapper init] in Wrapper.o; Symbol(s) not found for architecture arm64. How do I fix this? Thanks!
You're not linking the .o file generated when compiling Derived.cpp. Just add Derived.o to the end of your compiler arguments when you compile your Wrapper.mm file to create an executable and you should be good (though I hadn't even ever heard of obj-c++ before)

inheritance of child from another child classes

I have the following classes ( Enemy class is the parent class and Boss and SuperDuperBoss are children classes). My problem with SuperDuperBoss
#ifndef _ENEMY_H
#define _ENEMY_H
#include <iostream>
class Enemy
{
public:
void SelectAnimation();
inline void runAI()
{
std::cout << "RunAI() is running ...\n";
}
private:
int m_iHiPoints;
};
#endif
and this is the children classes
#ifndef BOSS_H
#define BOSS_H
#include "Enemy.h"
class Boss : public Enemy
{
public:
void runAI();
};
class SuperDuperBoss : public Boss
{
public:
void runAI();
};
#endif
This is the main.cpp
#include "Enemy.h"
#include "Boss.h"
int main()
{
Enemy enemy1;
Boss boss1;
SuperDuperBoss supBoss;
enemy1.runAI();
boss1.runAI();
supBoss.runAI(); // <--- Error: linking
return 0;
}
I have a linking error.
Undefined symbols for architecture x86_64:
"SuperDuperBoss::runAI()", referenced from:
_main in main.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: *** [test] Error 1
Only declare runAI in the derived classes if you want to override the parent's definition. So, assuming you want to use the definition from Enemy, your derived classes should look like this:
class Boss : public Enemy
{
public:
//other bossy stuff
};
class SuperDuperBoss : public Boss
{
public:
//other superduperbossy stuff
};

Duplicate symbol linker error with LLVM compiler flag visibility=hidden

I have a problem with a static library build with Xcode 4.6.2 using the LLVM Clang Compiler. The problem occurs only if I use the C/C++ Compiler flag visibility=hidden. The Linker reports me a duplicate symbol error. I will explain the setup in an easy scenario and I hope someone can explain why this happen.
Imagine I have two classes ClassA and ClassB which I compile into a static library myLib. As said above, I set the Compiler flag -fvisibility=hidden.
Then I have a project which creates an executable from a main.cpp which uses the myLib.
Here are the classes:
ClassA.h
#pragma once
#include <boost/exception/all.hpp>
struct my_error : virtual std::exception, virtual boost::exception {};
class ClassA
{
public:
explicit ClassA() {};
virtual ~ClassA() {};
virtual void doSomething();
};
ClassA.cpp
#include "ClassA.h"
void ClassA::doSomething()
{
BOOST_THROW_EXCEPTION( my_error() << boost::errinfo_api_function("doSomething") );
}
ClassB.h
#pragma once
#include "ClassA.h"
class ClassB
{
public:
explicit ClassB() {};
virtual ~ClassB() {};
virtual void doSomething();
};
ClassB.cpp
#include "ClassB.h"
void ClassB::doSomething()
{
BOOST_THROW_EXCEPTION( my_error() << boost::errinfo_api_function("doSomething") );
}
These two classes a build into my library without a problem. In my executable project which links the myLib I have the following main.cpp
main.cpp
#include <iostream>
#include "ClassA.h"
#include "ClassB.h"
int main(int argc, const char * argv[])
{
ClassA A;
ClassB B;
return 0;
}
The C++ flags:
The build results in the following errors:
duplicate symbol __ZTIPN5boost21errinfo_api_function_E in:
/Users/georg/Library/Developer/Xcode/DerivedData/ExceptionTest-dcnvciwqbwqvciaokkjjaqchftll/Build/Products/Debug/libExceptionLib.a(ClassA.o)
/Users/georg/Library/Developer/Xcode/DerivedData/ExceptionTest-dcnvciwqbwqvciaokkjjaqchftll/Build/Products/Debug/libExceptionLib.a(ClassB.o)
duplicate symbol __ZTSPN5boost21errinfo_api_function_E in:
/Users/georg/Library/Developer/Xcode/DerivedData/ExceptionTest-dcnvciwqbwqvciaokkjjaqchftll/Build/Products/Debug/libExceptionLib.a(ClassA.o)
/Users/georg/Library/Developer/Xcode/DerivedData/ExceptionTest-dcnvciwqbwqvciaokkjjaqchftll/Build/Products/Debug/libExceptionLib.a(ClassB.o)
ld: 2 duplicate symbols for architecture i386 clang: error: linker
command failed with exit code 1 (use -v to see invocation)
If I change the Compiler Flags to the following it works:
The problem is the boost::errinfo_api_function object, but I do not understand why?
I hope someone can help me.
Kind Regards
Georg