Compiling the very first example from the manual

I am trying to compile an example from the manual (Chapter 3 - 1.2 Compiling without CMake) but without success.

The program I am trying to compile is:

#include <igraph.h>

int main() {
  igraph_real_t diameter;
  igraph_t graph;

  igraph_rng_seed(igraph_rng_default(), 42);

  igraph_erdos_renyi_game(&graph, IGRAPH_ERDOS_RENYI_GNM, 1000, 3000,
                          IGRAPH_UNDIRECTED, IGRAPH_NO_LOOPS);

  igraph_diameter(&graph, &diameter, 0, 0, 0, IGRAPH_UNDIRECTED, 1);
  printf("Diameter of a random graph with average degree %g: %g\n",
          2.0 * igraph_ecount(&graph) / igraph_vcount(&graph),
          (double) diameter);

  igraph_destroy(&graph);

  return 0;
}

(No changes to the code from the section 1.1 of the manual).

I try to compile the above code on LinuxMX 19.4 using the following command:

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

as a result I get a lot undefined referece to errors like:

/usr/bin/ld: /usr/local/lib/libigraph.a(random.c.o): in function `igraph_qnorm5.part.5':
random.c:(.text+0x76d): undefined reference to `exp'
/usr/bin/ld: random.c:(.text+0xa4f): undefined reference to `log'
/usr/bin/ld: random.c:(.text+0xbc9): undefined reference to `expl'
/usr/bin/ld: random.c:(.text+0xd13): undefined reference to `exp'
/usr/bin/ld: random.c:(.text+0xd88): undefined reference to `expl'
/usr/bin/ld: random.c:(.text+0xde9): undefined reference to `sqrt'
/usr/bin/ld: /usr/local/lib/libigraph.a(random.c.o): in function `igraph_rng_get_binom':
random.c:(.text+0x19c6): undefined reference to `log'
/usr/bin/ld: random.c:(.text+0x1ac6): undefined reference to `log'
/usr/bin/ld: random.c:(.text+0x1c83): undefined reference to `log'
/usr/bin/ld: random.c:(.text+0x1d66): undefined reference to `log'
/usr/bin/ld: random.c:(.text+0x1d95): undefined reference to `log'

and a lot more.

The output of pkg-config --libs --cflags igraph command is
-I/usr/local/include/igraph -L/usr/local/lib -ligraph so at least this seems to be ok.

I should mention that installing the library using the manual was seamless (or at least I did not get any errors after the “build and test” command at the end of chapter 2.1).

What is the output from the following?

pkg-config --libs --cflags --static igraph

It looks like you installed igraph as a static library, so you need to also use --static with pkg-config. This will list all of igraph’s dependencies which you also need to link to.

Generally, when linking to a shared library, you don’t need to do anything extra. The shared library knows its own dependencies. However, when using static linking, it is your responsibility to include that library’s transitive dependencies. The --static flag instructs pkg-config to list these.


All that said, we do recommend using CMake when possible, as CMake can take care of these intricacies automatically.

1 Like

It works!
Compiling the program with the command:

 cc igraph_test2.c -I/usr/local/include/igraph -L/usr/local/lib -ligraph -lm -lstdc++ -lgomp -lpthread

has produced a working program.
Thank you very much and I apologize if the question is cluttering the forum.

About using CMake, I have tried to use it (with CMakeLists.txt exactly like in the manual) but received the following error:

CMake Error at CMakeLists.txt:6 (add_executable):
  Target "igraph_test" links to target "OpenMP::OpenMP_CXX" but the target
  was not found.  Perhaps a find_package() call is missing for an IMPORTED
  target, or an ALIAS target is missing?

I guess it is a topic for another thread.

Thanks again for helping me out.

I can reproduce this and we will look into it. In the meantime, you can work around it by changing the line project(igraph_test C) to project(igraph_test). This problem occurs only if compiling igraph with OpenMP support (which is the default when OpenMP is available).

1 Like

Thanks again!
This indeed solved the problem.

Hope your answers will be as beneficial to others in the future as they are to me now.