在 Windows 中将 Graphviz 与 Clion 结合使用

Using Graphviz with Clion in Windows

我不熟悉在 C 中使用外部库,所以这可能是一个非常愚蠢的错误。当我尝试使用提供的 CMakeLists.txt 运行 下面的程序时,出现引用错误。任何人都可以看到问题是什么吗?

CMAKELists.txt

cmake_minimum_required(VERSION 3.6)
project(Learning)

set(CMAKE_CXX_FLAGS "${CMAKE_C_FLAGS}")
set(GRAPHVIZ_INCLUDE_DIR "C:\Program Files (x86)\Graphviz2.38\include\graphviz")

set(SOURCE_FILES main.c)
include_directories("${GRAPHVIZ_INCLUDE_DIR}")
add_executable(Learning ${SOURCE_FILES})

main.c

#include <gvc.h>
#include <cgraph.h>

int main() {
    Agraph_t *graph;
    Agnode_t *nodeA, *nodeB;
    Agedge_t *edge1;
    Agsym_t *symbol1;
    GVC_t *gvc;

    gvc = gvContext();
    graph = agopen( "graph", Agdirected, NULL);
    nodeA = agnode(graph, "nodeA", 1);
    nodeB = agnode(graph, "nodeB", 1);
    edge1 = agedge(graph, nodeA, nodeB, 0, 1);

    agsafeset(nodeA, "color", "red", "");
    gvLayoutJobs(gvc, graph);
    gvRenderJobs(gvc, graph);
    gvFreeLayout(gvc, graph);

    }

输出

"C:\Program Files\JetBrains\CLion 2017.2.1\bin\cmake\bin\cmake.exe" --build E:\Development\C\Learning\cmake-build-debug --target Learning -- -j 2
Scanning dependencies of target Learning
[ 50%] Building C object CMakeFiles/Learning.dir/main.c.obj
[100%] Linking C executable Learning.exe
CMakeFiles\Learning.dir/objects.a(main.c.obj): In function `main':
E:/Development/C/Learning/main.c:38: undefined reference to `gvContext'
E:/Development/C/Learning/main.c:39: undefined reference to `_imp__Agdirected'
E:/Development/C/Learning/main.c:39: undefined reference to `agopen'
E:/Development/C/Learning/main.c:40: undefined reference to `agnode'
E:/Development/C/Learning/main.c:41: undefined reference to `agnode'
E:/Development/C/Learning/main.c:42: undefined reference to `agedge'
E:/Development/C/Learning/main.c:44: undefined reference to `agsafeset'
E:/Development/C/Learning/main.c:45: undefined reference to `gvLayoutJobs'
E:/Development/C/Learning/main.c:46: undefined reference to `gvRenderJobs'
E:/Development/C/Learning/main.c:47: undefined reference to `gvFreeLayout'
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[3]: *** [Learning.exe] Error 1
CMakeFiles\Learning.dir\build.make:96: recipe for target 'Learning.exe' failed
mingw32-make.exe[2]: *** [CMakeFiles/Learning.dir/all] Error 2
CMakeFiles\Makefile2:66: recipe for target 'CMakeFiles/Learning.dir/all' failed
CMakeFiles\Makefile2:78: recipe for target 'CMakeFiles/Learning.dir/rule' failed
mingw32-make.exe[1]: *** [CMakeFiles/Learning.dir/rule] Error 2
mingw32-make.exe: *** [Learning] Error 2
Makefile:117: recipe for target 'Learning' failed

编辑 -- 编辑 cmakeslists 以添加 target_link_libraries 但随后出现以下错误

cmake_minimum_required(VERSION 3.6)
project(Learning)

set(CMAKE_CXX_FLAGS "${CMAKE_C_FLAGS}")
set(GRAPHVIZ_INCLUDE_DIR "C:\Program Files (x86)\Graphviz2.38\include\graphviz")
set(GRAPHVIZ_LIB_DIR "C:\Program Files (x86)\Graphviz2.38\lib\release\lib")

target_link_libraries( "${GRAPHVIZ_LIB_DIR}" )

set(SOURCE_FILES main.c)
include_directories("${GRAPHVIZ_INCLUDE_DIR}")
add_executable(Learning ${SOURCE_FILES})

导致这个错误

"C:\Program Files\JetBrains\CLion 2017.2.1\bin\cmake\bin\cmake.exe" -DCMAKE_BUILD_TYPE=Debug -G "CodeBlocks - MinGW Makefiles" E:\Development\C\Learning
CMake Error at CMakeLists.txt:8 (target_link_libraries):
  Cannot specify link libraries for target "C:\Program Files
  (x86)\Graphviz2.38\lib\release\lib" which is not built by this project.


-- Configuring incomplete, errors occurred!
See also "E:/Development/C/Learning/cmake-build-debug/CMakeFiles/CMakeOutput.log".

[Finished]

您需要 link 在 graphviz 库中使用 target_link_libraries(Learning path/to/graphviz.so)