LiamRyan
LiamRyan

Reputation: 1928

Using Graphviz with Clion in Windows

I'm new to using external libraries in C so this could be a very silly mistake. I get reference errors when I try to run the below program using the provided CMakeLists.txt. Can anyone see what the issue is?

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);

    }

output

"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

Edit -- edited cmakeslists to add target_link_libraries but then I get the error below

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})

Results in this error

"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]

Upvotes: 1

Views: 1370

Answers (1)

arrowd
arrowd

Reputation: 34421

You need to link in graphviz library using target_link_libraries(Learning path/to/graphviz.so).

Upvotes: 1

Related Questions