Accessing a static atomic data member causes a linker error - c++

How should a static atomic variable be accessed without causing a linker error?
I've reduced my code example to the following:
#include <iostream>
#include <atomic>
class MyClass
{
public:
static std::atomic_bool onOrOff;
};
std::atomic_bool onOrOff(false);
int main(int argc, const char * argv[]) {
std::cout << "It is: " << (MyClass::onOrOff? "on": "off") << "\n";
return 0;
}
This results in the following linker error (MaxOS X 10.11, XCode 7.2):
Undefined symbols for architecture x86_64:
"MyClass::onOrOff", 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)
NB: There are no build errors if I remove the line in main() accessing
MyClass::onOrOff, so I think that rules out problems with compiler options.

This:
std::atomic_bool onOrOff(false);
Should be:
std::atomic_bool MyClass::onOrOff(false);
As it stands you have declared two different onOrOff variables, one inside the class and one outside. And only allocated storage for one.

You forgot that onOrOff is a scoped name to MyClass. You need
std::atomic_bool MyClass::onOrOff(false);
Live Example

Related

C++: Undefined symbols for architecture arm64 ... linker command failed [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 last month.
I have the following files,
my.h
extern int foo;
void print_foo();
void print(int);
my.cpp
#include <iostream>
#include "./headers/my.h"
void print_foo() {
std::cout << foo << '\n';
}
void print(int i) {
std::cout << i << '\n';
}
use.cpp
#include "./headers/my.h"
int main() {
foo = 7;
print_foo();
print(99);
}
building gives the following error,
/> g++ -W -std=c++11 -o output *.cpp
Undefined symbols for architecture arm64:
"_foo", referenced from:
print_foo() in my-e8b938.o
_main in use-318772.o
(maybe you meant: print_foo())
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Tried removing each definition and its references one by one and testing. It compiles fine when
void print(int)
and its references are alone. The other two defintions give a similar error shown above.
My environment: Mac M1 (Monterey), VSCode, g++ v.14
New to C++. What's going on? How do I correct?
extern int foo;
That does not define foo, it just declares it. You need exactly 1 .cpp file with int foo defined somewhere, and optionally initialized.

Xcode C++ extern variable linker error

I am learning to code in C++ and am working in Xcode9.1 on OS X 10.13.1. While trying to understand the use of keyword extern, I encountered the problem that the following code:
extern int foo;
#include <iostream>
int main() {
foo = 7;
std::cout << foo << std::endl;
return 0;
}
results in a linker error when run:
Undefined symbols for architecture x86_64:
"_foo", 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 am not sure why the linker cannot find foo despite the definition being the first line in main.
Thanks very much for looking into my problem!
The linker cannot find foo, because it's not defined anywhere. By declaring extern int foo', you're telling the linker that the definition is somewhere else. Remove extern, or define foo somewhere where the linker can find it.
Have a look at this example on Wikipedia.

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.

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;