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.

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 very well possible 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)

As a C/C++ compiler suite, we will use MinGW-w64, which is easy to set up and supports producing both 32 and 64 bit binaries.

There are several alternatives for getting MinGW-w64 on your system, which can be confusing at first. Below, we will outline the two most common ones.

Note that you can install both 32 and 64 bit compilers on your system. You will be able to decide which target to compile for later.

TDM-GCC toolchain (recommended)

By far the most convenient (and recommended) option is to use the TDM-GCC toolchain, which includes MinGW-w64, and comes with an installer software. Go to http://tdm-gcc.tdragon.net/ and click "Downlad TDM-GCC". On the page with downloads, locate the "Bundle installer" and download the 32 or 64 bit version (both will be able to run on a 32 bit system).

Double-click the .exe file to start the installer:

  1. On the first page, click "Create". You can now choose whether to install a 32 bit only or 32/64 bit version of the compiler suite. Choose the latter option ("Create a MinGW-w64-based installation").
  2. If you like, you can then choose to change the installation directory. However, for the purpose of these instructions, we will assume that the compiler was installed in C:\MinGW64 (default).
  3. Select a download mirror and click "Next>".
  4. On the final page, you can select the components to install. We recommend to keep the default selection (which can be restored at any time by choosing "TDM-GCC Recommended C/C++") from the drop-down menu. Then click "Install".
  5. Be patient while the software is downloaded and installed.

The default operation mode of TDM-GCC toolchain is to produce 64 bit binaries (even on 32 bit systems). We will show you how to explicitely produce 32 bit binaries when we discuss the actual compilation of source code later.

Please refer to the included file README-gcc-tdm64.txt (to be found in the installation folder you chose above) for more details.

original MinGW-w64 files (advanced)

If you like, you can also download and install a specific version of the MinGW-w64 toolchain manually.

There are many versions of this compiler suite, available for download here (32 bit) and here (64 bit). For a version that works on Windows, you need to get one that has the name mingw-w32-bin_i686-mingw_<version>.zip (32 bit) or mingw-w64-bin_i686-mingw_<version>.zip (64 bit).

At the time of writing, the most recent ones were here (32 bit) and here (64 bit). This is a large download, with a size of more than 300 MB.

If you need more details, take a look at this page on the MinGW-w64 filename structure.

We will not cover the manual installation process here. It is documented on the MinGW-w64 Wiki.

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"). The latest version as of this writing was 4.4.2-2.

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.

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.

The following instructions assume that you have installed MSYS into C:\msys.

From this point on, you can start the shell, configure and compile source code from the command line, by running C:\msys\msys.bat.

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

 gcc --version

You should get output similar to this:

 gcc.exe (tdm64-1) 4.5.2
 Copyright (C) 2010 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.

Now continue by reading the page about Getting started with MSYS.

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.