Unable to properly reference class from another namespace - c++

I'm using an external library that has a class bplus_tree that is defined in a namespace bpt. The class declaration is below
// bpt.h
class bplus_tree {
public:
bplus_tree(const char *path, bool force_empty = false);
// bpt.cc
bplus_tree::bplus_tree(const char *p, bool force_empty)
: fp(NULL), fp_level(0)
{ code here }
I'm referencing it from another file main.cpp that has the following code
// main.cpp
#include "BPlusTree/bpt.h" // This is the correct path to the bpt.h file
bplus_tree tree("", false); // Error here
When I try and compile this with g++ main.cpp -o main -std=c++11, I get the following error.
error: unknown type name 'bplus_tree'; did you mean 'bpt::bplus_tree'?
When I change bplus_tree to bpt::bplus_tree, however, I get the new error:
Undefined symbols for architecture x86_64:
"bpt::bplus_tree::bplus_tree(char const*, bool)", referenced from:
_main in main_2-c3bbcc.o
ld: symbol(s) not found for architecture x86_64
I've tried a lot of different combinations for a couple hours now, and I'm honestly not sure what's going on. Is it just something obvious that I am missing or what else am I not getting?

Related

Undefined symbols building in C++ on macOS

While implementing class member functions in.cpp file on macOS 10.15, displaying an error:
clang: Undefined symbols for architecture x86_64:error:
"Circle::Area()", referenced from:
_main in main-4cfa92.o
"Circle::Circle(double)", referenced from:
_main in main-4cfa92.o
linker command failed with exit code 1 (use -v to see invocation)
To verrify this case, I found an example on the website to try, and it worked fine when I copied the function definitions from Circle.cpp file into Circle.h file, But when the function declaration is in Circle.h and function definition is in Circle.cpp files, respectively, an error occurs
//Circle.h
#ifndef CIRCLE_H
#define CIRCLE_H
class Circle
{
private:
double r;//radius
public:
Circle();//constructor
Circle(double R);//The constructor
double Area();//computing area
};
#endif
//Circle.cpp
#include "Circle.h"
Circle::Circle(){
this->r=5.0;
}
Circle::Circle(double R){
this->r=R;
}
double Circle::Area(){
return 3.14*r*r;
}
//main.cpp
#include <iostream>
#include "Circle.h"
using namespace std;
int main(){
Circle c(3);
cout<<"Area="<<c.Area()<<endl;
return 0;
}
Error message:
Undefined symbols for architecture x86_64:
"Circle::Area()", referenced from:
_main in main-4cfa92.o
"Circle::Circle(double)", referenced from:
_main in main-4cfa92.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Looks like you have not included Circle.cpp in the compilation step. Make sure you include both main.cpp and Circle.cpp

Linking Error - C++ Clang MacOs

I have two projects, both built with clang++ and Xcode on MacOS.
I have a library with a header defined as follows.... (serialization.h)
#pragma once
#include <visionApp/cv/matchers/visual_database.h>
namespace visionApp {
void serializeDB(visionApp::VisualDb visualDB, std::string fileName);
visionApp::VisualDb* deserializeDB(std::string fileName);
}
The cpp file is as follows.... (serialization.cpp)
#include "serialization.h"
namespace visionApp {
void serializeDB(visionApp::VisualDb visualDB, std::string fileName)
{
}
visionApp::VisualDb* deserializeDB(std::string fileName)
{
return new visualDB();
}
}
The method is then called in another class.....
void saveRecogniser(std::string fileName)
{
serializeDB(currentVisualDB.get(), fileName);
}
void loadRecogniser(std::string fileName)
{
mVisualDatabase.reset(deserializeDB(fileName));
}
Note: currentVisualDB is a shared pointer to .get() returns a pointer.
This all builds fine. Which is great...... and make libvisionApp.a
But when i build a dependent application that can only see the headers i get the following error....
Undefined symbols for architecture x86_64:
"visionApp::serializeRecognizer(visionApp::VisualDb*, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >)", referenced from:
visionApp::Recogniser::saveRecogniser(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >) in libvisionApp.a(planar_recogniser.o)
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Now, this linking error suggests that clang is not able to link the serialize method, but it has no problems with the deserialize method. Removing the serialize method, and leaving only the deserialize builds correctly.
I do not understand this issue, and am unsure how to proceed. Can someone educate me as what to do in a situation like this?
Any advice on how to tackle this issue?
Note: Lipo output for the library in question.
Hal:Release daniel$ lipo -info libvisionDB.a
input file libvisionDB.a is not a fat file
Non-fat file: libvisionDB.a is architecture: x86_64
You have declared serializeDB to get first parameter by value.
void serializeDB(visionApp::VisualDb visualDB, std::string fileName);
When you call the function, you use a pointer as first parameter:
void saveRecogniser(std::string fileName)
{
serializeDB(currentVisualDB.get(), fileName); // You said that currentVisualDB is a std::shared_ptr
}
I don't know why your build gets to linking stage, it shouldn't. Maybe you have more than one place where you declare serializeDB?

os kern error : "ld: symbol(s) not found for architecture x86_64"

I have looked all over Stack Overflow and other websites about this famous error, and all of them are very specific, and in my case I cannot find a solution. I am making an ncurses application and when i try to compile it, it causes the following error:
Undefined symbols for architecture x86_64:
"NCRS::End()", referenced from:
_main in crspro-85eaaf.o
"NCRS::Start()", referenced from:
_main in crspro-85eaaf.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I compile the code with the following line:
$ clang++ crspro.cpp -lncurses -o crspro
Here is the code:
crspro.cpp
#include "ncrs.h"
int main(int argc, char* argv[]) {
NCRS::Start();
getch();
NCRS::End();
return 0;
}
ncrs.h
#ifndef NCRS_H
#define NCRS_H
#include <ncurses.h>
#include <string>
typedef std::string string;
class NCRS {
private:
static bool __curses_on;
static bool __buffer;
static bool __echo;
static bool __keypad;
public:
static void Start(bool bbuffer=false, bool becho=false, bool bkeypad=false);
static void End();
};
#endif
ncrs.cpp
#include "ncrs.h"
static void NCRS::Start(bool bbuffer=false, bool becho=false, bool bkeypad=false) {
initscr();
if (bbuffer) raw();
if (becho) echo(); else noecho();
if (bkeypad) keypad(stdscr, TRUE); else keypad(stdscr, FALSE);
__buffer = bbuffer;
__echo = becho;
__keypad = bkeypad;
__curses_on = true;
}
static void NCRS::End() { nocbreak(); echo(); keypad(stdscr, FALSE); endwin(); }
I don't have any issues in the code itself as far as I can tell. I have tried even including ncrs.cpp (The horror!!) but I still get the same problems.
Can anyone help with this issue? I've had this problem before with other projects and I've had to abandon them because I couldn't find a solution.
Thanks to anyone who can help!
_
_
EDIT
compile with:
clang++ crspro.cpp ncrs.cpp -lncurses -o crspro
returns error:
Undefined symbols for architecture x86_64:
"NCRS::__curses_on", referenced from:
NCRS::Start(bool, bool, bool) in ncrs-e52041.o
"NCRS::__echo", referenced from:
NCRS::Start(bool, bool, bool) in ncrs-e52041.o
"NCRS::__buffer", referenced from:
NCRS::Start(bool, bool, bool) in ncrs-e52041.o
"NCRS::__keypad", referenced from:
NCRS::Start(bool, bool, bool) in ncrs-e52041.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Your compilation isn't including anything from ncrs.cpp, which is where both NCRS::Start() and NCRS::End() are defined. You probably want
clang++ crspro.cpp ncrs.cpp -lncurses -o crspro
Or if you want to build the object files separately and then link them:
clang++ -c crspro.cpp -c
clang++ -c ncrs.cpp -c
clang++ crspro.o ncrs.o -lncurses -o crspro
Your next error about "NCRS::__curses_on" is because you're using static variables without defining them you need to add
bool NCRS::__curses_on=false;
bool NCRS::__buffer=false;
bool NCRS::__echo=false;
bool NCRS::__keypad=false;
to one of your .cpp files. (presumably ncrs.cpp is the logical place.)
It's probably worth thinking about whether they should be static (and whether the functions should be static too) - they may need to be, but static class variables are essentially global variables, which will often come back to bite you later. They make it harder to understand the flow of the code, and can make multi-threading and testing painful.

Undefined symbols for architecture x86_64:

interface:
class rmKeyControl {
static map<char, function<char(char)>> sm_function_list;
public:
static bool addKeyAction(char, function<char(char)>);
};
implementation:
bool rmKeyControl::addKeyAction(char key, function<char(char)> func) {
if (!sm_function_list.count(key)) {
sm_function_list.insert(pair<char, function<char(char)>>(key, func));
return true;
} return false;
}
The full error message is:
Undefined symbols for architecture x86_64:
"control::rmKeyControl::sm_function_list", referenced from:
control::rmKeyControl::addKeyAction(char, std::__1::function) in rm_KeyControl.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
This seems to be a standard linker error for Xcode 4, but it seems to occur for all sorts of reasons, and it never elaborates. This error seems to indicate the presence of binary instructions that don't work on the x86_64 architecture, but that doesn't make sense in this context. Why am I getting this error?
Edit: I forgot to mention that rmKeyControl is in namespace control. I am using namespace control; in the implementation, although you cannot see it.
Static member is just declaration. Define it in the implementation/source file like-
// include interface header and then do -
map<char, function<char(char)>> rmKeyControl::sm_function_list;

ld: symbol(s) not found

I'm now studying Fundamentals Of Data Structures in C++ written by Ellis Horowitz, trying to implement the example on the page 77. However, after I build the project, Eclipse Console shows up some warning.
Here's my header file:
#ifndef RECTANGLE_H_
#define RECTANGLE_H_
class Rectangle{
public:
Rectangle();
~Rectangle();
int GetHeight();
int GetWidth();
private:
int xLow, yLow, height, width;
} ;
#endif
And here's my source file:
#include <iostream>
#include "Rectangle.h"
using namespace std;
int main(){
Rectangle r, s;
Rectangle *t = &s;
if(r.GetHeight()*r.GetWidth() > t->GetHeight()*t->GetWidth())
cout << "r";
else
cout << "s";
cout << "has the greater area" << endl;
return 0;
}
And the CDT Build Console shows:
Building target: rectangle
Invoking: MacOS X C++ Linker
g++ -o "rectangle" ./main.o
Undefined symbols:
"Rectangle::Rectangle()", referenced from:
_main in main.o
_main in main.o
"Rectangle::GetWidth()", referenced from:
_main in main.o
_main in main.o
"Rectangle::GetHeight()", referenced from:
_main in main.o
_main in main.o
"Rectangle::~Rectangle()", referenced from:
_main in main.o
_main in main.o
_main in main.o
_main in main.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
make: *** [rectangle] Error 1
**** Build Finished ****
Besides, will binary files automatically created after building the project?
The implementations to your Rectangle methods are really missing.
That are the methods, you see in the linker error messages:
Rectangle::Rectangle()
Rectangle::GetHeight()
Rectangle::GetWidth()
If you have a Rectangle.cpp (or .cc, .cxx) file, than you need to compile this also and link the Rectangle.o file.
since you asked, here a simplified overview, what different filename ending are for:
Rectangle.h is the header file contains the Interface to your class. Usually it's enough if I read and understand this file, to use the classes that are defined there.
Rectangle.cpp is the implementation or source file and contains the implementation. You can put them also in the header but for larger classes this makes the header file more crowded and some other disadvantages (compile time speed, less encapsulation, ...)
Rectangle.o is the object file. This is what the compiler makes out of the header and source file and is used by the linker.
You haven't defined the Rectangle class functions anywhere. Where is Rectangle.c?
The header file simply declares that the class exists, but you have provided no definitions for that class. You need a Rectangle.c that does that. Also you will have to link with Rectangle.o.