How to use g++ in terminal in mac? - xcode

I've installed Xcode and I can compile .cpp files in Xcode. However, I wanna use g++ in Terminal but command not found: g++ now.
My OS is Mountain Lion Xcode version is 4.4.

You need to download and install the Command Line Tools for OSX Mountain Lion from Apple Developer (you'll need a Apple ID which is free to sign up for). This will install GCC (including g++) so you can build direct from Terminal
The exact package currently is Command Line Tools (OS X Mountain Lion) for Xcode - September 2012
Description about the package from Apple:
This package enables UNIX-style development via Terminal by installing command line developer tools, as well as Mac OS X SDK frameworks and headers. Many useful tools are included, such as the Apple LLVM compiler, linker, and Make. If you use Xcode, these tools are also embedded within the Xcode IDE, and can be installed on your system using the Downloads preferences pane within Xcode 4.5.

Note that when you run "g++" with Xcode 4.5 installed, you're picking up llvm-g++-4.2 (based on gcc 4.2.1) -- a provided, but no longer supported compiler on the platform. I'd strongly recommend using clang++ instead, the actively supported/developed compiler on the platform.

Related

Compile with gcc on Macos 10.14.5 that will support earlier versions of Mac

I have a c ++ code that I wrote that uses almost nothing, which is not the language itself (except using osascript).
After compiling it on my operating system version (10.14.5) with the following flags:
D_DEBUG, Os, Wall, Wextra
I found that it was not running on older operating systems (in 10.13 for example - it raises an error that it can only run on 10.14 or later)
What are the right flags to use to tell GCC, that I want to support as many MacOs versions as possible?
You will need a versioning flag: -mmacosx-version-min=10.9
And let the compiler know where the SDK is: -isysroot= =/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk
The older SDKs are available in older xcode bundles, or via https://github.com/phracker/MacOSX-SDKs/releases. You need to decompress the SDK and place the folder inside the xcode bundle, as shown in the sysroot flag above.
Apple sayeth: You must target your compile for the oldest version of OS X on which you want to run the executable. In addition, you should install and use the cross-development SDK for that version of OS X. For more information, see SDK Compatibility Guide

Xcode Command-Line Tools & Make

I have installed Xcode 5 on OS X Mavericks. I have a problem with the command-line tools (cc et al).
The version of cc in /usr/bin is outdated, so I’m not using it. The version of cc embedded deep within Xcode’s application bundle is current, so I’ve pasted the following code into my shell profile:
export PATH=`xcode-select -print-path`/usr/bin:`xcode-select -print-path`/Toolchains/XcodeDefault.xctoolchain/usr/bin:${PATH}
This worked under Xcode 4 on Mountain Lion. Now, however, while the compiler seems to run fine when invoked as cc, it won’t compile anything when I invoke it that way; it apparently cannot find system headers (e.g. stdio.h). When I invoke the compiler as xcrun cc, everything works just fine. Unfortunately, this requires me to patch the inputs for each and every build-automation program (such as make) before they will run properly.
The other problem is that I cannot find the command-line tools for download from ADC. As I upgraded from Mountain Lion, invoking /usr/bin/cc does not ask me if I want to download the tools; it just runs the (outdated) copy of cc from Xcode 4.
What is the difference between cc and xcrun cc, and is there anything I can do that will cause cc to work properly from the Terminal (or automated build tools)?
On OS X 10.9 Mavericks, run xcode-select --install to update the installed command line tools so they match the version inside of Xcode. This also installs header files into /usr/include and /System/Library and installs additional development libraries. This is a change from previous versions of OS X where the command line tools were either installed via Xcode.app itself or by an Xcode installer. It is still possible to download a standalone installer from the Apple Developer site but should normally no longer be necessary on 10.9.
I have no problems with cc (although you will get better results if you use clang instead of cc. I suggest that you remove the program, download it again, and install the command line tools with xcode-select --install

Desperately trying to build open source tools (octave) on a Mac 10.6 Snow Leopard (involves Xcode, gcc, fink)

The ironic thing is that all this used to work on my Mac, but Apple no longer supports 10.5.8, so I was forced to update to Snow Leopard, 10.6. And everything broke (thank you Apple).
On the surface, it seems simple. Build an open source package like octave under Snow Leopard (Mac OS X 10.6)
Apple has made this difficult, to say the least. They no longer download XCode for older operating systems unless you are a paid developer. My Macbook pro 2.16Ghz cannot load Lion, so that option is not available.
My old fink doesn't work because it was old. In order to build a new one, I need Xcode 3.2, which I can't get (see above).
I downloaded a free gcc 4.2, and it works fine.
So in order to try to build octave, it's the old style gnu install:
./configure
make
make install
./configure fails because there is no fortran installed. That's a special case because the install of gcc didn't include fortran. So a fallback would be building a complete gcc which I have done in the past.
downloaded gcc 4.9:
gcc-4.9-20130728
inside, gcc49
gcc can't build because it needs the three subsidiary packages gmp, mpfr and mpc
I am now trying to build these, so that I can bootstrap a complete gcc build, but in the meantime, is there any simpler way to bootstrap these things? I find it hard to understand why no binaries are available for:
fink
octave
which would solve part of my current problems.
You can still get XCode. You just have to be registered on Apple Developer, but you do not have to pay for the license. You then download it through the Mac App store, or you can get a link that opens it in the App store here. Finally, you have to install the command line tools from within Xcode. These can be found under the Components tab of the Download Preferences panel.
Let me know if that does not help. My iMac running 10.6 is currently in for repairs, so I am on my 10.7 laptop and cannot test all the specifics yet.

Can't use GCC in OS X Terminal

I have installed the developer tools. I can compile code via Xcode and according to the docs /usr/bin/gcc & /usr/bin/cc should point to /usr/bin/gcc-4.0. Neither the symlinks or gcc-4.0 exist on my system (Snow Leopard). All I wish to do is compile some C on the terminal! I'm amazed by how complicated this task is. The command GCC is unsurprisingly returning "gcc: command not found".
Can anyone shed some light on this?
No! Reinstalling is the wrong answer!
Newer versions of XCode require you to install the command-line versions of the tools separately. In XCode, go to Preferences | Downloads, check the Components tab, and install them from there:
These binaries should be there after installing xcode. Check your path settings, and if you still don't have these reinstall xcode
Command line tools are no longer included with the latest XCode (even as an add on). Now you can download them here:
https://developer.apple.com/downloads/index.action
Did you install the latest Xcode after installing Snow Leopard, or is this the version of Xcode from a previous OS installation?
On my system with a clean Snow Leopard install the default compiler (and the one symlinked from cc and gcc) is gcc-4.2
I do have gcc-4.0 too, although it's not the default.

Using 'make' on OS X

I have a MacBook Pro that I'm trying to do some development on.
I have a program I want to build, and when I went to use make to build it, I got a "command not found" error. I did some googling and Stack Overflow searches and it doesn't look like this is a common problem. Why don't I have make installed and how do I get it?
I'm extra confused, because I know I used it relatively recently (in the past month or so) when I was on this laptop.
For those of you who get to this page using Xcode 4.3 and Lion, the command line tools are no longer bundled by default, and there is no /Developer anymore. To install them, open Xcode, go to Preferences -> Downloads -> Components -> Command Line Tools. This should install make, gcc etc.
Have you installed the Apple developer tools?
What happens if you type gcc -v ?
It look as if you do not have downloaded the development stuff. You can get it for free (after registration) from http://developer.apple.com/
There is now another way to install the gcc toolchain on OS X through the osx-gcc-installer this includes:
GCC
LLVM
Clang
Developer CLI Tools (purge, make, etc)
DevSDK (headers, etc)
The download is 282MB vs 3GB for Xcode.
You will have to install the "Developer Tools" that are provided as optional packages in OS X installation disks.
For Xcode 4.1 you can simply add /Developer/usr/bin to the PATH environment variable. This is easily done:
$ export PATH=$PATH:/Developer/usr/bin
Also be certain to update your ~/.bashrc (or ~/.profile or ~/.bash_login) file.
In addition, if you have migrated your user files and applications from one mac to another, you need to install Apple Developer Tools all over again. The migration assistant does not account for the developer tools installation.
If you've installed Xcode 4.3 and its Command Line Tools, just open Terminal and type the following: On Xcode 4.3, type the following in Terminal:
export PATH=$PATH:/Applications/Xcode.app/Contents/Developer/usr/bin
#Daniel's suggestion worked perfectly for me. To install make, open Xcode, go to Preferences -> Downloads -> Components -> Command Line Tools.You can then test with gcc -v
I agree with the other two answers: install the Apple Developer Tools.
But it is also worth noting that OS X ships with ant and rake.
I believe you can also get just the Xcode command-line tools which is about 170 MB.. It's described in the 'brew' setup guide: https://github.com/mxcl/homebrew/wiki/installation and can be found here: https://developer.apple.com/downloads/index.action#
Edit: this was already mentioned above by #josh
I found the Developer Tools not as readily available as others. In El Capitan, in terminal I just used gcc -v, it then said gcc wasn't available and asked if I wanted to install the command line Apple Developer Tools. No downloading of Xcode required. Terminal session below:
Pauls-MBP:~ paulhillman$ gcc -v
xcode-select: note: no developer tools were found at '/Applications/Xcode.app', requesting install. Choose an option in the dialog to download the command line developer tools.
Pauls-MBP:~ paulhillman$ gcc -v
Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 7.3.0 (clang-703.0.31)
Target: x86_64-apple-darwin15.6.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin

Resources