So I am a new programmer and I just installed XCode on my Macbook to get the GCC. I think Xcode is the only way for getting GCC on OSX. Now when I run my Hello World application, in C++, g++ comes up saying it is version 4.0.1 but when I look for commands starting with g I also see g++-4.2. Is there any way of making 4.2 default rather than 4.0.1, and also is there a way to updating gcc to the latest version 4.4.0?
EDIT: Ok, so I installed macports and installed gcc4.4 and it shows up on terminal as gcc-mp-4.4 and how do I make it default with gcc_select, like what are the commands and stuff. Thanks.
If you install macports you can install gcc select, and then choose your gcc version.
/opt/local/bin/port install gcc_select
To see your versions use
port select --list gcc
To select a version use
sudo port select --set gcc gcc40
I know it is an old request. But it might still be useful to some. With current versions of MacPorts, you can choose the default gcc version using the port command.
To list the available versions of gcc, use:
$ sudo port select --list gcc
Available versions for gcc:
gcc42
llvm-gcc42
mp-gcc46
none (active)
To set gcc to the MacPorts version:
$ sudo port select --set gcc mp-gcc46
I'm just dropping in to say that using a soft link to accomplish this is a terrible, no-good, horrible idea.
One of the key things about writing software is reproduceability - you want to be able to get the same results every time. These systems are so complex that you want to reduce all invisible sources of error.
Having a soft link is an invisible source of error. It's the sort of thing you'll forget in a month, then move to a different machine, and wonder why you are getting different results - or, you'll try to upgrade your system, and you'll get weird errors because it's not expecting a softlink there.
Moreover, this isn't guaranteed to work - in particular, it's not clear that you will get the correct system include files, which have certainly changed between iterations of gcc.
gcc_select is a systematic way of doing the same thing which will work predictably, or in the very worst case you can file a bug report and get an eventual fix or fix it yourself.
Unfortunately :-( gcc_select does not affect which compiler XCode uses so it's not the way to go if you need to work in XCode (which I do). I still don't know what that way might be.
The following recipe using Homebrew worked for me to update to gcc/g++ 4.7:
$ brew tap SynthiNet/synthinet
$ brew install gcc47
Found it on a post here.
use "gcc_select -l"
>
gcc_select -l
gcc40 mp-gcc44
>
gcc_select mp-gcc44
You can have multiple versions of GCC on your box, to select the one you want to use call it with full path, e.g. instead of g++ use full path /usr/bin/g++ on command line (depends where your gcc lives).
For compiling projects it depends what system do you use, I'm not sure about Xcode (I'm happy with default atm) but when you use Makefiles you can set GXX=/usr/bin/g++ and so on.
EDIT
There's now a xcrun script that can be queried to select appropriate version of build tools on mac. Apart from man xcrun I've googled this explanation about xcode and command line tools which pretty much summarizes how to use it.
in /usr/bin type
sudo ln -s -f g++-4.2 g++
sudo ln -s -f gcc-4.2 gcc
That should do it.
You can install your GCC manually
either through
sudo port install gcc46
or your download the source code from one of the mirrors from here for example here
tar xzvf gcc-4.6.0.tar.gz
cd gcc-4.6.0
./configure
make
well if you have multiple version, then through you can choose one
port select --list gcc
remember port on mac is called macport https://www.macports.org/install.php and add add the bin into your path export PATH=$PATH:/opt/local/bin
Whatever Apple ships as the default gcc in xcode (4.2.1 on 10.6, 4.0.1 before) is well tested (and maintained) by the apple guys and the "standard" to build software with on OS X. Everything else is not, so think twice if you want to develop software, or be gcc/OS X beta tester.
Related
How do I install the latest version of GCC on my macOS Big Sur. I am using Visual Studio Code, version 1.60 and I want to run C++ programs using it. I tried using homebrew to install GCC but it kept on giving me errors.
E.g.: When I typed in the path as /opt/homebrew/Cellar/gcc/11.2.0/bin
Result: zsh: permission denied: /opt/homebrew/Cellar/gcc/11.2.0/bin
What is wrong with the permission? How will I make it allow.
Firstly, /opt/homebrew/Cellar/gcc/11.2.0/bin is a directory, so you can't run that.
Secondly, homebrew generally makes symbolic links in /usr/local/bin for everything it installs, so you should add that to your PATH, e.g.
export PATH=/usr/local/bin:$PATH
Then you need to look in /usr/local/bin to see what program name you need, e.g.
ls /usr/local/bin/gcc*
and if you see gcc-11 in there, you then need to compile with:
gcc-11 program.c
Note you will need to look for g++* if you actually mean C++ rather than C.
I am running centos 6 on a cluster. I installed the latest gcc-8.2.0. and made a link "ln -sf /usr/bin/gcc-8.2 gcc".
I did the same for g++ and gfortran.
I wanted to reinstall gcc-8.2.0 and went ahead to
make clean
in the gcc-8.2 directory.
When I try
./configure
I get that C compiler cannot create executables
The links I made are broken.
The system gcc-4.4.7 cannot be found
which gcc
gives no gcc
sudo yum install gcc gcc-c++
gives gcc is already installed.
I tried to install an rpm, which fails because of dependencies.
I have pg compilers installed in /opt/pgi
When I configure with
CC=/path to/pgi/bin/pgcc FC=/path to/pgi/bin/pgfortran ./configure
I still get C compiler cannot create executables
I tried the following c++ programm
#include <iostream>
using namespace std;
int main() {
cout << "Hello world!" << endl;
return 0;
}
With the command
/opt/pgi/linux_86_64/12.08/bin/pgcpp hello.cpp -o hello
It gives compilation error that float.h not found. On another linux PC with working gcc, the program works with the command
g++ hello.cpp -o hello
I will appreciate any assistance to either find the systemgcc or use pg compilers to compile gcc if possible
I admit it is a big mess which will require OS reinstallation and reconfiguration. But then I did
sudo yum install compat-gcc-34
Now I have gcc34 and configure of gcc-8.2 goes through without "c compiler cannot create executables". (Note that the ./configure referred to in earlier post was actually
../gcc_8_2_release/configure
inside "gcc_8_2_release_build, so gcc was not being built in its source directory.
The problem I have now is with make, which needs g++, giving error
uint_t(64) or int_t(64) not found.
Thanks all who have gone through this post, for your patience.
Any assistance will be appreciated.
Here is how I got out of this mess.
With the following two commands
sudo yum install compat-gcc-34-c++
sudo yum install compat-gcc-34-g77
I was able to install the older version of gcc, c++ and g77. Then I was able to build gcc-8.2.
Now I have a functional system with the latest gcc, yes it may need re-installation/re-configuring but it is fully functional.
I have learnt a lot and very much appreciate the comments and guidelines of #Basile. However, at one point he was rather negative and discouraging.
But thanks to my belief and perseverance, and more importantly browsing the knowledge shared by others, I have been able to recover what I was beginning to be convinced was a lost cause.
Thanks all.
This is more a sysadmin question than a programming one.
My recommendations:
don't mess your /usr/bin/. Leave your package manager yum to fill it -and never add anything inside it without yum ; so remove manually any symlinks you made there (by mistake)
reinstall the old system gcc 4.4 and g++ 4.4 (using yum)
rebuild your GCC 8 from scratch from its source code. Configure it with --program-suffix=-8 (but no --prefix, or a --prefix=$HOME/soft/ if you don't have root access). So it will install /usr/local/bin/gcc-8 and /usr/local/bin/g++-8 etc... (or, if you have given --prefix=$HOME/soft/ , a $HOME/soft/bin/gcc-8 etc...)
create a $HOME/bin/ if you don't have already one
be sure to have $HOME/bin/ early in your $PATH (before /usr/bin/)
add a symlink ln -sv /usr/local/bin/gcc-8 $HOME/bin/gcc and likewise for g++ etc..
Then, when you type gcc you are getting that symlink to /usr/local/bin/gcc-8 etc.
If you cannot write to /usr/local/ (e.g. because you don't have root permission...) you could pass --prefix=$HOME/soft/ to GCC 8 .../configure then replace /usr/local/ above with $HOME/soft/
If you are the sysadmin and can write to /usr/local/ and have to set up things for many users: add a symlink ln -s /usr/local/bin/gcc-8 /usr/local/bin/gcc etc and ask your users to put /usr/local/bin/ in their $PATH before /usr/bin/
BTW, notice that it is explicitly documented that GCC 8 (or others) need to be built outside of its source tree: in Installing GCC you can read:
First, we highly recommend that GCC be built into a separate directory from the sources which does not reside within the source tree.
(the "highly recommend" should be considered as a polite way to say "you absolutely should")
So your ./configure was another mistake.
It could happen that you messed up your system more seriously than you thought (and perhaps you need to reinstall, or to call Redhat support).
PS. I don't know Redhat (used it only in the previous century). My favorite distro is Debian/testing or Debian/unstable (and my computers are desktops, not clusters).
This problem was solved by the following commands
sudo yum install compat-gcc-34-c++
sudo yum install compat-gcc-34-g77
Once this older version of gcc is installed, the latest version, gcc-8.2 was built and the system is no longer messed terribly. It is very healthy and fit.
I installed gcc 4.6. from macports (for support of C++0x). But when I check the 'gcc --version` it is showing older version. How to use the newer gcc installed by macports?
You can control the symlink in /opt/local/bin/gcc by using port select. You can see available version using port select --list gcc. Anything listed with mp- as prefix refers to MacPorts' own port, gcc42 and llvm-gcc42 refer to the compilers shipped with Xcode by Apple.
Example from my system:
$ port select --list gcc
Available versions for gcc:
gcc42
llvm-gcc42
mp-gcc45
none (active)
$ sudo port select --set gcc mp-gcc45
Password:
Selecting 'mp-gcc45' for 'gcc' succeeded. 'mp-gcc45' is now active.
After that, either open a new terminal window or issue hash -r to make bash recognize the change.
I remember it being something like g++-mp-4.6. I believe it's enough to set the environment variable CXX to that.
Just make sure macports' path comes first in your $PATH. Or use gcc-mp-4.6 or something like that.
I've got NetBeans 6.9 installed on a Mac OS Snow Leopard. Also, I installed gcc 4.4 through MacPorts in order to experiment with C++0x. I believe, by default NetBeans uses the standard Apple gcc 4.2 compiler which is invoked through a g++ symlink found in /usr/bin.
My question is: how can I still use the new compiler?
I tried creating a new Tool Collection in NetBeans by specifying the default directory to /opt/local/bin which is where gcc 4.4 is present. But this caused error messages, i.e. No compiler sets were found.... I also tried modifying the existing GNU Tool Collection by selectively specifying the C++ Compiler command as /opt/local/bin/x86_64-apple-darwin10-g++-mp-4.4. However, this caused build errors which I didn't even understand. Any ideas appreciated.
What you describe here, adding a new tools chain with that path, just should work with gcc 4.4, see here http://forums.netbeans.org/post-70004.html
Are you sure nothing is missing in your GCC 4.4 ?
The only missing step is to click "Reset Settings" help in Code Assistance tab after creating the tool chain, to make sure that code assistance picks the new headers.
This works for me:
Create a directory for your toolchain:
mkdir -p ~/toolchains/gcc-4.4-mp/bin
then link gcc's binaries into the bin directory
cd ~/toolchains/gcc-4.4-mp/bin
ln -fs /opt/local/bin/*-mp-4.4
and give gcc's tools their unmodified names back:
for x in *-mp-4.4 ; do ln -fs $x ${x%-mp*} ; done
or
for x in *-mp-4.4 ; do mv $x ${x%-mp*} ; done
Now you can add ~/toolchains/gcc-4.4-mp/bin as a new Toolchain Collection.
Here's the "easiest" solution. Edit your PATH environment variable and then open NetBeans.
sudo port install gcc_select
sudo gcc_select mp-gcc44
# tcsh:
setenv PATH /opt/local/bin:${PATH}
# bash:
PATH=/opt/local/bin:${PATH}; export PATH
# And then:
open /Applications/NetBeans.app
You should be good to go after that unless NetBeans (uses an absolute path to the compiler).
We have a new application that requires glibc 2.4 (from gcc 4.1). The machine we have runs on has gcc 3.4.6. We can not upgrade, and the application must be run on this machine.
We installed gcc 4.1, however, when it comes to compile time it is using all the includes, etc, from 3.4.6.
How do we get around this?
Any suggestions on using 4.1 for just this application?
Refer "How to install multiple versions of GCC" here in the GNU GCC FAQ.
There's also a white paper here.
for Ubuntu it's pretty easy
sudo add-apt-repository ppa:ubuntu-toolchain-r/test
sudo apt-get update
and then install for example gcc version 6
sudo apt-get install gcc-6
Have you tried gcc-select? Otherwise, try setting the INCLUDE_PATH and LIBRARY_PATH in your shell.
update-alternatives is a very good way to have multiple gcc versions:
http://ubuntuguide.net/how-to-install-and-setup-gcc-4-1g4-1-in-ubuntu-10-0410-10
You possibly still execute the old gcc. Try making a symlink from gcc to your version of it, like
ln -s gcc-4.1 gcc
Beware of not removing an old "gcc" binary placed there, in case they placed not just a symlink. If you can recompile your own gcc version, the safest is just use another prefix at configure time of gcc, something like --prefix=/home/jojo/usr/gcc (i did it that way with gcc-4.4 from svn-trunk, and it worked great).
Note that that just runs the right gcc version. If you update your gcc, your glibc won't be updated automatically too. It's a separate package which is deeply coupled with the rest of the system. Be careful when installing another glibc version.