Undefined reference in main Makefile - c++

I did a sample project in linux but i am getting error while running main Makefile
Project Info:
project/database folder having files database.h , database.cpp , bulid-database ,Makefile
database.h
/*data base file*/
#include<iostream>
using namespace std;
class mydatabase
{
public:
mydatabase(int a , int b);
int sum(){return m_a +m_b;}
int diff(){return m_a -m_b;}
int mul(){return m_a *m_b;}
float div(){return m_a /m_b;}
int reminder(){return m_a %m_b;}
private:
int m_a , m_b;
};
database.cpp
#include "database.h"
mydatabase::mydatabase(int a ,int b):m_a(a) , m_b(b)
{
}
bulid-database
make
if [ -f libdatabase.a ];
then
echo "Database-Library Build Success"
cp libdatabase.a ../LIBs/
else
echo "databse-Library Build Failure"
fi
Makefile
HEADERFILES = $(wildcard *.h)
CPPFILES = $(wildcard *.cpp)
OBJFILES = $(patsubst %.cpp,%.o ,$(wildcard *.cpp))
$(OBJFILES): %.o : %.cpp $(HEADERFILES)
g++ -c -o $# $<
ar ruv libdatabase.a $#
ranlib libdatabase.a
project/Main folder having files main.cpp , Makefile
main.cpp
#include "database.h"
#include <iostream>
int main()
{
mydatabase *obj = new mydatabase(10 ,5);
std::cout<<"sum is"<<obj->sum()<<endl;
std::cout<<"diff is"<<obj->diff()<<endl;
std::cout<<"mul is"<<obj->mul()<<endl;
std::cout<<"div is"<<obj->div()<<endl;
std::cout<<"reminder is"<<obj->reminder()<<endl;
getchar();
return 0;
}
Makefile
CC = g++
INCPATH = -I. \
-I.. \
-I../database
LIBPATH = -L../LIBs
LDFLAGS = ${LIBPATH}/libdatabase.a
CFLAGS = ${INCPATH}
testdate:main.o
$(CC) $(CFLAGS) -o testdate main.o $(LDFLAGS)
main.o:main.cpp
$(CC) $(CFLAGS) -c -o main.o main.cpp
ISSUE: database make file is working fine but main Makefile i am having some issue like
Error: main.o: In function main':
main.cpp:(.text+0x92): undefined reference tomydatabase::mydatabase(int, int)'
collect2: ld returned 1 exit status

This line is wrong:
$(CC) $(CFLAGS) -o testdate $(LDFLAGS) main.o
because the library should be specificed AFTER the object main.o on the line. This is due to the way the linker handles the objects. Look at this example:
gcc -o test someobject.o library.a
The linker will:
look up all undefined references of someobject.o and store them
then it opens library.a and resolves the undefined references via library.a
then it closes library.a
If the object and the library are in the other way around, then the linker opens library.a, sees no undefined references in its table and closes it. Then it tries and compiles someobject.o and the undefined references are never satisfied
EDIT:
This is a well-known caveat of GCC, a more detailed stack-overflow explanation can be seen here, and options --start-group and --end-group can help resolve cases where A depends on B, and B depends on A.

It's your Makefile. You want:
libdatabase.a
or
-ldatabase
at the end of your main compile line

Related

Can't cross compile c++ class with sqlite included (undefined reference) on linux (i686-w64-mingw32-g++)

