You can make a difference in the Apple Support Community!

When you sign up with your Apple Account, you can provide valuable feedback to other community members by upvoting helpful replies and User Tips.

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Problem compiling .cpp program with gcc

Hi all,

I would like to compile a .cpp (main.cpp) program with my MacBook Air M1, with gcc, with the command:


gcc main.cpp


But, even with an "hello world" program I am not able to do that for this error (complete error log in the file):


ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)


What I've noticed is that if I try to compile a .c program it doesn't give me an error and everything goes well.


Details about gcc version:

gcc --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-darwin21.1.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin


I don't know what to do. It would be great if someone will answer me. Thanks in advance.

MacBook Air (2020 or later)

Posted on Nov 2, 2021 1:33 AM

Reply
Question marked as Top-ranking reply

Posted on Nov 2, 2021 4:26 AM

In Apple's Command Line tools, gcc is not GNU C, but rather a link to clang for legacy purposes. Just compile your C++ program as:


clang++ -O -o foo main.cpp [-stdlib=libc++ ] [-std=c++17 ]
./foo



Implicitly, the above command finds and uses the standard C++ library without you needing to involve either of the [ ] options.


If you want to see the list of supported C++ standards, either look at the clang man page or:


clang++ main.cpp -std=foo


Similar questions

3 replies
Question marked as Top-ranking reply

Nov 2, 2021 4:26 AM in response to jack_9862

In Apple's Command Line tools, gcc is not GNU C, but rather a link to clang for legacy purposes. Just compile your C++ program as:


clang++ -O -o foo main.cpp [-stdlib=libc++ ] [-std=c++17 ]
./foo



Implicitly, the above command finds and uses the standard C++ library without you needing to involve either of the [ ] options.


If you want to see the list of supported C++ standards, either look at the clang man page or:


clang++ main.cpp -std=foo


Problem compiling .cpp program with gcc

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.