1. Lesson 1. Compiling programs using igraph.

The following short example program demonstrates the basic usage of the igraph library.

#include <igraph.h>

int main(void)
     igraph_real_t diameter;
     igraph_t graph;
     igraph_erdos_renyi_game(&graph, IGRAPH_ERDOS_RENYI_GNP, 1000, 5.0/1000,
                             IGRAPH_UNDIRECTED, IGRAPH_NO_LOOPS);
     igraph_diameter(&graph, &diameter, 0, 0, 0, IGRAPH_UNDIRECTED, 1);
     printf("Diameter of a random graph with average degree 5: %f\n",
             (double) diameter);
     return 0;

This example illustrates a couple of points. First, programs using the igraph library should include the igraph.h header file. Second, igraph uses the igraph_real_t type for real numbers instead of double. Third, igraph graph objects are represented by the igraph_t data type. Fourth, the igraph_erdos_renyi_game() creates a graph and igraph_destroy() destroys it, ie. deallocates the memory associated to it.

For compiling this program you need a C compiler, if this is called gcc and the previous code is saved in file igraph_test.c, you will need a command like this:

gcc igraph_test.c -I/usr/local/igraph -L/usr/local/lib -ligraph -o igraph_test

The exact form depends on where igraph was installed on your system. The directory after the -I switch is the one containing the igraph.h file, while the one following -L should contain the library file itself, usually a file called libigraph.so, libigraph.a or igraph.dll. It your system has the pkg-config utility you are likely to get the neccessary compile options by issuing the command

pkg-config --libs --cflags igraph

The executable can be run by simply typing its name like this:


on most systems. If you use dynamic linking and the igraph libraries are not at a standard place, you may need to set the LD_LIBRARY_PATH variable, the syntax depends on the shell use are using. In bash it goes like this:

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/user/libs/igraph

Here we assumed that the igraph library is installed in /home/user/libs/igraph. Alternatively, you can use the LD_PRELOAD variable to preload the igraph library before invoking your program:

LD_PRELOAD=/home/user/libs/igraph/libigraph.so ./igraph_test

Please note that LD_PRELOAD and LD_LIBRARY_PATH are usually available only on Un*x-like systems. On Windows using Cygwin it is usually enough to set the PATH enviroment variable to include the folder in which the igraph library is installed, look for the cygigraph-0.dll or similar file.