Setting up the GNU Compiler Collection

From gvSIG CE Wiki

Jump to: navigation, search

You must complete these steps before you can go on to Compiling the C/C++ support libraries.

Most of the support libraries that gvSIG uses (such as GDAL for raster data access) are written in C or C++ and must be compiled specifically for each operating system in 32 or 64 bit mode.

To achieve this, you need two things:

  1. A C/C++ Compiler.
  2. A command line environment that allows you to run the configuration scripts that set up the compilation on your operating system.

Fortunately, all of this exists as open source software. The first component is provided by the GNU Compiler Collection (GCC), the second by a collection of tools from the GNU project that are available on Linux, Mac OS X and Windows.

Q: Why do you not support MS Visual C/Studio?

A: Because there is an open source alternative in the form of MinGW that can produce 32/64 bit binaries. And this is an open source project. So go figure ...

Q: Why do you not show how to do this in a graphical IDE?

A: Because it's an absolute nightmare to write technical instructions for GUI-based software. Every time a button's name or menu position changes, they need updating. And anyway, there are many different IDEs for C/C++ development -- but only one command line.


Contents

Linux

This is the operating system for which compiling C/C++ source code is the most straight-forward. After all, C co-evolved with Unix and thus the two harmonize perfectly.

The precise steps for getting all necessary software onto your system will vary between Linux distributions. Consult your distribution's documentation on how to install additional software (this is usually achieved by using a so-called "package manager").

On Ubuntu, you can install all required software by executing this command:

 sudo apt-get install build-essential

If you launch GCC:

 gcc

And you get something similar to:

 gcc: no input files

Then you are ready to go.

You will not need any additional development packages (those are the ones that are usually suffixed with "-dev" in the package manager).

More recent Ubuntu distributions have a nice feature: If you try to launch a command and the software is not yet installed on your system, it will suggest a package to install. In that case, use:

 sudo apt-get install <package-name>

This will also automatically install any other packages that it may depend on.

Note: If you are new to Linux, and/or have never used the command line in earnest, you might still want to read the page about Getting started with MSYS. Much of it also applies to your operating system.

Mac OS X

The precise steps may differ between versions of Mac OS X, so consider the below a rough guide. The instructions on this wiki assume a minumum version of Mac OS X 10.7.3. The latest Xcode version compatible with this is 4.6.3.

Apple delivers all necessary software a part of the Xcode package. If you didn't get Xcode on a DVD, you can download it here. You will be required to register for an Apple Developer Connection account. Once you’ve registered, login and click "Download Software" and then "Developer Tools". Find the "Download" link next to "Xcode Tools (version) – CD Image".

Double-click the downloaded package and follow the installation instructions. This will install GCC and all other required tools on your system. In order to use GCC, run the Terminal application (it is located in the "Utilities" folder of your Applications collection). Once the Terminal window has opened, execute this command:

 gcc

If you get something similar to:

 gcc: no input files

Then you are all set up.

An in-depth article about GCC on Mac OS X is available here.

Note: If you are new to Mac OS X, and/or have never used the command line in earnest, you might still want to read the page about Getting started with MSYS. Much of it also applies to your operating system.

Windows

Windows does not come with any development tools, so you must install both the GCC and a set of command line tools first.

At some point, you will need to edit source code or configuration files. This is not much fun with the built-in Windows text editor. There are many free and open source alternatives that can handle multiple open files, syntax highlighting, text encoding and different end-of-line characters (important!). Take a look at Notepad++, JEdit or PSPad.

You will also need a decent software to handle a number of archive formats (ZIP, .tar.gz, etc.). Try IZArc or 7-Zip.

MinGW-w64 (C/C++ compiler)

We will use MinGW-w64 for a C/C++ compiler suite. It is easy to set up and supports producing both 32 and 64 bit binaries. It runs on a 32 bit or 64 bit system, and allows you to create 64 bit binaries on a 32 bit OS and vice versa (i.e. MinGW is actually a cross-compiler toolchain). MinGW is based on a Windows port of the GNU Compiler Collection (GCC). The instructions on this Wiki assume that you are using a version of MinGW that is based on GCC 4.5.4. More recent versions of GCC are known to cause problems with compiling the GEOS C++ libraries (see [here]).

Be advised that there are some grave problems with creating 64 bit binaries using MinGW-w64. These stem from the fact that the MinGW-w64 toolchain (i.e. the actual executables like "gcc.exe") are 32 bit programs, capable of producing 64 bit code, and Windows uses an absolutely insane mechanism for deciding which version of its system libraries (the ones in the aptly named directory "C:\Windows\System32") to link into a compiled binary. Unfortunately, the MingW toolchain (even the one for creating 64 bit targets!) consists of 32 bit binaries, so Windows' auto mechanism will fail when attempting to link a 64 bit binary to a 64 bit system library using MinGW. Therefore, pay very close attention to all details when creating 64 bit binaries.

For better control over the linking process, you can extract the required Windows system DLLs from the "System32" folders of your 32 and 64 bit Windows installations and copy them respectively into the "i686-w64-mingw32\lib" and/or "x86_64-w64-mingw32\lib" folders of your MinGW installation. To match GCC's naming scheme, you must rename each library using the "lib" prefix, e.g. "libcrypt32.dll". Note that you absolutely have to extract the libraries manually, using a Windows system that has the "right" bit number. Only then will the Windows GUI shell pick up the correct libraries from "System32" (on 64 bit Windows, do not copy these files from "SysWOW64", as the later contains a 32 bit runtime environment -- no comment...). The required libraries are: "crypt32.dll", gdi32.dll, "msimg32.dll", "Wldap32.dll" and "ws2_32.dll".

Further information can be found at http://www.gaia-gis.it/gaia-sins/mingw64_how_to.html

