Undefined Symbols For Architecture x86 [duplicate] - c++

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;

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.

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

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

c++ Mac Xcode: Undefined symbols for architecture x86_64:

I am running the following code in c++ in Xcode on Mac
int fibo(int x)
{
if (x==1||x==2)
return 1;
else
return fibo(x-1)+fibo(x-2);
}
and receiving this error cannot know why.
undefined symbols for architecture x86_64:
"_main", referenced from:
implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Can somebody help me with?
You need to define a main function. That's first function that gets called to "start" your program.
Add this to your file:
int main()
{
fibo(10); // calls your function with
}
You should to implement main() function.
The main function is called at program startup after initialization of
the non-local objects with static storage duration. It is the
designated entry point to a program that is executed in hosted
environment (that is, with an operating system). The entry points to
freestanding programs (boot loaders, OS kernels, etc) are
implementation-defined.
http://en.cppreference.com/w/cpp/language/main_function
#include <iostream> // for std::cout
int fibo(int x)
{
if (x==1||x==2)
return 1;
else
return fibo(x-1)+fibo(x-2);
}
int main()
{
int x = 1;
int result = fibo(x);
std::cout << "Result: " << x; // Printing result
return 0;
}

C++ class static member initialisation [duplicate]

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.