Not compiling properly using G++

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?

i got the following code from
https://www.learncpp.com/cpp-tutorial/recursion/ (thanks GeorgeP)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

#include <iostream>

void printBinary(unsigned int 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<unsigned int>(x));
}
Last edited on
(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.
Last edited on
> g++ -std=c++20 Recursion.cpp

Get into the habit of using a robust set of compiler options.

Use this as a starter set: g++ -std=c++20 -O3 -Wall -Wextra -pedantic-errors Recursion.cpp
i have tried compiling using
g++ -std=c++20 -O3 -Wall -Wextra -pedantic-errors Recursion.cpp

as recommended, also removed "-std=c++20" option, doesn't work either.

if it helps, here's the version of my g++

% g++ --version
Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/4.2.1
Apple clang version 12.0.5 (clang-1205.0.22.9)
Target: arm64-apple-darwin20.4.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin

i tried googling for solutions but couldn't find any relevant leads...

would you have any further advice pls?

in the worst-case scenario, i could start using a visual basic IDE (by running windows via parallel on my M1 macbook air) or use CLION ...
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.
Hi Ganado,

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.
Last edited on
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>

void printBinary(unsigned int 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.
George P wrote:
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>
unsigned long long F[70]{};

unsigned long long 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 );
}
Last edited on
That's cheatin'.
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.
Last edited on
thanks lastchance, for reminding me to update.


i haven't been able to solve the compiler problem on my macbook air's terminal


i could, however, get it compiled well on various online compilers

Additionally, i downloaded Clion and i could get it to compile properly locally on my macbook.

in other words, my mac's terminal is using a g++ compiler that has some inherent problems?

in other words, my mac's terminal is using a g++ compiler that has some inherent problems?


% g++ --version
Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/4.2.1
Apple clang version 12.0.5 (clang-1205.0.22.9)
Target: arm64-apple-darwin20.4.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin


g++ is a front end that can use various candidate compilers. For example on Windows it might use a mingw compiler.

It looks like the problem you are having is that g++ is using Apple Clang, which is way behind the capabilities of ordinary clang and g++.

Presumably CLion has it's own (up to date?) compiler.

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.

See for example: http://www.edparrish.net/common/macgpp.php

If all else fails install Xcode even if you don't intend ever using it and fudge the result so the terminal-way works.

Topic archived. No new replies allowed.