Search code examples
c++visual-studio-2022graphviz

Can't use Graphviz as a library in C++


I'm attempting to build the example "demo.c" from graphviz documentation in Visual Studio 2022.

But when I try to compile the code, I am receiving the following two build errors:

1 unresolved externals, unresolved external symbol Agdirected

Agdirected is used in line 33 from "demo.c".

I have already added the include directory in the project's Property Pages, along with the necessary libraries in the Link Library Dependencies.

Additional Include Directories: C:\Program Files\Graphviz\include\graphviz;%(AdditionalIncludeDirectories)

Additional Library Directorys: C:\Program Files\Graphviz\lib;%(AdditionalLibraryDirectories)

Additional Dependencies: gvc.lib;gvc++.lib;cgraph.lib;cgraph++.lib;cdt.lib


Solution

  • You have to define "GVDLL" before including gvc.h, e.g.:

    #define GVDLL
    #include <graphviz/gvc.h>
    ...
    

    Alternatively, you can add this define under "C/C++ - Preprocessor - Preprocessor Definitions" in your Visual Studio project.

    I've got the same error as you and adding that preprocessor directive solved it in my case.