undefined reference to `vtable for implementation' error - c++

I wrote some c++ files and after compiling with out make file it works fine . But when using make file it pop out some errors . My codes are :
include directory files :
application.h
#ifndef APPLICATION_H
#define APPLICATION_H
#include "employee.h"
#include "employee_data.h"
#include "employee.h"
...some defintions here...
#endif
employee.h
#ifndef EMPLOYEE_H
#define EMPLOYEE_H
#include "employee_data.h"
#endif
employee_data.h
#ifndef EMPLOYEE_DATA_H
#define EMPLOYEE_DATA_H
typedef struct
{
int emp_id;
char *name,
*dept,
*book,
*time;
}employeedata;
...some codes here...
#endif
library.h
#ifndef LIBRARY_H
#define LIBRARY_H
#include "employee_data.h"
#include "application.h"
using namespace std;
class Library
{
public:
virtual int addE() = 0;
virtual int deleteE() = 0;
virtual int issue() = 0 ;
virtual int returnB() = 0;
virtual int employee() = 0;
};
class implementation : public Library
{
private:
employeedata *emp; /*structure object*/
public:
int addE();
int deleteE();
int issue();
int returnB();
int employee();
};
#endif
main.h
#ifndef MAIN_H
#define MAIN_H
#include "library.h"
class message
{
public:
void errormessage(int);
};
#endif
and my src directory conatins .cpp files . It includes
main.cpp
#include "main.h"
#include "library.h"
#include "employee_data.h"
#include "application.h"
int main()
{
message msg;
/* codes here..../*
}
library_function.cpp
#include "library.h"
#include "employee.h"
#include "main.h"
#include "application.h"
#include "employee_data.h"
int implementation :: addE()
{
}
etc..
error_function.cpp
#include "main.h"
void message :: errormessage(int errno)
{
}
employee_functions.cpp
#include "employee.h"
#include "main.h"
..some code...
display.cpp
#include "employee_data.h"
#include "application.h"
..some code..
thread.cpp
#include "employee.h"
#include "application.h"
...some code..
and my make file is :
CC=g++
FLAGS=-o
CFLAGES=-c -Wall
THREAD=-lpthread
INCLUDE=../include/
SRC=../src/
OBJ=../obj/
OUTPUT=../bin/
$(OUTPUT)vkp:$(OBJ)main.o $(OBJ)library_functions.o $(OBJ)employee_functions.o $(OBJ)display.o $(OBJ)error_function.o $(OBJ)thread.o
$(CC) $(FLAGS) vkp $(OBJ)main.o $(OBJ)library_functions.o $(OBJ)employee_functions.o $(OBJ)display.o $(OBJ)error_function.o $(OBJ)thread.o $(THREAD)
mv vkp $(OUTPUT)
$(OBJ)main.o:$(SRC)main.cpp $(INCLUDE)main.h $(INCLUDE)employee_data.h $(INCLUDE)application.h
$(CC) $(CFLAGS) $(SRC)main.cpp -I $(INCLUDE)
mv main.o $(OBJ)
$(OBJ)library_functions.o:$(SRC)library_functions.cpp $(INCLUDE)library.h $(INCLUDE)employee.h $(INCLUDE)main.h $(INCLUDE)application.h $(INCLUDE)employee_data.h
$(CC) $(CFLAGS) $(SRC)library_functions.cpp -I $(INCLUDE)
mv main.o $(OBJ)
$(OBJ)employee_functions.o:$(SRC)employee_functions.cpp $(INCLUDE)employee.h $(INCLUDE)main.h
$(CC) $(CFLAGS) $(SRC)employee_functions.cpp -I $(INCLUDE)
mv main.o $(OBJ)
$(OBJ)display.o:$(SRC)display.cpp $(INCLUDE)employee_data.h $(INCLUDE)application.h
$(CC) $(CFLAGS) $(SRC)display.cpp -I $(INCLUDE)
mv main.o $(OBJ)
$(OBJ)error_function.o :$(SRC)error_function.cpp $(INCLUDE)main.h
$(CC) $(CFLAGS) $(SRC)error_function.cpp -I $(INCLUDE)
mv main.o $(OBJ)
$(OBJ)thread.o:$(SRC)thread.cpp $(INCLUDE)employee.h $(INCLUDE)application.h
$(CC) $(CFLAGS) $(SRC)thread.cpp -I $(INCLUDE)
mv main.o $(OBJ)
After runing make i got eroor like :
g++ ../src/main.cpp -I ../include/
/tmp/cc09snhj.o: In function `main':
main.cpp:(.text+0x568): undefined reference to `message::errormessage(int)'
main.cpp:(.text+0x5fb): undefined reference to `message::errormessage(int)'
main.cpp:(.text+0x6c5): undefined reference to `message::errormessage(int)'
main.cpp:(.text+0x758): undefined reference to `message::errormessage(int)'
main.cpp:(.text+0x7f3): undefined reference to `message::errormessage(int)'
/tmp/cc09snhj.o: In function `implementation::implementation()':
main.cpp:(.text._ZN14implementationC2Ev[_ZN14implementationC5Ev]+0x1f): undefined reference to `vtable for implementation'
collect2: ld returned 1 exit status
make: *** [../obj/main.o] Error 1
what is wrong on my code ? any problem in make file ? I think the problem is linking the header files . Is this correct way to link header files ? Please help me to get my make file work .

I think you just misspelled CFLAGS in CFLAGES=-c -Wall
I'm guessing this is the case since
g++ ../src/main.cpp -I ../include/
does not have the -c option

Related

Strange error with the Test_F and EXPECT_EQ functions of GTEST

I'm trying to implement an extremely simple GoogleTest example in VScode on Linux and I'm getting some weird errors that I cannot find online. When I'm trying to actually define the tests that will be run -
'''
#include <limits.h>
#include "gtest/gtest.h"
#include </home/tester/src/Multiply.h>
class MultiplyTest : public ::testing::Test {
protected:
virtual void SetUp() {
}
virtual void TearDown() {
}
};
TEST_F(MultiplyTest,twoValues){
const int x = 4;
const int y = 5;
Multiply multiply;
EXPECT_EQ(20,multiply.twoValues(x,y));
EXPECT_EQ(6,multiply.twoValues(2,3));
}
'''
Errors get thrown at the TEST_F and EXPECT_EQ parts, saying specifically that TEST_F is "this declaration has no storage class or type specifier" and EXPECT_EQ is "class "testing::internal::EqHelper" has no member "Compare"."
Any help would be greatly appreciated.
Here is my multiply.h header file:
#ifndef _MULTIPLY_HPP_
#define _MULTIPLY_HPP_
class Multiply{
public:
static int twoValues(const int x, const int y);
};
#endif
and my makefile:
CXX = gcc
CXXFLAGS = -g -L/opt/gtest/lib -lgtest -lgtest_main -lpthread
INCS = -I./ -I../../src -I/opt/gtest/include
OBJS = ../../src/Addition.o Addition_Test.o ../../src/Multiply.o Multiply_Test.o
testAll: $(OBJS)
$(CXX) $(CXXFLAGS) $(INCS) -o testAll Main_TestAll.cpp $(OBJS)
.cpp.o:
$(CXX) $(CXXFLAGS) -c $< -o $# $(INCS)
clean:
rm testAll *.o testAll.xml
I'm currently running gcc 7.3.1 if that helps.

How to fix : 'undefined reference to foo()' with Makefile and g++

I've been trying to get into C++ again, and I have a linker error when I try to compile :
main.cpp:(.text+0x92): undefined reference to `eval(std::string, double, double, double)'
I've already tried compiling each file separatly and then linking by hand but it never works.
I have tried numerous solutions provided by other similar posts but none of these work and I don't have any idea on how to fix my problem (which doesn't seem to be that hard)
main.cpp :
#define _GLIBCXX_USE_CXX11_ABI 0
#include <iostream>
#include "functions.h"
int main()
{
std::string expression = "1/2+x";
double x = 1.45;
double y = 1.65;
double z = 1.77;
double result = 0.0;
result = eval(expression,x,y,z);
std::cout << result << std::endl;
return 0;
}
functions.cpp :
#include "functions.h"
double eval(std::string expression_s, double x, double y, double z)
{
typedef exprtk::symbol_table<double> symbol_table_t;
typedef exprtk::expression<double> expression_t;
typedef exprtk::parser<double> parser_t;
symbol_table_t symbol_table0;
symbol_table_t symbol_table1;
symbol_table_t symbol_table2;
expression_t expression;
parser_t parser;
symbol_table0.add_variable("x",x);
symbol_table1.add_variable("y",y);
symbol_table2.add_variable("z",z);
expression.register_symbol_table(symbol_table0);
expression.register_symbol_table(symbol_table1);
expression.register_symbol_table(symbol_table2);
parser.compile(expression_s,expression);
std::cout << expression.value();
return expression.value();
}
functions.hpp :
ifndef FUNCTIONS_H_INCLUDED
#define FUNCTIONS_H_INCLUDED
#include "exprtk/exprtk.hpp"
#include <iostream>
double eval(std::string expression_s, double x, double y, double z);
#endif
Makefile :
CC = g++
EXEC = Crystal_MET
LIBS =
FLAGS =
all: main.o
$(CC) *.o -o $(EXEC) $(LIBS)
main.o : functions.o main.cpp functions.h
$(CC) main.cpp -c $(FLAGS)
functions.o : functions.cpp functions.h
$(CC) functions.cpp -c $(FLAGS)
clear :
rm -f *.o
mr_proper :
rm -f *.o $(EXEC)
main.cpp is compiled against a different ABI than functions.cpp because of :
#define _GLIBCXX_USE_CXX11_ABI 0
Either remove that line from main.cpp, or add it to functions.cpp .
More details : https://gcc.gnu.org/onlinedocs/libstdc++/manual/using_dual_abi.html

