# Compiling the very first example from the manual

**URL:** <https://igraph.discourse.group/t/compiling-the-very-first-example-from-the-manual/919>\
**Category:** Usage\
**Tags:** C\
**Created:** [24 October 2021 19:19 UTC](https://igraph.discourse.group/t/compiling-the-very-first-example-from-the-manual/919 "2021-10-24T19:19:57Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![zajer](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/zajer/32/609_2.png) [@zajer](https://igraph.discourse.group/u/zajer)\
**Post date:** [24 October 2021 19:19 UTC](https://igraph.discourse.group/t/compiling-the-very-first-example-from-the-manual/919/1 "2021-10-24T19:19:57Z")

</div>

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:

```auto
#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:

```auto
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:

```auto
/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).

---

<div class="post-metadata">

**Author:** ![szhorvat](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/szhorvat/32/3_2.png) [@szhorvat](https://igraph.discourse.group/u/szhorvat)\
**Post date:** [24 October 2021 19:37 UTC](https://igraph.discourse.group/t/compiling-the-very-first-example-from-the-manual/919/2 "2021-10-24T19:37:04Z")

</div>

> [@zajer](#):
>
> The output of `pkg-config --libs --cflags igraph` command is

What is the output from the following?

```plaintext
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.

---

<div class="post-metadata">

**Author:** ![zajer](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/zajer/32/609_2.png) [@zajer](https://igraph.discourse.group/u/zajer)\
**Post date:** [24 October 2021 19:45 UTC](https://igraph.discourse.group/t/compiling-the-very-first-example-from-the-manual/919/3 "2021-10-24T19:45:17Z")

</div>

It works!  
Compiling the program with the command:

```auto
 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:

```auto
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.

---

<div class="post-metadata">

**Author:** ![szhorvat](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/szhorvat/32/3_2.png) [@szhorvat](https://igraph.discourse.group/u/szhorvat)\
**Post date:** [24 October 2021 22:19 UTC](https://igraph.discourse.group/t/compiling-the-very-first-example-from-the-manual/919/4 "2021-10-24T22:19:09Z")

</div>

> [@zajer](#):
>
> About using CMake, I have tried to use it (with CMakeLists.txt exactly like in the manual) but received the following error:
> 
> ```auto
> 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 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).

---

<div class="post-metadata">

**Author:** ![zajer](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/zajer/32/609_2.png) [@zajer](https://igraph.discourse.group/u/zajer)\
**Post date:** [24 October 2021 22:48 UTC](https://igraph.discourse.group/t/compiling-the-very-first-example-from-the-manual/919/5 "2021-10-24T22:48:21Z")

</div>

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.
