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
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 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.
This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
static variable link error [duplicate]
(2 answers)
Closed 3 years ago.
I am not able to use structures for static variable and function. Could anyone explain why exactly is this behavior?
#include <iostream>
using namespace std;
class Foo
{
private:
typedef struct
{
int bee_1;
}bee;
static bee test;
public:
static void Inc(){ test.bee_1++;}
static int getBee_1(){return test.bee_1;}
};
int main(){
Foo::Inc();
Foo::Inc();
Foo::Inc();
Foo::Inc();
cout << Foo::getBee_1();
return 0;
}
I get the following error when I use this code:
clang++-7 -pthread -o main main.cpp
/tmp/main-521333.o: In function `Foo::Inc()':
main.cpp:(.text._ZN3Foo3IncEv[_ZN3Foo3IncEv]+0x24): undefined reference to `Foo::test'
main.cpp:(.text._ZN3Foo3IncEv[_ZN3Foo3IncEv]+0x2e): undefined reference to `Foo::test'
/tmp/main-521333.o: In function `Foo::getBee_1()':
main.cpp:(.text._ZN3Foo8getBee_1Ev[_ZN3Foo8getBee_1Ev]+0x7): undefined reference to `Foo::test'
clang: error: linker command failed with exit code 1 (use -v to see invocation)
compiler exit status 1
This question already has answers here:
Undefined reference to a static member
(5 answers)
Closed 6 years ago.
I am coding in c++ and I am attempting to learn about static variables.
When I wrote my practice code, I got this error message:
Undefined symbols for architecture x86_64:
"pizza::firstLetterFavPizza", referenced from:
pizza::favPizzaFirstLetterChan(char) 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)
Please help! I don't know what's wrong. The source code is here:
#include <iostream>
class pizza
{
public:
static char firstLetterFavPizza;
char favPizzaFirstLetterChan (char letter = firstLetterFavPizza)
{
pizza::firstLetterFavPizza = letter;
return pizza::firstLetterFavPizza;
}
};
int main()
{
pizza *a = new pizza();
pizza *b = new pizza();
std::cout << a->favPizzaFirstLetterChan('c') << std::endl;
delete a;
std::cout << b->favPizzaFirstLetterChan('b') << std::endl;
delete b;
return 0;
};
You have declared static data member, but not defined it. Add a definition to your code somewhere (in the global namespace):
char pizza::firstLetterFavPizza;
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
This question already has answers here:
Undefined reference to static class member
(9 answers)
Closed 9 years ago.
I have the following code:
class employee {
public:
static int last_id;
...
};
int main() {
employee::last_id=0;
}
When i try to run it it gives the following error:
Undefined symbols for architecture x86_64:
"employee::last_id", referenced from:
_main in chap7-F3IpS1.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
[Finished in 0.3s with exit code 1]
int employee::last_id=0;
int main() {
[...]
}
You only declared the static data member but not defined it. Write before main in the global namespace
int employee ::last_id;
It will be initialized by zero though your explicitly can specify the initializer.