I tried to compile my c++ class with sqltie3 include.
I run Ubuntu 20.04.
For this I use the i686-w64-mingw32-g++ cross compiler, I also tested it with g++ and it works perfectly but not with the i686-w64-mingw32-g++ compiler.
I always get the same error:
/usr/bin/i686-w64-mingw32-ld: DBWrapper.o:DBWrapper.cpp:(.text+0x61): undefined reference to `sqlite3_open'
/usr/bin/i686-w64-mingw32-ld: DBWrapper.o:DBWrapper.cpp:(.text+0x12c): undefined reference to `sqlite3_close'
My Makefile:
mingw = i686-w64-mingw32-g++
SQLCOMPILE = -I/usr/local/sqlite/include
CFLAGS = -Wall
SRCFILES = *.cpp
OBJFILES = *.o
TARGETWIN = progwx.exe
all: $(TARGETWIN)
$(TARGETWIN): $(OBJFILES)
$(mingw) $(CFLAGS) $(OBJFILES) $(SQLCOMPILE) -o $(TARGET) -l sqlite3
$(OBJFILES): $(SRCFILES)
$(mingw) $(CFLAGS) $(SQLCOMPILE) -c $(SRCFILES)
.PHONY: clean
clean:
rm -f *.o
I put the sqlite source code in $(SQLCOMPILE) and the compiler can include everything unless I call the sqlite3_open() function.
I also compiled the source code to a library (libsqlite3.a) so that the -l tag can find it.
Because the g++ compiler brings also the same "undefined reference" error if I don't put the -lsqlite3 tag in.
$ i686-w64-mingw32-gcc -c sqlite3.c
$ ar rcs libsqlite3.a sqlite3.o
Here is my Headerfile (DBWrapper.h):
#pragma once
#include <string>
#include <stdexcept>
#include <sqlite3.h>
class DBWrapper {
sqlite3 *db_;
const int errCode;
public:
DBWrapper(const std::string &dbname);
DBWrapper(const DBWrapper&) = delete;
DBWrapper& operator=(const DBWrapper&) = delete;
sqlite3* operator*();
~DBWrapper();
};
Here is my CPP-File (DBWrapper.cpp):
#include "DBWrapper.h"
DBWrapper::DBWrapper(const std::string &dbname) : db_(nullptr), errCode(sqlite3_open(dbname.c_str(), &db_)){
if(errCode) {
throw std::runtime_error("ERROR at opening database!");
}
}
DBWrapper::~DBWrapper() {
sqlite3_close(db_);
}
sqlite3* DBWrapper::operator *() {
return db_;
}
(sorry for my bad english)
I figured it out I just needed sqlite3 as a compiled object file (sqlite3.o) for the compiler.

C++ undefined reference in Makefile