There are several versions of the MinGW-w64 compiler suite available for download. The recommended, stable version on Windows is the "rubenv" toolchain which you can find

Find the folder with the latest GCC version that is marked "release" and then download the version of the package that is marked gcc-4.5.4-release-win32 (32 bit!). Not that there is also a version "release-win64" that runs only on Windows 64 bit. Avoid any packages that have "DW2" in the name.

Installation is simple. Just make sure to keep the 32 and 64 bit toolchains in separate directories. The recommended default is to unpack directly into "C:\mingw32" and "C:\mingw64", respectively.

For a quick test, start "cmd.exe" and extend the system PATH to include the toolchain binaries:

 set PATH=C:\mingw64\bin;%PATH% 
 set PATH=C:\mingw32\bin;%PATH% 

Test that the compiler for 32 bit targets works:

 i686-w64-mingw32-gcc -v

Test that the compiler for 64 bit targets works:

 x86_64-w64-mingw32-gcc -v

In both cases, you should get a detailed listing of compiler properties and the version number.

Further details can be found on the MinGW64 MinGW-w64 Wiki.

MSYS (command line tools)

In addition to the compiler suite itself, you will need a number of command line tools. Essentially, the MSYS (Minimal SYStem) tools provide a minimal POSIX environment on Windows. This allows configuration and compilation of source code that was originally designed to work on Unixish systems, such as Linux.

Rather than downloading dozens of individual files from the MSYS home page, the MinGW-w64 project provides a convenient, single file download that contains all you need.

Installation is very simple. Instructions can be found on the MinGW-w64 Wiki page on MSYS:

  1. Download the above package.
  2. Unzip it to "C:\msys" so that "C:\msys\bin" contains (among others) "bash.exe".
  3. Doubleclick on "C:\msys\msys.bat".
  4. type:
 sh /postinstall/pi.sh

When running the post install script, specify that you have MinGW in "C:/mingw32" (for a 32 bit toolchain) or "C:/mingw64" (note the unusual "/" used as a path separator: MSYS follows the Posix conventions!).

You can also get the "MinGW-builds" package for a more complete collection of tools. Just unpack the additional files on top of your existing MSYS installation.

From this point on, you can start the shell, configure and compile source code from the command line, by running C:\msys\msys.bat. You can then directly run GCC for 32 bit targets from within MSYS. The following instructions assume that you have installed MSYS into C:\msys.

Make a quick test by running the GCC and querying its version number:

 gcc --version

You should get output similar to this:

 gcc.exe (rubenvb-4.5.4-release) 4.5.4
 Copyright (C) 2012 Free Software Foundation, Inc.
 This is free software; see the source for copying conditions.  There is NO
 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

To complete the MSYS installation, you will need some additional tools. Read on below. If you have downloaded and installed "MinGW-builds" as suggested above, then you might already have some or all of the additional tools on your system and you can skip the following. Just test by writing the name of the tools on the MSYS command line. If you get anything else than a "command not found", then you are good to go.

GNU Findutils

On Windows, you also need the GNU Findutils, which provide some command line tools to search for files. You can download a binary distribution of tools here (you will only need the ".bin.tar.lzma" package of the "findutils").

For some reason, the package uses the LZMA compression format, which is not widely supported. So chances are you will need to get a copy of the XZ Utils for Windows from here. This includes a command line tool, xz, which you can use to decompress the Autotools package. E.g. if you have the XZ Utils binaries and the Autotools package in the same folder, launch cmd.exe, change into that folder and do:

 xz -d findutils-<version>-msys-<version>-bin.tar.lzma

This will result in a plain TAR archive file, which you can then extract with your Windows tool of choice. The package contents simply need to be copied into the corresponding bin subfolder of your MinGW-w64 istallation folder.

Test if the new command line "find" works:

 find

This should produce a list of files in the current folder and all subfolders (press CTRL+C keys to interrupt). If you get an error message about wrong parameters instead, then Windows' system "find.exe" is being used, which is not what you want. In that case, review all steps above to make sure that you have the MSYS "find.exe" installed in the correct folder.

pkg-config

On Windows, you also need pkg-config. This is a little setup tool that keeps a record of which libraries have been installed on the (MSYS) system. Many open source projects rely on this tool for automatic configuration. Unfortunately, it is not provided by MSYS.

First, download pkg-config here and GLib from here. Unzip both archives and copy the contents of the "bin" and "share" folders into the corresponding folders of your MSYS installation.

Finally, copy "msys-intl-8-dll" (it should be in the "bin" folder of your MSYS installation) as "intl.dll" into the same "bin" folder of your MSYS installation.

Performance tuning

The MinGW/MSYS/GCC toolchain can be painfully slow. This page has some tips on how to speed things up significantly. Essentially, it recommendes using ccache and make -r (instead of plain make). Note, however, that these changes can introduce additional issues and sources of error into the already complex process of compiling large amounts of C/C++ source code on Windows.

Additional useful tools

This section lists some additional tools that will make your life easier when compiling large amounts of source code.

Console output colorizers

CCZE (https://github.com/cornet/ccze) is a log colorizer that also does an excellent job with output coming from Make, linker and compiler messages. It makes spotting error and warning messages in large amounts of console output much easier. E.g. to colorise the output of a "make" run, use the following:

 make | ccze -A

Note the "-A" option to use raw ANSI output. Similarly, to colorise the output of a cleaning operation:

 make clean | ccze -A

... etc.

For Windows/MSYS colorgcc (http://schlueters.de/colorgcc.html) might be a good alternative (Perl interpreter required).

Next steps

If you wish to get acquainted with MSYS and the Unix shell (command line) first, then continue with Getting started with MSYS.

Otherwise, go straight to Compiling the C/C++ support libraries.

Previous steps

Go back to Compiling the Java sources.