Undefined symbols str::string and string literals [duplicate] - c++

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 2 years ago.
I am learning C++ and stuck with what seems to be a super simple thing. :-(
Here is my Person.h
#include <string>
class Person {
private:
std::string firstName;
std::string lastName;
int arbitaryNumber;
public:
Person(std::string firstName, std::string lastName, int arbitaryNumber);
std::string getName();
};
My CPP file is:
#include "Person.h"
Person::Person(std::string firstName, std::string lastName, int arbitaryNumber):
firstName(firstName), lastName(lastName), arbitaryNumber(arbitaryNumber) {
}
std::string Person::getName() {
return this->firstName + " " + this->lastName;
}
So far things are super simple. Let's used it in the main.
#include <iostream>
# include "Person.h"
int main(int argc, char **argv) {
Person p("Nawa", "Man", 100);
return 0;
}
When I compile/run my code, I got this error.
Building in: /Users/nawa/eclipse-workspace-CPP/ExampleMake/build/default
make -f ../../Makefile
g++ -c -O2 -o ExampleMake.o /Users/nawa/eclipse-workspace-CPP/ExampleMake/ExampleMake.cpp
g++ -o ExampleMake ExampleMake.o
Undefined symbols for architecture x86_64:
"Person::Person(std::__1::basic_string<char, std::__1::char_traits<char>,
std::__1::allocator<char> >, std::__1::basic_string<char, std::__1::char_traits<char>,
std::__1::allocator<char> >, int)", referenced from:
_main in ExampleMake.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: *** [ExampleMake] Error 1
Build complete (2 errors, 0 warnings): /Users/nawa/eclipse-workspace-
CPP/ExampleMake/build/default
What is wrong with the constructor? I am using Eclipse on Mac.
I suspected that it has something to do with string literal and str::string but I am not sure. So, when I compiled it in the command line, I got the same error so this does not seems to be Eclipse problem.
Please help.
Thanks.

g++ -o ExampleMake ExampleMake.o
You are linking just one file, when you have 2 cpp files.

Related

Why am I getting "ld: symbol(s) not found for architecture x86_64" [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 2 years ago.
I am a complete beginner to c++. I am learning c++ through the object oriented programming Data structures in c++. In the course I have the following program
Cube.h
#pragma once
class Cube {
public:
double getVolume();
double getSurfaceArea();
void setLength(double length);
private:
double length_;
};
Cube.cpp
#include "Cube.h"
double Cube::getVolume() {
return length_ * length_ * length_;
}
double Cube::getSurfaceArea() {
return 6 * length_ * length_;
}
void Cube::setLength(double length) {
length_ = length;
}
main.cpp
#include <iostream>
#include "Cube.h"
int main() {
Cube c;
c.setLength(3.48);
double volume = c.getVolume();
std::cout << "Volume" << volume << std::endl;
return 0;
}
When I make this program with make main, I get the following error message
c++ main.cpp -o main
Undefined symbols for architecture x86_64:
"Cube::getVolume()", referenced from:
_main in main-6c5fe0.o
"Cube::setLength(double)", referenced from:
_main in main-6c5fe0.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: *** [main] Error 1
Not sure what I am doing wrong. I am working from a macbook. I followed this link to run cpp programs in mac.
Not sure what I am doing wrong. An explanation to the error will also be nice.
Thanks in advance
You need to link main.cpp and Cube.cpp together, so you have to compile with:
c++ main.cpp Cube.cpp -o main

Unable to properly reference class from another namespace

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?

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?

linker cannot find a C++ static member [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 7 years ago.
When i write simple C++ code in X-code, it shows Linker Error.
Undefined symbols for architecture x86_64:
"Emp::id", referenced from:
Emp::Emp() 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)
#include <iostream>
using namespace std;
class Emp
{
public:
static int id;
int sal;
Emp()
{
Emp::id =10; // When i comment this line its working fine.
};
};
int main(int argc, const char * argv[])
{
Emp Ram;
cout << Ram.sal ;
return 0;
}
You have declared id as a static variable. You then set it in every constructor call, which is probably not what you want to do.
For a 'fix', you can add the following line above main:
int Emp::id = 0;
However, you may not want that to be static. For more information on static class variables, see this page

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.