I created my Makefile for a simple program but it returns undefined reference for class functions constantly:
g++ -c src/main.cpp -o lib/main.o
g++ -c src/functions.cpp -o lib/functions.o
g++ -c src/Circular.cpp -o lib/Circular.o
g++ lib/main.o -o bin/app.exe
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: lib/main.o:main.cpp:(.text+0x20): undefined reference to `Circular::Circular()'
collect2.exe: error: ld returned 1 exit status
make.exe: *** [app.exe] Error 1
Here is my Makefile:
app.exe: lib/main.o lib/Circular.o lib/functions.o
g++ lib/main.o -o bin/app.exe
lib/functions.o: src/functions.cpp
g++ -c src/functions.cpp -o lib/functions.o
lib/Circular.o: src/Circular.cpp
g++ -c src/Circular.cpp -o lib/Circular.o
lib/main.o: src/main.cpp
g++ -c src/main.cpp -o lib/main.o
Here is a short snippet of main.cpp:
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <fstream>
#include <string>
#include "../include/Circular.h"
#include "../include/functions.h"
using namespace std;
int main(int argc, const char * argv[]) {
Circular item;
return 0;
}
Circular.h:
#include "node.h"
class Circular
{
public:
Circular();
node *start;
node *last;
int counter;
}
Circular.cpp:
#include "../include/Circular.h"
#include <iostream>
using namespace std;
Circular::Circular()
{
start = NULL;
last = NULL;
}
and node.h:
struct node
{
int data;
struct node *next;
struct node *prev;
};
I know the problem is about linker and with Makefile but even though I tried different possible solutions, somehow it doesn't work. Therefore, maybe someone can see the mistake I am making. Thanks!
I managed to create a Makefile from this source .
The Makefile looks like this:
CXX = g++
CXXFLAGS = -std=c++17 -Wall
LXXFLAGS = -std=c++17
OBJECTS = main.o Circular.o functions.o
TARGET = main
$(TARGET): $(OBJECTS)
$(CXX) $(LXXFLAG) $(OBJECTS) -o $(TARGET)
main.o: main.cpp Circular.cpp Circular.h functions.cpp functions.h
$(CXX) $(CXXFLAGS) -c main.cpp
Circular.o: Circular.cpp
$(CXX) $(CXXFLAGS) -c Circular.cpp
functions.o: functions.cpp
$(CXX) $(CXXFLAGS) -c functions.cpp
clean:
rm -f $(TARGET) $(OBJECTS)
And also added cout to you Circular constructor to check the execution as below:
#include "Circular.h"
#include <iostream>
using namespace std;
Circular::Circular()
{
start = NULL;
last = NULL;
cout << "Yes!" << endl;
}
Here's the result:
Output
Don't forget to put a semicolon for your Circular class in your Circular.h.
NOTE: If you aren't able to use make in cmd,use choco install make.
The Makefile should be structured to build the dependencies, then the final assembly into a .exe. Each path should be specified exactly as it is, not approximated:
app.exe: lib/main.o lib/Circular.o lib/functions.o
g++ lib/main.o lib/Circular.o lib/functions.o -o app.exe
lib/main.o: src/main.cpp
g++ -c src/main.cpp -o lib/main.o
lib/functions.o: src/functions.cpp
g++ -c src/functions.cpp -o lib/functions.o
lib/Circular.o: src/Circular.cpp
g++ -c src/Circular.cpp -o lib/Circular.o
The key here is be consistent and that includes things like the order of things specified in this file. Whatever order you pick, stick to it. This makes tracking down problems way easier.
If this project gets more complex you probably want to pivot to using a dependency tracking Makefile template instead of this homebrew one. Note how in those you don't need to specify a rule for each file, but instead a rule for each type of file, as in .cpp -> .o, and the rest happens automatically.

c++ Makefile compiling-gflags

I am having trouble compiling using a makefile and including gflags. I have not done Makefiles in a while. The compile error I am getting is related to gflags
This is my makefile:
CXX = g++
CPPFlags = -g -Wall -std=c++11
LDLIBS = -lgflags
pa1: Main.cpp PA1.o Node.o
$(CXX) $(CPPFlags) $(LDFLAGS) Main.cpp Node.o PA1.o -o PA1
PA1.o:PA1.h PA1.cpp
$(CXX) $(CPPFlags) -c PA1.cpp -o PA1.o
Node.o:Node.h Node.cpp
$(CXX) $(CPPFlags) -c Node.cpp -o Node.o
This is my main.cpp
#include "PA1.h"
#include <string>
#include <iostream>
#include <fstream>
#include <string>
#include <gflags/gflags.h>
DEFINE_string(root, "0,0", "Root");
int main(int argc, char *argv[])
{
gflags::ParseCommandLineFlags(&argc, &argv, true);
PA1 run= PA1(argv[2]);
std::string rc=FLAGS_root;
int r= rc[0];
int c= rc[2];
if(run.ReadMaze()==-1)
{
return -1;
}
run.SolveMaze(r,c);
return 0;
}
edit:
This is the error message
g++ -g -Wall -std=c++11 Main.cpp Node.o PA1.o -o PA1
/tmp/ccIdQf46.o: In function `main':
/home/peteryan/Documents/Main.cpp:10: undefined reference to
`google::ParseCommandLineFlags(int*, char***, bool)'
/tmp/ccIdQf46.o: In function `__static_initialization_and_destruction_0(int,
int)':
/home/peteryan/Documents/Main.cpp:7: undefined reference to
`google::FlagRegisterer::FlagRegisterer(char const*, char const*, char
const*, char const*, void*, void*)'
collect2: error: ld returned 1 exit status
Makefile:5: recipe for target 'pa1' failed
make: *** [pa1] Error 1
The linker does not appear to be linking with with -lgflags and your Makefile is likely the cause. You should put your LDLIBS at the end of your build arguments. This generic Makefile will compile all the .cpp sources and link all the object files in the same directory. It also should track your header file dependencies. Notice the LDLIBS at the end of the build directive. Give it a try for your program.
program_name := PA1
CXX := g++
CXXFLAGS := —g -Wall std=c++11
LDLIBS := -lgflags
source_files := $(wildcard *.cpp)
objects := ${source_files:.cpp=.o}
all: $(program_name)
$(program_name): $(objects)
$(CXX) $(CXXFLAGS) $(LDFLAGS) -o $(program_name) $(objects) $(LDLIBS)
depend: .depend
.depend: $(source_files)
rm -f ./.depend
$(CXX) $(CXXFLAGS) -MM $^>>./.depend;
clean:
rm -f $(objects)
distclean: clean
rm -f *~ .depend
include .depend

C++ undefined reference with static library

I got 'undefined reference' when compile code with a static library. I have searched a lot of answer which told to use extern "C" or correct the order of parameter when compiling, but those doesn't solve my problem.
I have these :
compiler.hpp
#ifndef COMPILER_H
#define COMPILER_H
int buildTree();
#endif
compile.cpp
#include <compiler.hpp>
int buildTree() {
Package package = new Package();
}
And a lot of another source code, then I build them to lib.a by building them into .o files with -c flag and :
ar rvs lib.a $(OBJS)
My Main.cpp:
#include <compiler.hpp>
int main() {
buildTree();
}
Finally, I build executable file :
LIBS = lib.a
main: main.cpp
$(CPP) $(CPPFLAGS) $(LIBDIR) $^ $(LIBS) -o $#
But g++ complier throws this Error :
undefined reference to `buildTree()'

Can't compile the project after splitting the source files to cpp and h, getting "undefined refernce to..."

