将graphviz库正确链接到C ++

时间:2018-10-03 20:11:37

标签: c++ libraries graphviz qmake

我正在使用QtCreator开发一个程序,在其中使用graphviz2.38。

我做了一个简单的例子来说明。只需考虑一下将SVG文件中的点文件转换为的小代码即可:

#include <QCoreApplication>
#include <graphviz/gvc.h>
#include <graphviz/gvplugin.h>
#include <graphviz/cgraph.h>
#include <graphviz/cdt.h>
#include<iostream>
using namespace std;

void saveImageGV(){

    GVC_t *gvc= gvContext();
    extern gvplugin_library_t gvplugin_dot_layout_LTX_library;
    gvAddLibrary(gvc, &gvplugin_dot_layout_LTX_library);
    FILE *fp = fopen(("ancestry.dot"), "r");
    Agraph_t *g = agread(fp,0);
    gvLayout(gvc, g, "dot");
    gvRender(gvc, g, "svg", fopen(("ancestry.svg"), "w"));
    gvFreeLayout(gvc, g);
    agclose(g);
    (gvFreeContext(gvc));
    return exit(0);
}


int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    saveImageGV();
    return a.exec();
}

编译选项如下:

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets printsupport


SOURCES += \
        main.cpp
QMAKE_LFLAGS += "-Wl,--rpath=lib/ "
QMAKE_LFLAGS += "-Wl,--dynamic-linker=lib/ld-linux-x86-64.so.2"

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
INCLUDEPATH += /usr/local/include/graphviz/

unix:!macx: LIBS += -L$$PWD/../../../../usr/lib/ -lcdt
INCLUDEPATH += $$PWD/../../../../usr/lib
DEPENDPATH += $$PWD/../../../../usr/lib

unix:!macx: LIBS += -L$$PWD/../../../../usr/lib/ -lcgraph
INCLUDEPATH += $$PWD/../../../../usr/lib
DEPENDPATH += $$PWD/../../../../usr/lib

unix:!macx: LIBS += -L$$PWD/../../../../usr/lib/ -lgvc
INCLUDEPATH += $$PWD/../../../../usr/lib
DEPENDPATH += $$PWD/../../../../usr/lib

unix:!macx: LIBS += -L$$PWD/../../../usr/lib/graphviz/ -lgvplugin_dot_layout
INCLUDEPATH += $$PWD/../../../usr/lib/graphviz
DEPENDPATH += $$PWD/../../../usr/lib/graphviz

unix:!macx: LIBS += -L$$PWD/../../../usr/lib/graphviz/ -lgvplugin_core
INCLUDEPATH += $$PWD/../../../usr/lib/graphviz
DEPENDPATH += $$PWD/../../../usr/lib/graphviz

unix:!macx: LIBS += -L$$PWD/../../../../usr/lib/ -lpathplan
INCLUDEPATH += $$PWD/../../../../usr/lib
DEPENDPATH += $$PWD/../../../../usr/lib

unix:!macx: LIBS += -L$$PWD/../../../../usr/lib/ -lxdot
INCLUDEPATH += $$PWD/../../../../usr/lib
DEPENDPATH += $$PWD/../../../../usr/lib

如果ancestry.dot需要,我还提供了一个graphviz点代码示例:

graph {
    a -- b;
    b -- c;
    a -- c;
    d -- c;
    e -- c;
    e -- a;
}

在我的lib文件夹中,我具有以下库:

ld-linux-x86-64.so.2  libc.so.6              libgvplugin_dot_layout.so.6  libxdot.so.4
libcdt.so.5           libgvc.so.6            libgvplugin_gd.so.6
libcgraph.so.6        libgvplugin_core.so.6  libpathplan.so.4

要在执行时加载这些库,我使用以下脚本:

#!/bin/sh
appname=`basename $0 | sed s,\.sh$,,`

dirname=`pwd`
lib=/lib/

chmod +x $dirname/$appname

LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$dirname$lib
echo $LD_LIBRARY_PATH

./$appname "$@"

当我执行我的代码时,我遇到了分段错误...对发生的事情有任何了解吗?

0 个答案:

没有答案