[英]Configuring CMake with graphviz library
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:我正在尝试制作一个 function 它将生成一个带有来自.dot 文件的图形的图像,所以我必须使用 graphviz 库,这里是这个 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));
}
I'm using clion IDE and cmake, so here is my CMakelists.txt
:我正在使用 clion IDE 和 cmake,所以这是我的
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")
The problem is that when I build the project, I get errors like undefined reference to 'gvContext'
问题是,当我构建项目时,我收到诸如
undefined reference to 'gvContext'
类的错误
But clion can see this functions and refers to gvc.h
properly, so I think the problem is in CMakeLists.txt
但是 clion 可以看到这个函数并正确引用
gvc.h
,所以我认为问题出在CMakeLists.txt
I can't understand how to properly refer to an external library in CMake and which file to specify.我不明白如何正确引用 CMake 中的外部库以及要指定哪个文件。 So how should I do that?
那么我该怎么做呢?
gvContext
is in libgvc.so
, so your final CMake line should read: gvContext
在libgvc.so
中,因此您的最终 CMake 行应为:
target_link_libraries(untitled PUBLIC cgraph gvc)
This makes use of the "A plain library name" section of the target_link_libraries
documentation .这利用了
target_link_libraries
文档的“A plain library name”部分。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.