I’ve made some progress. The following program builds & runs fine in Visual Studio:
// TestGraphviz.cpp
//
#include "pch.h"
#include <iostream>
#define GVDLL // https://stackoverflow.com/questions/77733103/cant-use-graphviz-as-a-library-in-c
#include <F:\Program Files\Graphviz\include\graphviz\gvc.h>
int main()
{ char MyGraf[] = "MyGraf", label[] = "label", style[] = "style", fillcolor[] = "fillcolor", color[] = "color",
fontcolor[] = "fontcolor", shape[] = "shape", fontname[] = "fontname", fontsize[] = "fontsize", N1[] = "N1", N2[] = "N2",
empty[] = "";
std::cout << "Hello World!\n";
GVC_t *gvc = gvContext();
if(gvc == nullptr)
return 0;
std::cout << "\ngvc " << gvc << "\n";
Agraph_t *graf = agopen(MyGraf, Agdirected, nullptr);
if(graf == nullptr)
return 0;
std::cout << "graf " << graf << "\n";
Agsym_t *attr = agattr(graf, AGNODE, label, "Default label");
agattr(graf, AGNODE, style, "filled");
agattr(graf, AGNODE, fillcolor, "white");
agattr(graf, AGNODE, color, "black");
agattr(graf, AGNODE, fontcolor, "black");
agattr(graf, AGNODE, shape, "box");
agattr(graf, AGNODE, fontname, "Arial");
agattr(graf, AGNODE, fontsize, "12");
Agnode_t *n1 = agnode(graf, N1, 1);
if(n1 == nullptr)
return 0;
int r1 = agset(n1, label, "Node 1");
Agnode_t *n2 = agnode(graf, N2, 1);
if(n2 == nullptr)
return 0;
int r2 = agset(n2, label, "Node 2");
Agedge_t *e = agedge(graf, n1, n2, empty, true);
if(e == nullptr)
return 0;
gvLayout(gvc, graf, "dot");
std::cout << "n1 " << n1 << ND_label(n1)->text << " r1 " << r1 << "\n";
std::cout << "n2 " << n2 << ND_label(n2)->text << " r2 " << r2 << "\n";
std::cout << "\n" << ND_pos(n1) << ND_label(n1)->text << "\n";
return 0;
}
You need to link cgraph.lib
and gvc.lib
However, the same code crashes at gvLayout
when built with Qt 5.13. I link cgraph.lib
and gvc.lib
. I’ve tried with and without cgraph++.lib
, gvc++.lib
and cdt.lib
.
I’d attach the source and project files if they let me.
Any Qt’ers out there? Can you reproduce the problem?