Undefined reference to my function

I have some trouble with building the part of my project. Here it is:
utility.h:
#ifndef UTILITY_H
#define UTILITY_H
#include <boost/array.hpp>
#include <boost/shared_ptr.hpp>
#include <fstream>
#include <iostream>
#include <openssl/md5.h>
#include <string>
namespace utility {
typedef boost::array<unsigned char, MD5_DIGEST_LENGTH> md5sum;
typedef boost::shared_ptr<md5sum> md5sum_ptr;
...
md5sum_ptr calculate_md5(const std::string& full_path);
};
#endif //UTILITY_H
utility.cpp:
#include "utility.h"
using namespace utility;
...
md5sum_ptr calculate_md5(const std::string& full_path) {
md5sum_ptr md5(new md5sum);
std::ifstream in(full_path);
std::string content;
in >> content;
MD5((unsigned char*)content.data(), content.size(), md5->data());
return md5;
}
cdaemon.cpp
void CTCPConnection::handle_read_filename(ECommand command, EDataType datatype,
const boost::system::error_code& ec) {
...
case ECommand::GET_MD5:
_send_container<md5sum>(*(calculate_md5(filename)));
async_write(m_socket, m_writebuf,
boost::bind(&CTCPConnection::handle_write_response, shared_from_this(),
placeholders::error));
break;
...
}
}
Makefile:
CC=g++
override CFLAGS+=-c -std=c++11 -Wall -Wextra -Werror
override LFLAGS+=-lboost_log -lpthread -lboost_system -lboost_thread -lcrypto
server: servermain.o cdaemon.o
$(CC) ../build/datatype.o ../build/utility.o build/servermain.o build/cdaemon.o -o build/frtpdaemon $(LFLAGS)
servermain.o: main.cpp
$(CC) $(CFLAGS) main.cpp -o build/servermain.o
cdaemon.o: cdaemon.cpp
$(CC) $(CFLAGS) -DBOOST_LOG_DYN_LINK cdaemon.cpp -o build/cdaemon.o
When I try to build server, I get linker error:
build/cdaemon.o: In function `CTCPConnection::handle_read_filename(utility::ECommand, utility::EDataType, boost::system::error_code const&)':
cdaemon.cpp:(.text+0x127c): undefined reference to `utility::calculate_md5(std::string const&)'
I have already tried to google the solution, but all what I could find were suggestions to change the order of ../build/utility.o and build/cdaemon.o. That didn't help me.
You don't define the function you have declared in the header. A using directive is insufficient when defining functions in a namespace. You'll either need to open the namespace before defining your function or qualify the function with the namespace, e.g.:
utility::md5sum_ptr utility::calculate_md5(const std::string& full_path) {
...
}

