Undefined symbols for architecture x86_64: - c++

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;

Related

C++ Undefined symbols for architecture x86_64 when using xcode

I have the following piece of code:
// Critical section, lock mutex
m_dbMutex.lock();
sqlite3_stmt *statement;
int ret = sqlite3_prepare(m_dbConnection, query, -1, &statement, 0);
if(ret == SQLITE_OK)
{
ret = sqlite3_step(statement);
sqlite3_finalize(statement);
}
m_dbMutex.unlock();
// End of critical section
Where m_dbMutexis a std::mutex object, which is declared in the header in the following way:
static std::mutex m_dbMutex;
For some reason, i can't compile my program because of this.
Full error print:
Undefined symbols for architecture x86_64:
"DBManager::m_dbMutex", referenced from:
DBManager::runQuery(char const*) in DBManager.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 tried adding libc++.tbd and libstdc++.tbd to my project but it still won't compile.
Any suggestions?
EDIT:
It seems this happens when the mutex is declared static.
I don't get this error when it's not.

Initializing AudioDeviceManager in JUCE Audio App

I am trying to initialize the instance of AudioDeviceManager, in the constructor of my MainContentComponent:
MainContentComponent(): deviceManager (getSharedAudioDeviceManager())
where
AudioDeviceManager& getSharedAudioDeviceManager()
{
if (sharedAudioDeviceManager == nullptr)
{
sharedAudioDeviceManager = new AudioDeviceManager();
sharedAudioDeviceManager->initialise (2, 2, 0, true, String::empty, 0);
}
return *sharedAudioDeviceManager;
}
and my declarations would be:
AudioDeviceManager& deviceManager;
static ScopedPointer<AudioDeviceManager> sharedAudioDeviceManager;
There are no compile errors, but I have runtime errors, wherein the compiler tells me:
Undefined symbols for architecture x86_64:
"MainContentComponent::sharedAudioDeviceManager", referenced from:
MainContentComponent::getSharedAudioDeviceManager() in MainComponent.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Totally lost. Help!
You need to define your static member outside of the class declaration, like this...
ScopedPointer<AudioDeviceManager> MainContentComponent::sharedAudioDeviceManager;
Also, this is a linktime error, not a runtime error.

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.

C++ templates not defined in header / linker error [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why can templates only be implemented in the header file?
I just came across something that I failed to understand.
I had a problem at the linking stage in the following case.
//header file
class A
{
template<class T>
std::weak_ptr<T> GetSomethingFromSomeWhere(const char* Id);
};
//cpp file
template<class T>
std::weak_ptr<T> A:GetSomethingFromSomeWhere(const char* id)
{
//A method with the right stuff inside and the right return statement
...
}
//Another class
class B
{
};
//main.cpp
int main ()
{
A a;
auto pB = a.GetSomethingFromSomeWhere<B>( "id" );
}
This didn't compile, during linking I have something of this kind :
Undefined symbols for architecture x86_64:
"std::__1::weak_ptr A::GetComponentFromName(char const*)", referenced from:
_main 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)
I fixed it by defining the template method directly in the header file.
Should I always define template method in the header ? Why ?
I am on OSX and use clang++ with XCode if that can be of any help
Thanks
Template definition needs to be visible to the code using it. Otherwise linker errors will be generated.
There are different workarounds for situations like that:
Read This

C++ does not compile (link?) in XCode 4.2

I have a very small program in Xcode only displaying a label and changing the text of the label in the viewWillAppear method. The label.text should come from a C++ library with a function like this:
int getNumber(){
return 42;
}
The problem is, that including the class with #import "TestLibMain.h" in my *.mm(!) class and using the function with
TestLibMain *tlb = new TestLibMain();
int myInt = tlb->getNumber();
NSString *myString = [NSString stringWithFormat:#"%d",myInt];
doesn't invoke a compiler error, but a linker error:
Undefined symbols for architecture i386:
"TestLibMain::getNumber()", referenced from:
-[tbViewController buttonPressed:] in tbViewController.o
ld: symbol(s) not found for architecture i386
clang: error: linker command failed with exit code 1 (use -v to see invocation)
My impression is, that the cpp-class has not been compiled.
I tried a lot of things around, but somewhere it's hanging. It's all in the same directory, I use the .mm extension, everything fine, but always this linker error. Getting crazy :-)
Mac OS X Lion, XCode 4.2
Any ideas?
int getNumber() {
return 42;
}
in a .cpp or .mm defines getNumber() as a free function. This:
int TestLibMain::getNumber() {
return 42;
}
defines getNumber() as a member of TestLibMain.