Saturday, June 18, 2011

Linker Problem on Linux

Now that the program builds and runs on Windows, it was time to make sure it still worked on Linux. However, the linker failed because the compiler/linker did not understand that ‑static‑libstdc++ option. Upon research, it was discovered that this option was only in GCC version 4.5 and later, so it was not available in the GCC 4.4.5 on Kubuntu.

The latest version of GCC (4.6.0) was downloaded and installed on Kubuntu using the source code. This required quite a bit of work to accomplish (for complete details, hit the continue link). Now that the project was building on both Linux and Windows, it was time to introduce it to the NetBeans IDE (which was now at version 7.0, version 6.9.1 was being used for development on Windows).


Before GCC 4.6.0 could be built on Linux, some prerequisite packages and libraries need to be installed first.  The packages include m4, autoconf, automake, libtool and libc6-dev-i386.  Each of these can be installed with this command:
sudo apt-get install <package>
The libraries include GMP (GNU Multiple Precision arithmetic library), MPFR (Multiple Precision Floating point library with correct Rounding) and MPC (Multiple Precision Complex number library). The sources of the latest version of each of these were downloaded and installed (GMP 5.0.1, MPFR 3.0.1 and MPC 0.9). The procedure used for installing each of these were:
  1. uncompress the library source (tar xjf xxx.tar.bz2 or tar xjf xxx.tar.gz)
  2. change to the new directory created (cd xxx)
  3. ./configure
  4. make
  5. sudo make install
All of these libraries were installed at /usr/local/lib and /usr/local/include by default. Now GCC 4.6.0 was successfully built and installed using this procedure:
  1. tar xjf gcc‑4.6.0.tar.bz2 ‑or‑ tar tzf gcc‑4.6.0.tar.gz
  2. mkdir gcc-4.6.0-build
  3. cd gcc-4.6.0-build
  4. ../gcc‑4.6.0/configure ‑‑prefix=/usr/gcc‑4.6.0 ‑‑with‑gmp=/usr/local ‑‑with‑mpfr=/usr/local ‑‑with‑mpc=/usr/local
  5. export LD_LIBRARY_PATH=/usr/local/lib
  6. make bootstrap-lean
  7. sudo make install
For a multiple core processor, the ‑jX where X is the number of cores, can be added to the make commands to speed up the build. This will install GCC into the /usr/gcc‑4.6.0 so as not to overwrite the GCC that comes with Kubuntu. However, the PATH and library environment variables need to be set properly so it uses GCC 4.6.0 and not GCC 4.4.5 (or whichever version is installed). To do this, these commands are used:
export PATH=/usr/gcc‑4.6.0/bin:$PATH
export LD_LIBRARY_PATH=/usr/gcc‑4.6.0/lib64:/usr/gcc‑4.6.0/lib:/usr/local/lib
Which can be put into a script file (for example ~/bin/gcc460) and executed using the command ". ~/bin/gcc460" to set the variables. The period tells the current shell to execute the script directly, otherwise the script is executed in a new shell and when done, terminates having no effect in the current shell. The gcc‑4.6.0 (source) and gcc‑4.6.0‑build directories can safely be removed once GCC 4.6.0 is successfully installed, along with the directories used to build the three prerequisite libraries.

No comments:

Post a Comment

All comments and feedback welcomed, whether positive or negative.
(Anonymous comments are allowed, but comments with URL links or unrelated comments will be removed.)