# Undefined reference to \`\_imp\_\_agread'

**URL:** <https://forum.graphviz.org/t/undefined-reference-to-imp-agread/850>\
**Category:** Help\
**Created:** [September 27, 2021, 8:18pm UTC](https://forum.graphviz.org/t/undefined-reference-to-imp-agread/850 "2021-09-27T20:18:42Z")\
**Posts on this page:** 20\
**Page:** 3

<div class="post-metadata">

**Author:** ![magjac](https://sea2.discourse-cdn.com/graphviz/user_avatar/forum.graphviz.org/magjac/32/16_2.png) [@magjac](https://forum.graphviz.org/u/magjac)\
**Post date:** [September 28, 2021, 8:23pm UTC](https://forum.graphviz.org/t/undefined-reference-to-imp-agread/850/43 "2021-09-28T20:23:48Z")

</div>

Again, what happens? Any error messages or other output?

`test.dot` is correct anyway as you can see [here](http://magjac.com/graphviz-visual-editor/?dot=digraph%20ERD%20%7B%0Agraph%20%5B%20%20%20rankdir%20%3D%20%22LR%22%20%5D%3B%0Aranksep%3D2%3B%0A%22SCOTT.DEPT%22%20%5B%20label%3D%22%3CSCOTT.DEPT%3E%20SCOTT.DEPT%7C%3CPK_DEPT%3EDEPTNO%20%5Cl%20%7CDNAME%20%5Cl%20LOC%20%5Cl%20%22%20shape%20%3D%20%22record%22%20%5D%3B%0A%22SCOTT.EMP%22%20%5B%20label%3D%22%3CSCOTT.EMP%3E%20SCOTT.EMP%7C%3CFK_DEPTNO%3EDEPTNO%20%5Cl%20%7CEMPNO%20%5Cl%20ENAME%20%5Cl%20JOB%20%5Cl%20MGR%20%5Cl%20STARTDATE%20%5Cl%20SAL%20%5Cl%20COMM%20%5Cl%20%22%20shape%20%3D%20%22record%22%20%5D%3B%0A%22SCOTT.DEPT%22%3A%22PK_DEPT%22-%3E%22SCOTT.EMP%22%3A%22FK_DEPTNO%22%20%5Barrowhead%20%3D%20crow%5D%3B%7D).

---

<div class="post-metadata">

**Author:** ![student2](https://avatars.discourse-cdn.com/v4/letter/s/b9bd4f/32.png) [@student2](https://forum.graphviz.org/u/student2)\
**Post date:** [September 28, 2021, 8:37pm UTC](https://forum.graphviz.org/t/undefined-reference-to-imp-agread/850/44 "2021-09-28T20:37:07Z")

</div>

It’s just that no .png file is created in that directory which contains that graph image. This is what I am attempting to accomplish, so I’m wondering if my code does do that? Thanks!

---

<div class="post-metadata">

**Author:** ![magjac](https://sea2.discourse-cdn.com/graphviz/user_avatar/forum.graphviz.org/magjac/32/16_2.png) [@magjac](https://forum.graphviz.org/u/magjac)\
**Post date:** [September 28, 2021, 9:11pm UTC](https://forum.graphviz.org/t/undefined-reference-to-imp-agread/850/45 "2021-09-28T21:11:11Z")

</div>

I tried myself. It segfaults at `g = agread(fp, 0)` although fp is not NULL. Sorry. I have to call it a day now (I’m in CET). I’ll check in tomorrow to see how you’ve done.

---

<div class="post-metadata">

**Author:** ![student2](https://avatars.discourse-cdn.com/v4/letter/s/b9bd4f/32.png) [@student2](https://forum.graphviz.org/u/student2)\
**Post date:** [September 28, 2021, 11:24pm UTC](https://forum.graphviz.org/t/undefined-reference-to-imp-agread/850/46 "2021-09-28T23:24:51Z")

</div>

> [@magjac](#):
>
> segfaults at `g = agread(fp, 0)`

Thanks for your help! I actually didn’t even get that error printed to my console because I am using Powershell, so I will try to work on it.

---

<div class="post-metadata">

**Author:** ![smattr](https://sea2.discourse-cdn.com/graphviz/user_avatar/forum.graphviz.org/smattr/32/85_2.png) [@smattr](https://forum.graphviz.org/u/smattr)\
**Post date:** [September 29, 2021, 12:09am UTC](https://forum.graphviz.org/t/undefined-reference-to-imp-agread/850/47 "2021-09-29T00:09:16Z")

</div>

I don’t know what the error is offhand, but I would suggest introducing some error handling. E.g. `fopen` can fail. If you don’t check for failure, your program will blindly continue and likely not do what you expect.

---

<div class="post-metadata">

**Author:** ![student2](https://avatars.discourse-cdn.com/v4/letter/s/b9bd4f/32.png) [@student2](https://forum.graphviz.org/u/student2)\
**Post date:** [September 29, 2021, 12:24am UTC](https://forum.graphviz.org/t/undefined-reference-to-imp-agread/850/48 "2021-09-29T00:24:31Z")

</div>

Thanks! I did check whether fopen is failing using this, and determined that it is not:

```auto
if(fp == NULL){
        printf("Failed to open file/n");
    }
    else{
        printf("Opened file/n");
    }

```

Do you by any chance have any idea what agread would fail, given a valid file pointer?

---

<div class="post-metadata">

**Author:** ![smattr](https://sea2.discourse-cdn.com/graphviz/user_avatar/forum.graphviz.org/smattr/32/85_2.png) [@smattr](https://forum.graphviz.org/u/smattr)\
**Post date:** [September 29, 2021, 3:21am UTC](https://forum.graphviz.org/t/undefined-reference-to-imp-agread/850/49 "2021-09-29T03:21:41Z")

</div>

`agread` can fail for numerous reasons. Though they really shouldn’t result in a segfault.

Looking at dot.demo/demo.c in the repository, I have the impression you have to call both `gvContext` and `gvParseArgs` before calling and of the `ag*` functions:

```c
    /* set up a graphviz context */
    gvc = gvContext();

    /* parse command line args - minimally argv[0] sets layout engine */
    gvParseArgs(gvc, argc, argv);

    /* Create a simple digraph */
    g = agopen("g", Agdirected, 0);

```

I am mostly just guessing though. If you’re comfortable with it, you can use a debugger to figure out what’s going on. Options I’m aware of:

1. [GDB](https://www.gnu.org/software/gdb/). Likely your best bet if you’re in some Unix-y environment under Windows. Startup GDB, attaching to your process, `run` until you hit the segfault, then `bt` to get a stack trace of where you are.
2. [LLDB](https://lldb.llvm.org/). Does this work under Windows? Basically the same usage model as GDB.
3. [ASan](https://clang.llvm.org/docs/AddressSanitizer.html). Does this work under Windows?
4. [Valgrind](https://www.valgrind.org/). Almost certainly does not work under Windows ☹
5. [rr](https://rr-project.org/). Almost certainly does not work under Windows ☹

These may sound scary, but they’re not so bad. And if you’re writing C code, you’ll need to resort to a debugger sooner or later.

---

<div class="post-metadata">

**Author:** ![student](https://avatars.discourse-cdn.com/v4/letter/s/ecc23a/32.png) [@student](https://forum.graphviz.org/u/student)\
**Post date:** [September 29, 2021, 2:07pm UTC](https://forum.graphviz.org/t/undefined-reference-to-imp-agread/850/50 "2021-09-29T14:07:45Z")

</div>

Thanks! I was under the impression that gvParseArgs is used when you have user arguments you’d like to make note of. In my case, I don’t, so I believe I wouldn’t need to call that function? Not sure though…

Also, I’m using Powershell to run my program. Not sure if there are any good debuggers available for that?

The code snippet I am using I actually got from StackOverflow, here: [Generate image of GraphViz graph given dot text c++ - Stack Overflow](https://stackoverflow.com/questions/45225761/generate-image-of-graphviz-graph-given-dot-text-c)

It looks like:

```auto
bool saveImageGV(std::string file_path){
    GVC_t *gvc;
    Agraph_t *g;
    FILE *fp;
    gvc = gvContext();
    fp = fopen((file_path+".dot").c_str(), "r");
    g = agread(fp, 0);
    gvLayout(gvc, g, "dot");
    gvRender(gvc, g, "png", fopen((file_path+".png").c_str(), "w"));
    gvFreeLayout(gvc, g);
    agclose(g);
    return (gvFreeContext(gvc));
}

```

---

<div class="post-metadata">

**Author:** ![student](https://avatars.discourse-cdn.com/v4/letter/s/ecc23a/32.png) [@student](https://forum.graphviz.org/u/student)\
**Post date:** [September 29, 2021, 2:45pm UTC](https://forum.graphviz.org/t/undefined-reference-to-imp-agread/850/51 "2021-09-29T14:45:49Z")

</div>

After doing something like this instead:

```auto
void export_graph_string(){
    GVC_t *gvc;
    Agraph_t *g;
    gvc = gvContext();
    char source[] = "digraph { a -> b; b -> c; c -> d; d -> a; }";

    g = agmemread(source);
    gvLayout(gvc, g, "dot");
    gvRender(gvc, g, "png", fopen("C:/Users/claud/Documents/UWaterloo/4A/Roseseed/test.png", "w"));
    gvFreeLayout(gvc, g);
    agclose(g);
    gvFreeContext(gvc);
}

```

It is now failing on the gvRender() line. A png file is created in the right location, but when I attempt to open it my image viewer says the file format is unsupported or the file is corrupted. The source string is just a simple graph example I found on the Graphviz website…

---

<div class="post-metadata">

**Author:** ![schmoo2k](https://sea2.discourse-cdn.com/graphviz/user_avatar/forum.graphviz.org/schmoo2k/32/29_2.png) [@schmoo2k](https://forum.graphviz.org/u/schmoo2k)\
**Post date:** [September 29, 2021, 3:05pm UTC](https://forum.graphviz.org/t/undefined-reference-to-imp-agread/850/52 "2021-09-29T15:05:24Z")

</div>

FWIW - this is what I use:

```auto
extern gvplugin_library_t gvplugin_core_LTX_library;
extern gvplugin_library_t gvplugin_dot_layout_LTX_library;
extern gvplugin_library_t gvplugin_neato_layout_LTX_library;

const char doLayout(const char *src, const char *format, const char *engine)
{
    lastErrorStr[0] = '\0';

    GVC_t *context = gvContext();
    gvAddLibrary(context, &gvplugin_core_LTX_library);
    gvAddLibrary(context, &gvplugin_dot_layout_LTX_library);
    gvAddLibrary(context, &gvplugin_neato_layout_LTX_library);

    agseterr(AGERR);
    agseterrf(vizErrorf);

    agreadline(1);

    Agraph_t *graph;
    char *result = NULL;
    unsigned int length;
    while ((graph = agmemread(src)))
    {
        if (result == NULL)
        {
            gvLayout(context, graph, engine);
            gvRenderData(context, graph, format, &result, &length);
            gvFreeLayout(context, graph);
        }

        agclose(graph);

        src = "";
    }

    return result;
}

```

[hpcc-js-wasm/main.cpp at trunk · hpcc-systems/hpcc-js-wasm (github.com)](https://github.com/hpcc-systems/hpcc-js-wasm/blob/trunk/cpp/graphviz/graphvizlib/main.cpp)

I only support textual outputs (like SVG) so you may need some extra libs…

---

<div class="post-metadata">

**Author:** ![student](https://avatars.discourse-cdn.com/v4/letter/s/ecc23a/32.png) [@student](https://forum.graphviz.org/u/student)\
**Post date:** [September 29, 2021, 4:42pm UTC](https://forum.graphviz.org/t/undefined-reference-to-imp-agread/850/53 "2021-09-29T16:42:51Z")

</div>

Thanks! I do really need the output to be an image of some sort, thought. Like .png or .jpg.

In terms of this:

```auto
    gvAddLibrary(context, &gvplugin_core_LTX_library);
    gvAddLibrary(context, &gvplugin_dot_layout_LTX_library);
    gvAddLibrary(context, &gvplugin_neato_layout_LTX_library);

```

I’m not really sure what this does and when I integrate it, along with the extern headers, into my code, I get an ‘undefined reference to’ error.

---

<div class="post-metadata">

**Author:** ![magjac](https://sea2.discourse-cdn.com/graphviz/user_avatar/forum.graphviz.org/magjac/32/16_2.png) [@magjac](https://forum.graphviz.org/u/magjac)\
**Post date:** [September 29, 2021, 5:14pm UTC](https://forum.graphviz.org/t/undefined-reference-to-imp-agread/850/54 "2021-09-29T17:14:08Z")

</div>

> [@smattr](#):
>
> I have the impression you have to call both `gvContext` and `gvParseArgs` before calling and of the `ag*` functions:

No you shouldn’t have to do that. `libcgraph` where `agread` comes from is totally independent from `libgvc` where the `gv*` functions come from.

You _shouldn’t_ need to use `gvAddLibrary`. It’s only when you’re on a platform which does not support explicit loading of plugins as dynamic-link libraries, DLLs, (shared objects, `.so`-files on Linux) that you have to do that. That shouldn’t be the case here, but maybe it is.

If you do use Gordon’s method with built-in plugin, you would need to also use something like [`cmd/dot/dot_builtins.c`](https://gitlab.com/graphviz/graphviz/-/blob/main/cmd/dot/dot_builtins.c).

I would strongly advice against trying to run this from PowerShell, at least while debugging. It’s error handling is obscure, to say the least. I recommend that you run this from MINGW64 and make sure your PATH includes `C:/Program Files/Graphviz/bin` where the plugins reside together with the other DLLs.

Try to run the `dot` program to produce your png. If that succeeds, there shouldn’t be any problem with the libraries or plugins. Use `dot -?` for help.

---

<div class="post-metadata">

**Author:** ![student](https://avatars.discourse-cdn.com/v4/letter/s/ecc23a/32.png) [@student](https://forum.graphviz.org/u/student)\
**Post date:** [September 29, 2021, 5:18pm UTC](https://forum.graphviz.org/t/undefined-reference-to-imp-agread/850/55 "2021-09-29T17:18:57Z")

</div>

Thanks for clearing that up. Unfortunately, whenever I try to run the program from MingW64, I get the following error:

```auto
$ ./sqlpsql.exe -i
C:/users/claud/Documents/UWaterloo/4A/Roseseed/SQLPtoSQL/sqlpsql.exe: error while loading shared libraries: ?: cannot open shared object file: No such file or directory

```

So I’ve just been using Powershell instead. How can I run it? Also, does it support user interaction? I could not run it in Git Bash either because it would just display all the output at the end after quitting.

Do you by any chance have any other ideas about how to solve this seg fault? I’m still very confused…

---

<div class="post-metadata">

**Author:** ![magjac](https://sea2.discourse-cdn.com/graphviz/user_avatar/forum.graphviz.org/magjac/32/16_2.png) [@magjac](https://forum.graphviz.org/u/magjac)\
**Post date:** [September 29, 2021, 5:53pm UTC](https://forum.graphviz.org/t/undefined-reference-to-imp-agread/850/56 "2021-09-29T17:53:02Z")

</div>

That suggests to me that your PATH is not set correctly in MINGW64.

Try:

```auto
dot test.dot -T png -o test.png
file test.png

```

which for me gives:

```auto
test.png: PNG image data, 481 x 224, 8-bit/color RGB, non-interlaced

```

and the PNG looks like this:  
 ![test](https://global.discourse-cdn.com/graphviz/original/1X/36c98ec7b2a803db0a1ab860cf3d7e63a98809ec.png)

---

<div class="post-metadata">

**Author:** ![student](https://avatars.discourse-cdn.com/v4/letter/s/ecc23a/32.png) [@student](https://forum.graphviz.org/u/student)\
**Post date:** [September 29, 2021, 5:55pm UTC](https://forum.graphviz.org/t/undefined-reference-to-imp-agread/850/57 "2021-09-29T17:55:58Z")

</div>

Yes, great! That works. I was able to get my code to do that using this:

```auto
char *export_graph = "dot test.dot -T png -o test.png";
    system(export_graph);

```

Thanks for all your help! I really appreciate it.

---

<div class="post-metadata">

**Author:** ![magjac](https://sea2.discourse-cdn.com/graphviz/user_avatar/forum.graphviz.org/magjac/32/16_2.png) [@magjac](https://forum.graphviz.org/u/magjac)\
**Post date:** [September 29, 2021, 6:31pm UTC](https://forum.graphviz.org/t/undefined-reference-to-imp-agread/850/58 "2021-09-29T18:31:38Z")

</div>

If you’re satisfied with that then I’m glad to have been of help.

For anyone who is interested in how the story continues, I get what I think is the same problem with `gvRender` as Claudia describes above, with basically the same program:

```auto
#include <cgraph.h>
#include <gvc.h>
#include <stdio.h>
#include <stdlib.h>

void export_graph_string(){

    char source[] = "digraph { a -> b; b -> c; c -> d; d -> a; }";
    Agraph_t *g = agmemread(source);

    GVC_t *gvc = gvContext();
    gvLayout(gvc, g, "dot");

    FILE *fp;
    char outfile[] = "test.png";
    if ((fp = fopen(outfile, "w+")) == NULL) {
        fprintf(stderr, "Error opening %s for writing\n", outfile);
        exit(1);
    }

    fprintf(stderr, "Calling gvRender\n");
    fflush(stderr);

    gvRender(gvc, g, "png", fp);

    fprintf(stderr, "Returned from gvRender\n");
    fflush(stderr);

    gvFreeLayout(gvc, g);
    agclose(g);
    gvFreeContext(gvc);
}

int main() {
  export_graph_string();
}

```

Building and running it with gdb (installed with `pacman -S gdb`) gives:

```auto
/c/Users/magja/Documents/graphviz-help-850$ make
gcc -Wall -o sqlpsql SQLP.c -I"C:/Program Files/Graphviz/include/graphviz" -L"C:/Program Files/Graphviz/lib" -lgvc -lcgraph
/c/Users/magja/Documents/graphviz-help-850$ gdb sqlpsql.exe
GNU gdb (GDB) 10.2
Copyright (C) 2021 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-pc-msys".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<https://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
    <http://www.gnu.org/software/gdb/documentation/>.

For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from sqlpsql.exe...
(gdb) run
Starting program: /c/Users/magja/Documents/graphviz-help-850/sqlpsql.exe
[New Thread 14560.0x3068]
[New Thread 14560.0x3958]
[New Thread 14560.0x3bac]
Calling gvRender

Thread 1 received signal SIGSEGV, Segmentation fault.
0x00007ffbc1984ce1 in ntdll!RtlWaitOnAddress () from /c/WINDOWS/SYSTEM32/ntdll.dll
(gdb) bt
#0 0x00007ffbc1984ce1 in ntdll!RtlWaitOnAddress () from /c/WINDOWS/SYSTEM32/ntdll.dll
#1 0x00007ffbc199c4fc in ntdll!RtlEnterCriticalSection () from /c/WINDOWS/SYSTEM32/ntdll.dll
#2 0x00007ffbc199c362 in ntdll!RtlEnterCriticalSection () from /c/WINDOWS/SYSTEM32/ntdll.dll
#3 0x00007ffbbf6b4f82 in ucrtbase!fwrite () from /c/WINDOWS/System32/ucrtbase.dll
#4 0x00007ffbbf6b4f1c in ucrtbase!fwrite () from /c/WINDOWS/System32/ucrtbase.dll
#5 0x00007ffb994da2f6 in gvwrite () from /c/Program Files/Graphviz/bin/gvc.dll
#6 0x00007ffba8f82f02 in ?? () from /c/Program Files/Graphviz/bin/gvplugin_pango.dll
#7 0x00007ffb99020cb3 in cairo_surface_write_to_png () from /c/Program Files/Graphviz/bin/cairo.dll
#8 0x00007ffb99085c6d in cairo_append_path () from /c/Program Files/Graphviz/bin/cairo.dll
#9 0x00007ffb99078fc7 in cairo_append_path () from /c/Program Files/Graphviz/bin/cairo.dll
#10 0x00007ffb990791d7 in cairo_append_path () from /c/Program Files/Graphviz/bin/cairo.dll
#11 0x00007ffb99020a7d in cairo_pdf_surface_set_size () from /c/Program Files/Graphviz/bin/cairo.dll
#12 0x00007ffb99020d36 in cairo_surface_write_to_png_stream () from /c/Program Files/Graphviz/bin/cairo.dll
#13 0x00007ffba8f82440 in ?? () from /c/Program Files/Graphviz/bin/gvplugin_pango.dll
#14 0x00007ffb99499d19 in gvc!emit_once () from /c/Program Files/Graphviz/bin/gvc.dll
#15 0x00007ffb9949958b in gvc!emit_graph () from /c/Program Files/Graphviz/bin/gvc.dll
#16 0x00007ffb9949af59 in gvc!gvRenderJobs () from /c/Program Files/Graphviz/bin/gvc.dll
#17 0x00007ffb994d83f3 in gvc!gvRender () from /c/Program Files/Graphviz/bin/gvc.dll
#18 0x00007ff6d47f16b7 in export_graph_string ()
#19 0x00007ff6d47f1737 in main ()
(gdb)

```

This is way above my pay grade so I hope someone else can chime in. It would be nice to get to the bottom of this.

---

<div class="post-metadata">

**Author:** ![smattr](https://sea2.discourse-cdn.com/graphviz/user_avatar/forum.graphviz.org/smattr/32/85_2.png) [@smattr](https://forum.graphviz.org/u/smattr)\
**Post date:** [September 30, 2021, 2:53pm UTC](https://forum.graphviz.org/t/undefined-reference-to-imp-agread/850/59 "2021-09-30T14:53:06Z")

</div>

Looks like `gvwrite` is invoked with an invalid pointer or buffer length. Is this reproducible on Linux? If so, I can investigate further.

---

<div class="post-metadata">

**Author:** ![magjac](https://sea2.discourse-cdn.com/graphviz/user_avatar/forum.graphviz.org/magjac/32/16_2.png) [@magjac](https://forum.graphviz.org/u/magjac)\
**Post date:** [September 30, 2021, 4:19pm UTC](https://forum.graphviz.org/t/undefined-reference-to-imp-agread/850/60 "2021-09-30T16:19:26Z")

</div>

No, it isn’t. It render this:  
 ![test](https://global.discourse-cdn.com/graphviz/original/1X/f4ae80dc4d3e86362d6f0a53cecf3faa1be3549c.png)

---

<div class="post-metadata">

**Author:** ![scnorth](https://sea2.discourse-cdn.com/graphviz/user_avatar/forum.graphviz.org/scnorth/32/89_2.png) [@scnorth](https://forum.graphviz.org/u/scnorth)\
**Post date:** [October 1, 2021, 1:31pm UTC](https://forum.graphviz.org/t/undefined-reference-to-imp-agread/850/61 "2021-10-01T13:31:36Z")

</div>

This is way, way way above my knowledge of Windows, but I think years ago you had to be careful about opening files for writing in raw vs. text format, maybe something to do with newlines or other processing with text output. Sorry I can’t be more helpful and apologies if this is just misinformation now.

---

<div class="post-metadata">

**Author:** ![smattr](https://sea2.discourse-cdn.com/graphviz/user_avatar/forum.graphviz.org/smattr/32/85_2.png) [@smattr](https://forum.graphviz.org/u/smattr)\
**Post date:** [October 1, 2021, 2:59pm UTC](https://forum.graphviz.org/t/undefined-reference-to-imp-agread/850/62 "2021-10-01T14:59:16Z")

</div>

> [@scnorth](#):
>
> I think years ago you had to be careful about opening files for writing in raw vs. text format

Hm yes, interesting point. The `fopen` mode can also include `t` (text mode) or `b` (binary mode). This has no effect on Linux, but seems to be relevant on Windows. From [fopen, \_wfopen | Microsoft Learn](https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/fopen-wfopen?view=msvc-160) the default can be checked in `_fmode`, but regardless of what it is you’re going to have problems with the above code.

I believe the input file needs to be opened as `"rt"` and the output file as `"wb"`. Not sure if this is the root cause, but it might be something to try.

[Previous page](https://forum.graphviz.org/t/undefined-reference-to-imp-agread/850.md?page=2)

[Next page](https://forum.graphviz.org/t/undefined-reference-to-imp-agread/850.md?page=4)
