Monday, January 23, 2012

Compile and Install BigInt as a Shared Library in Kubuntu

BigInt is an awesome library that allow us to write C/C++ programs that use arbitrary precision arithmetic. Here we'll go through on how to setup the library as a shared object under GNU/Linux (Kubuntu).
#Download the library
wget --no-check-certificate
#Extract it
tar -xvf bigint-2010.04.30.tar.bz2
#Rename it to bigint
mv bigint-2010.04.30 bigint
#copy it to the include dir
sudo mkdir -p /usr/local/include/bigint/
sudo cp bigint/* /usr/local/include/bigint/
#creating shared library
#create object files
g++ -fpic -c
#create shared library called which can be invoked using -lbigint
g++ -shared -o BigInteger.o BigIntegerAlgorithms.o BigIntegerUtils.o BigUnsigned.o BigUnsignedInABase.o
#copy it into the local lib dir
sudo cp /usr/local/lib/
#configure ldconfig (dynamic linker runtime bindings) to rebuild the shared library cache
#note that ldconfig should contain the path where you copied the file
#/usr/local/lib is already added. You can add additional paths in /etc/
sudo /sbin/ldconfig -v /usr/local/lib
-fpic or -fPIC means position independent code.
Now that the library & the environment is ready, let's write a small program and verify it.
// big.cpp
#include <iostream>
#include <bigint/BigIntegerLibrary.hh>

using namespace std;

int main()
BigInteger a = 31415926535;
cout << a * a * a * a * a * a * a * a << endl;
return 0;
Compile & run
g++ big.cpp -o big -lbigint