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
Related
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.
I am new to C++ programming and I have been stucked in this for hours.
I have three header files: A.h, B.h and common.h and two .cpp files: A.cpp, B.cpp.
A.h
#ifndef A_H
#define A_H
#include"B.h"
class AA{
public:
void fun();
};
#endif
B.h
#ifndef B_H
#define B_H
#include <iostream>
#include "common.h"
using namespace std;
class BB{
public:
void fun2();
};
#endif
common.h
extern int c;
A.cpp
#include "A.h"
void AA::fun(){
cout<<"A"<<endl;
}
B.cpp
#include "B.h"
void BB::fun2(){
cout<<"FUN 2"<<endl;
}
main.cpp
#include "A.h"
int main(){
int c = 5;
AA a;
a.fun();
return 0;
}
Error: undefined reference to `AA::fun()'
Why am I getting this error? Can anyone explain it?
Thanks a lot.
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
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
I'm trying to force inline a member function and I get the error:
"a_class::mem_func()", referenced from:
func(a_class&) in func.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
here is the distilled error-producing code
a_class.h
#ifndef A_CLASS_H #define A_CLASS_H
class a_class {public: __inline__ void mem_func(); };
#endif
a_class.cpp
#include "a_class.h"
__inline__ void a_class::mem_func() {}
func.h
#ifndef FUNC_H #define FUNC_H #include"a_class.h"
void func(a_class & obj);
#endif
func.cpp
#include "func.h"
void func(a_class & obj) {obj.mem_func();}
main.cpp
#include <iostream> #include "func.h" #include "a_class.h"
int main () {a_class obj; func(obj);}
I'm using Xcode / gcc
If you want to make sure your function is inlined, move the definition in the header:
a_class.h
#ifndef A_CLASS_H #define A_CLASS_H
class a_class {public: __inline__ void mem_func() {} /*<-definition*/; };
#endif