Building GCC in a Non-Root Folder
Matthew Martinez
Alright, so this is my first question here--likely of many.
I am a Linux noob and am trying to learn my way around building from source.
My environment: SSH'd via Putty into Ubuntu Server 14.04.5 on a VM in Windows 10
I want to have GCC be the first program I build from source.
Here is my process so far:
In order to get GCC so that I can build a different GCC (funny, I know), from my default directory /home/anton I ran:
sudo apt-get install build-essential
sudo apt-get install m4
sudo apt-get install texinfoMy goal is to make another directory, off of root, my source directory for the new GCC build--let's call it /AntonProj1
What I am having a problem with are the next steps... here are my thoughts; please let me know if this is the right logic or if I am interpreting the build/dependency process wrong.
- Step 1) Download GCC (gcc-4.1-20080630.tar.bz2 from
) into
/AntonProj1[I will use FileZilla to manually move the .bz2 file into/AntonProj1 - Step 2) Run
contrib/download_prerequisitesin/AntonProj1 - Step 3)
export PREFIX="AntonProj1/opt/gcc-4.1"??? <- Does this look right?
Step 3 is about where my confusion begins... or at least where I feel stuck.
I read something here that says:
"Please note that we build everything out of the source directory tree, as is considered good practice. Some packages only support building outside, some only inside and some both (but may not offer extensive checking with make). Building GCC inside the source directory tree fails miserably, at least for older versions."
Is this just saying that it is OK to build GCC in /AntonProj1 (since the GCC I am using to build this GCC is in a different source directory); but, for example, if I want to use the new GCC build located in /AntonProj1 to build other programs... let's say, something like Lynx, I need to make sure Lynx's source directory is something other than /AntonProj1 . . . perhaps something like /AntonProj1/Lynx?
TL;DR: What's the best approach for how to prepare to compile GCC from source when my constraints are:
- 1) Download and compile GCC into
/AntonProj1 - 2) Use the GCC that was downloaded and compiled into
/AntonProj1to build other programs from source all within the/AntonProj1folder
2 Answers
First, since you have run sudo apt-get install build-essential you must be having gcc installed already. You can find out which version you have by running gcc -v.
The location of this installed version would be /usr/lib/gcc/version-number/.
/usr/bin/gcc is the default location of the binary of gcc in Ubuntu.
Next, You want to install a previous version.
- For
gcc-4.1
Since this version is available in the older releases of Ubuntu, we need to have the appropriate repositories for the version. From some search, I found them to be the following, which have to be added to the /etc/apt/sources.list
deb lenny main
deb-src lenny main
deb lenny/updates main
deb-src lenny/updates main*These links have been taken from this answer.
Then after doing sudo apt-get update the new repositories will be available.
Next, install the required compiler. I usually install gcc and g++ of the same version for inter-operability. For general purpose it is recommended.
Hence,
sudo apt-get install gcc-4.1 g++-4.1
2. Checking the available compilers
At this stage one will have two set of compilers (oen each for g++ and gcc). These can be checked by dpkg --list | grep compiler,
dpkg --list | grep compiler
ii antlr3.2 3.2-14 all language tool for constructing recognizers, compilers etc
ii g++ 4:5.3.1-1ubuntu1 amd64 GNU C++ compiler
ii g++-4.1 4.1.2-13 amd64 The GNU C++ compiler
ii g++-4.1-multilib 4.1.2-13 amd64 The GNU C++ compiler (multilib files)
ii g++-5 5.4.0-6ubuntu1~16.04.4 amd64 GNU C++ compiler
ii gcc 4:5.3.1-1ubuntu1 amd64 GNU C compiler
ii gcc-4.1 4.1.2-13 amd64 The GNU C compiler
ii gcc-4.1-multilib 4.1.2-13 amd64 The GNU C compiler (multilib files)
ii gcc-5 5.4.0-6ubuntu1~16.04.4 amd64 GNU C compiler
ii gcc-5-multilib 5.4.0-6ubuntu1~16.04.4 amd64 GNU C compiler (multilib support)
ii gcc-multilib 4:5.3.1-1ubuntu1 amd64 GNU C compiler (multilib files)
ii hardening-includes 2.7ubuntu2 all Makefile for enabling compiler flags for security hardening
ii libantlr-java 2.7.7+dfsg-6ubuntu1 all language tool for constructing recognizers, compilers etc (java library)
ii libllvm3.8:amd64 1:3.8-2ubuntu3 amd64 Modular compiler and toolchain technologies, runtime library
ii libllvm3.8:i386 1:3.8-2ubuntu3 i386 Modular compiler and toolchain technologies, runtime library
ii libxkbcommon0:amd64 0.5.0-1ubuntu2 amd64 library interface to the XKB compiler - shared librarImportant is the location of the two set of compilers, which can be listed by,
ls -lh /usr/bin/gcc*
lrwxrwxrwx 1 root root 5 五 5 2016 /usr/bin/gcc -> gcc-5
-rwxr-xr-x 1 root root 208K 七 10 2007 /usr/bin/gcc-4.1
-rwxr-xr-x 1 root root 895K 十一 3 10:00 /usr/bin/gcc-5and
ls -lh /usr/bin/g++*
-rwxr-xr-x 1 root root 210K 七 10 2007 /usr/bin/g++-4.1
-rwxr-xr-x 1 root root 899K 十一 3 09:41 /usr/bin/g++-5- Selecting a compiler for current purpose (building an application)
After having required compilers installed one can simply switch among compilers. This is done by updating the list of alternative versions of an application. To do this, the update-alternative command has to be run with certain parameters.
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-5 60 --slave /usr/bin/g++ g++ /usr/bin/g++-5
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.1 40 --slave /usr/bin/g++ g++ /usr/bin/g++-4.1
sudo update-alternatives --config gccThis will link g++ to gcc and on changing only gcc the g++ will change automatically.
Then whenever you want to change the compiler enter this:
sudo update-alternatives --config gccThen, user is asked which compiler to choose.
Selection Path Priority Status
------------------------------------------------------------
* 0 /usr/bin/gcc-5 60 auto mode 1 /usr/bin/gcc-4.1 40 manual mode
Press <enter> to keep the current choice[*], or type selection number:Here you can choose by pressing the key(0,1) and then pressing enter. The change in the current selected version can be checked by gcc -v.
4. Building an application using installed compilers
Some basic guidelines:
- There are in general two steps, building (
build) and making (make). Thebuildstep involves the generation of system specific codes for installation which requires linking to required compilers.makeis equivalent to installation in some directory. Any application can bebuildandmakein any folder as decided by the user. - To
builda temporary folder is chosen. This folder can be removed after a successfulmakestep. Important is that for themakestep the installation destination is defined usually by--prefix=/path/to/folderwhile thebuildscript is run. For example, if the setup script is.setupthen, in most cases,./setup --prefix=/path/to/installation/folder
For specific details please refer to the manual or guidelines for your application.
When running the build script the current selected compiler version (for gcc and g++) will be used. When running the installed application, the behavior will depend on the case whether the program takes compiler from environment variable or is linked to the specific binary. That has to be checked after installation.
cmakeandmakeare separate utility used for installation. Different applications require different versions, so install the required utility and version accordingly.The step of
buildandmakeare same for compilers as well.
Here's the process that ended up actually working for me:
Make directories to store the downloads and the unzipped directories:
sudo mkdir -p /AntonProj1/opt/downloads /AntonProj1/tmpStep 1) Download GCC (gcc-4.1-20080630.tar.bz2 from ) into
/AntonProj1/opt/downloads[I used FileZilla to manually move the .bz2 file into/AntonProj1Step 2) Move files to
/AntonProj1/opt/downloadsStep 3) Run the following commands:
sudo apt-get install build-essential sudo apt-get m4 sudo apt-get texinfosudo apt-get install libgmp3-dev sudo apt-get install libmpfr-dev libmpfr-doc libmpfr4 libmpfr4-dbgsudo apt-get update sudo apt-get upgrade sudo apt-get autoremove
And now for the most important/time consuming part:(This is where we actually ensure we're building in the desired directory /AntonProj1
sudo ./configure -prefix=/AntonProj1 --without-[blah blah]**The --without-[blah blah] is a way to help work through dependency issues when you do the command that actually builds from source:
sudo make*That is, chances are the .configure will show you what dependencies are missing... and may even halt before the makefile is created. However, even if a makefile is created, when you go to actually sudo make you may (read: will) find that the build fails. So, if you keep having builds fail, even when you install the requested dependencies, it may be a matter of telling the compiler not to worry about certain things, this can be done with --without-[blah blah]; more about the ./configure flags can be found by running .configure -h to access the help file.
Note: if you do end up changing the ./configure flags, you'll need to run:
sudo make clean
and then ./configure again with the new flags.