c - 为什么 cgraph 库中的 agwrite 函数在除 Win64 版本之外的任何配置/平台上意外失败?

标签 c graph graphviz

我一直在尝试让 cgraph ( https://graphviz.gitlab.io/_pages/pdf/cgraph.pdf) 正常工作,所以我读写了一些图形文件。我尝试编写一些非常基本的代码:

#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <float.h>
#include <limits.h>
#include <math.h>
#include <memory.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <Windows.h>
#include <mysql.h>

#include <graphviz/cgraph.h>

int main() {

    FILE *fp = NULL;
    fp = fopen("test.dot", "w+");

    if (fp == NULL) {
       return -1;
    }

    Agraph_t *g;
    g = agopen("test", Agdirected, NULL);

    Agnode_t *signal1;
    signal1 = agnode(g, "Signal1_ON", TRUE);

    Agnode_t *signal2;
    signal2 = agnode(g, "Signal1_OFF", TRUE);

    Agedge_t *link = agedge(g, signal1, signal2, "link1", TRUE);
    agattr(g, AGEDGE, "label", "transitionlink");

    agwrite(g, fp);

    fclose(fp);

    system("pause");

    return 0;
}

应该发生的是文件应该写入 test.dot。此代码在 Win64 版本上运行良好,但在 Win64 调试、Win32 调试和 Win32 版本上失败。我仔细检查了 Visual Studio 和文件目录中的 .lib 文件和 .dll 文件设置,确保正确复制每个平台的发布和调试版本。但是,agwrite 在 Win64 调试、Win32 调试和 Win32 版本上不断导致“Microsoft Visual Studio C Runtime Library 检测到 fatal error ”崩溃。奇怪的是如果我改变 agwrite(g, fp);agwrite(g, stdout);,代码适用于所有平台/配置。我很困惑为什么会这样。如果有帮助,这里是包含 agwrite 代码的源文件:https://github.com/ellson/MOTHBALLED-graphviz/blob/master/lib/cgraph/write.c

我无法调试问题,因为源代码已编译为每个平台/配置的 .dll 和 .lib。

我感谢任何建议/反馈, 谢谢

编辑:

对于任何虔诚地尝试在他们自己的系统上运行它的人来说,这里是我所有的二进制文件、库和包含文件:https://www.dropbox.com/sh/o9tjz7txu4m0k5q/AAAnp8Wu99q9IsFN7kvqZP7Ta?dl=0

编辑 2:

我使用的编译器是 Windows 10 上的 MSVC 14。

最佳答案

我发现在尝试使用 agwrite() 时直接使用 cgraph 会导致错误。解决方案是使用 Graphviz C API 附带的 GVC 抽象层来执行文件 I/O。这是有效的代码:

#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <float.h>
#include <limits.h>
#include <math.h>
#include <memory.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <Windows.h>
#include <mysql.h>

#include <graphviz/gvc.h>

int main() {

    GVC_t *gvc;
    gvc = gvContext();

    Agraph_t *g;
    g = agopen("test", Agdirected, NULL);

    Agnode_t *signal1;
    signal1 = agnode(g, "Signal1_ON", TRUE);

    Agnode_t *signal2;
    signal2 = agnode(g, "Signal1_OFF", TRUE);

    Agedge_t *link = agedge(g, signal1, signal2, "link1", TRUE);
    agattr(g, AGEDGE, "label", "transitionlink");

    gvLayout(gvc, g, "dot");
    gvRenderFilename(gvc, g, "dot", "test.dot");
    gvFreeLayout(gvc, g);

    agclose(g);

    gvFreeContext(gvc);

    system("pause");

    return 0;
}

编辑:

这是 GVC 的文档:https://graphviz.gitlab.io/_pages/pdf/gvc.3.pdf

关于c - 为什么 cgraph 库中的 agwrite 函数在除 Win64 版本之外的任何配置/平台上意外失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51434807/

相关文章:

graph - OCaml 有向图顶点模块

algorithm - 可供使用的图表数据来源

command-line - 在不创建中间文件的情况下显示来自 Graphviz 的图像?

c - 为什么C语言中没有split函数?

c - c 多线程编程中的 pthread_create 参数

android - 如何使用 MPAndroidChart 创建带有分组条形的条形图?

html - 设置 org-babel 生成的点图的宽度

prolog - 如何使用 gvterm 生成点图

python - 用数组成员包装 C 结构以便在 python : SWIG? cython 中访问?类型?

c - 迭代 GList 的宏