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;
}
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:
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
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;
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.