Search code examples
c++cmakeheader-filesgraphviz

Graphviz in cmake project build fails - header not found


I want to create an example Cmake project in C++ to see if Graphviz can be used in a big project for documentation purposes. My project structure is whereas I cloned graphviz from github into the external directory and performed git submodule update --init --recursive

Project
|external
|   graphviz
|main.cpp
|CMakeLists.txt

My Cmake file is

cmake_minimum_required(VERSION 3.17)
project(Example)
set(CMAKE_CXX_STANDARD 17)
set(SOURCES
    main.cpp
    )

add_subdirectory(external/graphviz)
add_executable(${PROJECT_NAME} ${SOURCES})

I use Windows 10 64bit, CLion and as compiler LLVM Clang and followed the guide http://graphviz.org/doc/winbuild.html, so I added c:\Projekte\Example\external\graphviz\windows\dependencies\graphviz-build-utilities to my Path environment variable. Because CMake failed in the beginning as it could not find Bison and Flex I added to the Cmake options -DBISON_EXECUTABLE="c:\Projekte\Example\external\graphviz\windows\dependencies\graphviz-build-utilities\winflexbison\win_bison.exe" -DFLEX_EXECUTABLE="c:\Projekte\Example\external\graphviz\windows\dependencies\graphviz-build-utilities\winflexbison\win_flex.exe" CMake runs now successfully and I could build the project, but it fails when I try to include the gvc header like this, although it shows no error on the include itself

#include "external/graphviz/lib/gvc/gvc.h"
int main() {
   //do stuff
   return 0;
}

This is the output

====================[ Build | Example | Debug_Clang ]===========================
"C:\Program Files\JetBrains\CLion 2020.3.2\bin\cmake\win\bin\cmake.exe" --build C:\Projekte\Example\cmake-build-debug_clang --target Example
[1/2] Building CXX object CMakeFiles\Example.dir\main.cpp.obj
FAILED: CMakeFiles/Example.dir/main.cpp.obj 
C:\PROGRA~1\LLVM\bin\clang-cl.exe  /nologo -TP   -m64 /Zi /Ob0 /Od /RTC1 -MDd -std:c++17  /showIncludes /FoCMakeFiles\Example.dir\main.cpp.obj /FdCMakeFiles\Example.dir\ -c -- ..\main.cpp
In file included from ..\main.cpp:4:
../external/graphviz/lib/gvc/gvc.h(14,10): fatal error: 'types.h' file not found
#include "types.h"
         ^~~~~~~~~
1 error generated.
ninja: build stopped: subcommand failed.

The file not found is located at external/graphviz/lib/common

What I tried

I assume it is a problem with Cmake, so I tried to change my Cmake file. By removing the add:subdirectory() command and replacing it with

1.

add_executable(${PROJECT_NAME} ${SOURCES})
target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/external/graphviz)
target_link_directories(${PROJECT_NAME} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/external/graphviz)
target_link_libraries(${PROJECT_NAME} PRIVATE ${GRAPHVIZ_LIB})
add_executable(${PROJECT_NAME} ${SOURCES})
target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/external/graphviz)
target_link_libraries(${PROJECT_NAME} PUBLIC cgraph gvc)
  1. instead of target_include_directories()
include_directories(SYSTEM ${CMAKE_CURRENT_SOURCE_DIR}/external/graphviz)
  1. adding to the Cmake options the library path -DCMAKE_PREFIX_PATH="c:\Projekte\Example\external\graphviz\lib"

But I always get the same error. Does anyone know a solution?


Solution

  • I asked the same question directly in graphviz support forum and got as answer, that it's a known issue with Cmake and linked me to this thread https://gitlab.com/graphviz/graphviz/-/issues/1477

    Tl:dr Graphviz is currently not buildable as as subdirectory in a Cmake project.