# igraph tutorial update

**URL:** <https://igraph.discourse.group/t/igraph-tutorial-update/708>\
**Category:** Development\
**Tags:** C\
**Created:** [10 April 2021 17:40 UTC](https://igraph.discourse.group/t/igraph-tutorial-update/708 "2021-04-10T17:40:43Z")\
**Posts on this page:** 4\
**Page:** 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:** [10 April 2021 17:40 UTC](https://igraph.discourse.group/t/igraph-tutorial-update/708/1 "2021-04-10T17:40:43Z")

</div>

This is a draft update to the first lesson in the tutorial, [igraph Reference Manual](https://igraph.org/c/doc/igraph-Tutorial.html)

It will apply to the future version 0.9.2. Those trying it now should install igraph from the master branch on GitHub.

## Lesson 1. Compiling programs using igraph

### Your first igraph program

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

```auto
/* igraph_test.c */

#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;
}

```

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()`](https://igraph.org/c/doc/igraph-Generators.html#igraph_erdos_renyi_game) creates a graph and [`igraph_destroy()`](https://igraph.org/c/doc/igraph-Basic.html#igraph_destroy) destroys it, i.e. deallocates the memory associated to it.

To compile this program, you need a C compiler. Optionally, [CMake](https://cmake.org/) can be used to automate the compilation.

### Compiling with CMake

It is convenient to use CMake because it can automatically discover the necessary compilation flags on all operating systems. Many IDEs support CMake, and can work with CMake projects directly. To create a CMake project for this example program, create a file name `CMakeLists.txt` with the following contents:

```auto
cmake_minimum_required(VERSION 3.16)
project(igraph_test C)

find_package(igraph REQUIRED)

add_executable(igraph_test igraph_test.c)
target_link_libraries(igraph_test PUBLIC igraph::igraph)

```

To compile the project, create a new directory called `build`, and switch to it:

```auto
mkdir build && cd build

```

Run CMake to configure the project:

```auto
cmake ..

```

If igraph was installed at a non-standard location, specify its prefix using the `-DCMAKE_PREFIX_PATH=...` option. The prefix must be the same directory that was specified as the `CMAKE_INSTALL_PREFIX` when compiling igraph.

If configuration has succeeded, build the program using

```auto
cmake --build .

```

### Compiling without CMake

On most Unix-like systems, the default C compiler is called `cc`. To compile the test program, you will need a command similar to the following:

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

```

The exact form depends on where **igraph** was installed on your system, whether it was compiled as a shared or static library, and the external libraries it was linked to. 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.a` (static library on Linux or macOS), `libigraph.so` (shared library on Linux), `libigraph.dylib` (shared library macOS), `igraph.lib` (static library on Windows) or `igraph.dll` (shared library on Windows). If **igraph** was compiled as a static library, it is also necessary to manually link to all of its dependencies.

If your system has the **pkg-config** utility you are likely to get the necessary compile options by issuing the command `pkg-config --libs --cflags igraph` (if igraph was built as a shared library) or `pkg-config --static --libs --cfgals igraph` (if igraph was built as a static library).

### Running the program

On most systems, the executable can be run by simply typing its name like this:

```auto
./igraph_test

```

If you use dynamic linking and the **igraph** library is not in a standard place, you may need to add its location to the `LD_LIBRARY_PATH` (Linux), `DYLD_LIBRARY_PATH` (macOS) or `PATH` (Windows) environment variables.

---

<div class="post-metadata">

**Author:** ![tamas](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/tamas/32/1261_2.png) [@tamas](https://igraph.discourse.group/u/tamas)\
**Post date:** [12 April 2021 13:47 UTC](https://igraph.discourse.group/t/igraph-tutorial-update/708/2 "2021-04-12T13:47:14Z")

</div>

Revision 3 was converted to DocBook and committed to the repo.

---

<div class="post-metadata">

**Author:** ![sony](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/sony/32/826_2.png) [@sony](https://igraph.discourse.group/u/sony)\
**Post date:** [24 August 2022 02:45 UTC](https://igraph.discourse.group/t/igraph-tutorial-update/708/3 "2022-08-24T02:45:05Z")

</div>

Thank you for your guidance!!  
I have followed all the steps through your tutorials.

Finally, I could run this cmd “cmake --build .” to generate the file and interact with igraph and my project.

However, After the running cmd is finished. I got another problem which I am not able to figure out whether it is because of my set-up or? Could you give me some idea?

Please kindly check about my latest error as follows:

 ![image](https://global.discourse-cdn.com/free1/uploads/igraph/original/1X/b04aa7889310d3bdeef1f0a1f9c1f93615a1d96f.png)

Thank you for your answer!

---

<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 August 2022 07:17 UTC](https://igraph.discourse.group/t/igraph-tutorial-update/708/4 "2022-08-24T07:17:35Z")

</div>

Please describe what you have done step by step, making sure not to omit any details, however minor they might seem to you.

Instructions for building igraph itself are here: [igraph Reference Manual](https://igraph.org/c/html/latest/igraph-Installation.html)

For building your own program that uses igraph, look here: [igraph Reference Manual](https://igraph.org/c/html/latest/igraph-Tutorial.html)
