I need to use a C++ singleton in ViewController,and got error.
TradeAPIManager.h
#include <stdio.h>
class TradeAPIManager{
public:
static TradeAPIManager * getInstance();
void test();
private:
TradeAPIManager(){}
static TradeAPIManager *m_pInstance;
};
TradeAPIManager.cpp
#include "TradeAPIManager.h"
TradeAPIManager *TradeAPIManager::m_pInstance = nullptr;
TradeAPIManager *TradeAPIManager::getInstance(){
if(m_pInstance == nullptr){
m_pInstance = new TradeAPIManager();
}
return m_pInstance;
}
void test(){
printf("asdasd");
}
ViewController.mm
- (void)viewDidLoad {
[super viewDidLoad];
TradeAPIManager *manager = TradeAPIManager::getInstance();
manager->test();
}
when build,i got error like this:
Undefined symbols for architecture x86_64:
"TradeAPIManager::test()", referenced from:
-[ViewController viewDidLoad] in ViewController.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Undefined symbols for architecture x86_64 in simulator
Undefined symbols for architecture arm64 in my iPhone
thats not a .a lib or a framework,just a cpp file write by myself
I tried six hours but have no way out.
hope anyone can help
many thanks
Related
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
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?
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.
I have C++ project in Xcode
I would like to use C library aws4c in it https://code.google.com/p/aws4c/
Here is my apn.cpp file:
#include "aws4c.h"
...
int main(int argc, char *argv[])
{
aws_init();
...
}
In Xcode it looks OK, but when I'm trying to build - get error
Undefined symbols for architecture x86_64:
"aws_init()", referenced from:
_main in apn.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
What is the right way to link C library to C++ project in Xcode5?
Resolved this
added
#ifdef __cplusplus
extern "C" {
#endif
and
#ifdef __cplusplus
}
#endif
to aws4c.h file and it works
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;