使用graphviz库配置CMake [英] Configuring CMake with graphviz library
问题描述
我正在尝试创建一个函数,该函数将从.dot文件中生成带有图形的图像,因此我必须使用graphviz库,这是此函数:
I'm trying to make a function which will generate an image with graph from .dot file, so i have to use graphviz library, here is this function:
#include <fstream>
#include <gvc.h>
bool draw_image(const string& path) {
GVC_t *gvc;
Agraph_t *gr;
FILE *fp;
gvc = gvContext();
fp = fopen((path + ".dot").c_str(), "r");
gr = agread(fp, nullptr);
gvLayout(gvc, gr, "dot");
gvRender(gvc, gr, "png", fopen((path + ".png").c_str(), "w"));
gvFreeLayout(gvc, gr);
agclose(gr);
return (gvFreeContext(gvc));
}
我正在使用clion IDE和cmake,所以这是我的 CMakelists.txt
:
I'm using clion IDE and cmake, so here is my CMakelists.txt
:
cmake_minimum_required(VERSION 3.16)
project(untitled)
set(CMAKE_CXX_STANDARD 14)
INCLUDE_DIRECTORIES(SYSTEM "/usr/include/graphviz/")
add_executable(untitled main.cpp)
TARGET_LINK_LIBRARIES(untitled LINK_PUBLIC "/usr/lib/x86_64-linux-gnu/libcgraph.so")
问题是,当我构建项目时,出现类似以下错误对"gvContext"的未定义引用
The problem is that when I build the project, I get errors like
undefined reference to 'gvContext'
但是clion可以看到此功能并正确地引用了 gvc.h
,所以我认为问题出在 CMakeLists.txt
But clion can see this functions and refers to gvc.h
properly, so I think the problem is in CMakeLists.txt
我不明白如何正确引用CMake中的外部库以及指定哪个文件.那我该怎么办呢?
I can't understand how to properly refer to an external library in CMake and which file to specify. So how should I do that?
推荐答案
gvContext
在 libgvc.so
中,因此最后的CMake行应显示为:
gvContext
is in libgvc.so
, so your final CMake line should read:
target_link_libraries(untitled PUBLIC cgraph gvc)
这利用了 target_link_libraries
文档.
This makes use of the "A plain library name" section of the target_link_libraries
documentation.
这篇关于使用graphviz库配置CMake的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!