I had a project, where I had only .cpp files. It worked well, but then I realized, that it's not a good practice, so I decided to split it to .cpp and .h files. Nevetheless now I'm not able to compile the project. Could anybody please look at the source and tell me, where the problem is?
Bot.h
#ifndef BOT_H
#define BOT_H
#include <vector>
#include <string>
#include <cstdlib>
using namespace std;
/**
* Class that represents casual Bot - the parent of other bots
*/
class Bot {
public:
Bot();
virtual ~Bot();
bool initialized;
string getRandomMessage();
string getName();
protected:
vector<string> messages;
string name;
};
#endif /* BOT_H */
Bot.cpp
#include <vector>
#include <string>
#include <cstdlib>
#include "Bot.h"
using namespace std;
string Bot::getRandomMessage() {
int r = static_cast<double> (std::rand()) / RAND_MAX * this->messages.size();
return messages[r];
}
Bot::Bot(){
}
Bot::~Bot(){
}
string Bot::getName() {
return this->name;
}
Example of the class that inherits from the Bot class:
GrumpyBot.h
#ifndef GRUMPYBOT_H
#define GRUMPYBOT_H
#include "Bot.h"
class GrumpyBot : public Bot{
public:
GrumpyBot();
GrumpyBot(const GrumpyBot& orig);
virtual ~GrumpyBot();
};
#endif /* GRUMPYBOT_H */
GrumpyBot.cpp
#include "GrumpyBot.h"
GrumpyBot::GrumpyBot() {
initialized = true;
this->name = "GrumpyBot";
messages.push_back("I hate dogs.");
messages.push_back("I hate cats.");
messages.push_back("I hate goats.");
messages.push_back("I hate humans.");
messages.push_back("I hate you.");
messages.push_back("I hate school.");
messages.push_back("I hate love.");
}
Till now it is ok, but problems appear in the Server.cpp class, where I try to create new instances of those classes and call their functions.
I include there both #include "Bot.h" & #include "GrumpyBot.h" and the compiler keeps getting me messages like /home/ubuntu/NetBeansProjects/SemestralniPraceChat/./Server.cpp:335: undefined reference to 'GrumpyBot::GrumpyBot()'
My makefile looks like this:
#macros
Remove=rm -rf
Doxygen=Doxyfile
RUN=./dvoram64
FLAGS=-Wall -pedantic -Wno-long-long -O0 -ggdb -lncurses -pthread -g
OBJECTS=main.o Bot.o Server.o Client.o
#generates final binary and documentation
all: $(Doxygen)
make compile
#build into final binary
compile: $(RUN)
#run program
run: $(RUN)
$(RUN)
clean:
$(Remove) dvoram64
$(Remove) $(OBJECTS)
#generate documentation in '<login>/doc' folder
doc: $(Doxygen) /*
( cd ./ | doxygen $(Doxygen))
#rules how to compile into the executalble file
$(RUN): $(OBJECTS)
Bot.o: ./Bot.cpp ./Bot.h
g++ $(FLAGS) -c ./Bot.cpp
DummyBot.o: ./DummyBot.cpp ./DummyBot.h ./Bot.h
g++ $(FLAGS) -c ./DummyBot.cpp
GrumpyBot.o: ./GrumpyBot.cpp ./GrumpyBot.h ./Bot.h
g++ $(FLAGS) -c ./GrumpyBot.cpp
JokerBot.o: ./JokerBot.cpp ./JokerBot.h ./Bot.h
g++ $(FLAGS) -c ./JokerBot.cpp
WeatherBot.o: ./WeatherBot.cpp ./WeatherBot.h ./Bot.h
g++ $(FLAGS) -c ./WeatherBot.cpp
Client.o: ./Client.cpp
g++ $(FLAGS) -c ./Client.cpp
main.o: ./main.cpp ./Server.cpp ./Bot.h ./JokerBot.h ./WeatherBot.h ./GrumpyBot.h ./DummyBot.h ./Client.cpp
g++ ./main.cpp $(FLAGS) -o ./dvoram64
Server.o: ./Server.cpp ./Bot.h ./JokerBot.h ./WeatherBot.h ./GrumpyBot.h ./DummyBot.h
g++ $(FLAGS) -c ./Server.cpp
undefined reference is a linker error, you are not passing the objects for the linking process.
In the makefile, replace main.o: lines with
main.o: main.cpp
g++ $(FLAGS) -c main.cpp
remove -lncurses from $(FLAGS) and add:
link: <all the o files>
g++ <all the o files> -lncurses -pthread -o dvoram64
then calling:
make link
will create the correctly linked executable.
Edit:
If you define the $(OBJECTS) variable, the link should be:
link: $(OBJECTS)
g++ $(OBJECTS) -lncurses -pthread -o dvoram64