I was trying to compile this using my mac terminal
g++ -std=c++20 Recursion.cpp
and i'm getting the following error
Undefined symbols for architecture arm64:
"_main", referenced from:
implicit entry/start for main executable
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
however, i was able to get it to work using the online editor/compiler here.
any idea what might be wrong with my mac terminal?
#include <iostream>
void printBinary(unsignedint n)
{
if (n > 1) // we only recurse if n > 1, so this is our termination case for n == 0
{
printBinary(n / 2);
}
std::cout << n % 2;
}
int main()
{
int x{};
std::cout << "Enter an integer: ";
std::cin >> x;
printBinary(static_cast<unsignedint>(x));
}
(1) Check (from the terminal) that the file Recursion.cpp resides in the directory you are issuing the command in and that it is exactly the same file that you have listed here.
(2) If it's currently residing in an editor make sure that you have actually SAVE'd it!
(3) Remove the "-std=c++20" option in your command; nothing in your code requires anything like that standard.
I know this sounds stupid, but what if you made a new directory and file (called something different than Recursion.cpp), and tried the command again in that directory.
Have you been able to build a 'hello world' program from g++ before?
It thinks you don't have a main function, and there's really no way for us to tell why.
while i still consider myself a beginner, I have been compiling everyday for the past 24 days (i have been quarantined for 21 days since entering China and I have nothing better to do than to study C++ daily)
so it's not my first time compiling a program.
and I'v been making new folders and directories for different exercises (for example, if i'm practising some codes/tutorials from Bucky's , i'll start a new folder named bucky. if i'm trying some codes from learncpp.com, i'll start another directory)
no problem running the command in other directories so far
What happens when you compile but not link the file:
g++ -c Recursion.cpp
Do you create the requisite object file Recursion.o ?
Also, do you have write permission to the directory that you are working in?
Did you copy the example by hand, or cut and paste from the web (with the very small possibility of hidden, non-readable, characters)? If you transferred it from Windows you might need to do whatever the mac equivalent of dos2unix is with the file.
Not that is matters, but why declare x in main as an int and then cast it to an unsigned int to pass into your recursive function? Declare it as an unsigned int to begin with.
#include <iostream>
void printBinary(unsignedint n)
{
if (n > 1) // we only recurse if n > 1, so this is our termination case for n == 0
{
printBinary(n / 2);
}
std::cout << n % 2;
}
int main()
{
unsigned x {}; // unsigned is an 'unsigned int'
std::cout << "Enter an integer: ";
std::cin >> x;
std::cout << '\n';
printBinary(x);
std::cout << '\n';
}
The source you modified declared everything as an int. So the source could be modified later to check the input for zero/negative numbers and reject the input.
Another thing to look out for with recursion is blowing up the stack with a large number of recursed function calls, or excessive processing time.
Using recursion to solve the Fibonacci number for even not-large numbers (around 45) runs into looooooong processing times. 46 is even more laggy, 47 is a snooze-fest. Each single digit increase gets progressive worse.
Another thing to note is using int in a Fibonacci function will result in end results that wrap-around for even "small" numbers. 47 smashes that barrier.
So why not use unsigned long long, the largest native C++ integer type? Even that has limits.
Using recursion to solve the Fibonacci number for even not-large numbers (around 45) runs into looooooong processing times. 46 is even more laggy, 47 is a snooze-fest.
1 2 3 4 5 6 7 8 9 10 11 12
#include <iostream>
unsignedlonglong F[70]{};
unsignedlonglong Fibonacci( unsigned n )
{
return F[n] = F[n] ? F[n] : n <= 1 ? n : Fibonacci( n - 1 ) + Fibonacci( n - 2 );
}
int main()
{
std::cout << Fibonacci( 47 );
}
I guess Fibonacci numbers have been calculated zillion or more times.
Wouldn't it be better to store them in an array and write a simple lookup function ?
@lastchance, using a container as part of a memorization algorithm lookup table is mentioned in the recursion lesson at Learn C++ I linked earlier. Using a static in-function std::vector instead of a global regular array.
My comment about slow and laggy is predicated on creating a Fibonacci recursion function, the way beginners would be taught to write the function, that is simple but time-wise inefficient:
1 2 3 4 5 6
int fibonacci(int count)
{
if (count == 0) return 0;
if (count == 1) return 1;
return fibonacci(count-1) + fibonacci(count-2);
}
Mmm, well you can get the Fibonacci numbers directly without either iteration or recursion anyway. But any recursive algorithm with an R-value of 2 or above ought to shout "try memoisation".
But I hope that the OP will return to tell us the outcome of his compiling/linking on a mac.
I guess Fibonacci numbers have been calculated zillion or more times.
Wouldn't it be better to store them in an array and write a simple lookup function ?
Yes. Small tables for factorials, fibs, the first few primes, and so on are the right way to do it in real code.
This isn't real code, though. This is a school exercise to learn about recursion. Lookup tables is a topic for another day, if they bother to cover it. Professors tend to gloss of the whole concept, convincing students that when they go to work the best way to get a value is to compute it every time rather than just have it and use it. The student often has to learn this on the job, after an experienced peer points out the idiocy of doing anything the school way.
I argue that there is no need for a function. table[value] is fine. If value is unsafe, then I guess the function can validate it and return the value.
I compiled and ran @OP's program using terminal on my macbook without any problems.
The .cpp file was in an arbitrary directory. Both versions of the g++ command produce the same successful result. The a.out file appears in that same directory and is executed via the same terminal instance.
I doubt whether the g++ version has much to do with the problem.
g++ --version
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/4.2.1
Apple clang version 13.0.0 (clang-1300.0.29.3)
Target: x86_64-apple-darwin20.6.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
g++ might not be running because command line tools have not been installed.