[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [igraph] Install igraph on ubuntu 10.04 failed

From: Tamas Nepusz
Subject: Re: [igraph] Install igraph on ubuntu 10.04 failed
Date: Tue, 1 Jun 2010 21:47:18 +0100

> I was trying to compile igraph source on ubuntu 10.04 TLS but failed. Got the 
> following error message:
> /usr/bin/ld: cannot find -lxml2
Have you installed libxml2-dev? If not, then:

$ sudo apt-get install libxml2-dev

> And I have asked the similar question but I haven' got a good answer: what is 
> the correct way to compile
> a dll equivalent on linux?
> I am very new to linux, and I have successfully complied a dll independent of 
> igraph.dll
> (static link) on windows, but I am just not very sure how to do this on 
> windows.
If you want to link your code statically to igraph, you have to link to 
libigraph.a, which is to be found in src/.libs after a successful compilation 
of igraph. This also gets installed into /usr/lib if you run "make install".

Assume that you have the following code on Linux in a file called main.c:

#include <igraph/igraph.h>
#include <stdio.h>

int main(int argc, char* argv[]) {
  igraph_t g;
  igraph_full(&g, 10, 0, 0);
  printf("Graph has %ld vertices and %ld edges\n",
    (long int)igraph_vcount(&g),
    (long int)igraph_ecount(&g));

You can compile it with:

$ gcc -c test.c

This will create test.o, which you can then link with libigraph.a:

$ gcc -static -o test test.o -ligraph -lm

This will create a static executable named "test". The -static flag tells gcc 
to prefer static libraries over dynamic ones; -ligraph tells gcc to link to 
igraph, -lm tells gcc to also link to the "m" library, which contains the 
mathematical functions. -lm is required by -ligraph.

If you want to do the whole thing in one step:

$ gcc -static -o test test.c -ligraph -lm

You can check whether the resulting executable is static or not:

$ file test
test: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, 
for GNU/Linux 2.6.4, not stripped

To remove unnecessary routines from the executable that are linked from 
libigraph.a but not used (this reduces the file size):

$ strip test
$ file test
test: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, 
for GNU/Linux 2.6.4, stripped

Linking a shared library (.so) with the dynamic igraph library is essentially 
the same:

$ gcc -shared -o libtest.so test.o -ligraph -lm

(Note that in this case, test.o should not contain a function named "main"). 
However, libtest.so depends on libigraph.so.0, which you can check by issuing:

$ ldd libtest.so

Linking a library with the _static_ igraph library is more complicated. In 
Linux, whenever you compile some code that you want to include later in a 
shared library, you have to compile it using position-independent code only; 
this is turned on at compilation time in gcc by passing the -fPIC switch. 
However, the static libigraph.a is not compiled using -fPIC (after all, it's a 
static library!), so you have to recompile igraph from source and pass the 
-fPIC switch explicitly:

$ CFLAGS=-fPIC LDFLAGS=-fPIC ./configure
$ make

After that, you will have a libigraph.a in src/.libs which is compiled using 
-fPIC, and you can link to this to produce a dynamic library that does not 
depend on libigraph.so.0:

$ gcc -shared -o libtest.so test.o /full/path/to/igraph/src/.libs/libigraph.a
$ ldd libtest.so
        linux-vdso.so.1 =>  (0x00007fff45a76000)
        libc.so.6 => /lib64/libc.so.6 (0x00007f01da66b000)
        /lib64/ld-linux-x86-64.so.2 (0x00007f01dac67000)


reply via email to

[Prev in Thread] Current Thread [Next in Thread]