Very basic: Why won't my makefile work with common suffix

objects = hello.o name.o printing.o
exename = himake
$(exename): $(objects)
$(CC) -o $(exename) $(objects)
%.o: %.cpp
$(CC) -c $^
I am trying to use common suffixes so I do not need to compile 3 files into .o first. This is supposed to do all three with the % wildcard.
It works fine when I do it the long way but not this.
Running the above makefile gives me this error below:
[alex#pcc Dir]$ make
cc -o himake hello.o name.o printing.o
hello.o: In function `__static_initialization_and_destruction_0(int, int)':
hello.cpp:(.text+0x23): undefined reference to `std::ios_base::Init::Init()'
hello.o: In function `__tcf_0':
hello.cpp:(.text+0x66): undefined reference to `std::ios_base::Init::~Init()'
and more that I did not include
Files:
hello.cpp:
// hello.cpp
// standard library
#include <iostream>
#include <string>
using namespace std;
// user defined header files
#include "name.h"
#include "printing.h"
int main ()
{
string name;
name = getName(); // getName is in name.h
printHello(name); // printHello is in print.h
return 0;
}
name.cpp
// name.cpp
// user defined header files
#include "name.h"
#include "printing.h"
string getName()
{
string name;
printGreeting(); // printGreeting is from print.h
getline(cin, name);
return name;
}
name.h
// name.h
#include <iostream>
using namespace std;
string getName();
printing.cpp
// printing.cpp
// user defined include files
#include "printing.h"
void printGreeting(void)
{
cout << "Your name: ";
return;
}
void printHello (string name)
{
cout << "Hi, " << name << endl;
return;
}
printing.h
// printing.h
#include <iostream>
using namespace std;
void printGreeting();
void printHello(string);
Because you use the C compiler frontend program, and not the C++ frontend program.
Change $(CC) to $(CXX).

C++: Undefined referense to class::function()

i been having a problem that i tried for many days to fix but i cant find a solution for it.
I keep getting this Error:
undefined reference to 'Simulator::Simulator(int, int)'
undefined reference to 'Simulator::run();'
undefined reference to 'Simulator::~Simulator()'
undefined reference to 'Simulator::~Simulator()'
I searched for an answer but nothing seems to be working for me.
Here are my classes:
main.cpp:
#include <iostream>
#include <cstdlib>
#include "Simulator.h"
int main(int argc, char** argv) {
//Some code
Simulator sim(rows, cols);
sim.run();
}
Simulator.h:
#include <iostream>
#include "PersonQueue.h"
#include "DogQueue.h"
class Simulator {
void drawBoard();
DogQueue _dq;
PersonQueue _pq;
public:
int dog_num, person_num;//Number of entitys
int _rows, _cols;//Should be static but wont work.
void fill_queue();//check
void init_clrs();
Simulator(int rows, int cols);//check
void run();//check
~Simulator();
};
Simulator.cpp:
#ifndef SIM_C
#define SIM_C
#include <iostream>
#include <curses.h>
#include "Simulator.h"
int Simulator:: _rows;
int Simulator:: _cols;
Simulator::Simulator(int row, int col) {
_rows = row;
_cols = col;
dog_num = 2; //number of dog entitys
person_num = 2;//number of person entitys
}
void Simulator::init_clrs() {
//some code...
}
void Simulator::drawBoard() {
//some code...
}
void Simulator::fill_queue() {
//some code...
}
void Simulator::run() {
initscr();
start_color();
Simulator::init_clrs();
Simulator::fill_queue();
Simulator::drawBoard();
while (std::cin.get() != (char)27) {
Person* person_obj = _pq.dequeue();
Dog* dog_obj = _dq.dequeue();
person_obj->Person::advance();
dog_obj->Dog::advance();
if (dog_obj->steps == MAX_STEP && person_obj->steps) {
_dq.enqueue(dog_obj);
_pq.enqueue(person_obj);
}
refresh();
}
}
Simulator::~Simulator() {//To_be_changed
delete this;
}
#endif
From what i read i believe the problem may be with the makefile but it seems ok and works until i get the undefined error.
makefile just in case:
tester: main.o simulator.o personqueue.o dogqueue.o character.o person.o dog.o
g++ -Wall main.cpp -lncurses -o tester
main.o: main.cpp
g++ -c -Wall main.cpp -o main.o
simulator.o: Simulator.cpp Simulator.h
g++ -c -Wall Simulator.cpp Simulator.h -o simulator.o
character.o: Character.cpp Character.h
g++ -c -Wall Character.cpp Character.h -o character.o
person.o: Person.cpp Person.h
g++ -c -Wall Person.cpp Person.h -o person.o
dog.o: Dog.cpp Dog.h
g++ -c -Wall Dog.cpp Dog.h -o dog.o
personqueue.o: PersonQueue.cpp PersonQueue.h
g++ -c -Wall PersonQueue.cpp PersonQueue.h -o personqueue.o
dogqueue.o: DogQueue.cpp DogQueue.h
g++ -c -Wall DogQueue.cpp DogQueue.h -o dogqueue.o
clean:
rm -rf *.o tester
Thank you for any input on this issue.
EDIT:
I dont believe the problem is with DogQueue or Person queue as all they include is Dog.h Person.h
but anyways just to be sure:
#include "DogQueue.h"
#include <cstdlib>
//Some code...
#include "PersonQueue.h"
#include <cstdlib>
//some code...
i think you have issues in your make file,
tester: main.o simulator.o personqueue.o dogqueue.o character.o person.o dog.o
g++ -Wall main.cpp -lncurses -o tester
try something like this, though I have not tested the code,
tester: main.o simulator.o personqueue.o dogqueue.o character.o person.o dog.o
g++ -o tester main.o simulator.o personqueue.o dogqueue.o character.o person.o dog.o -lncurses