CMakePCHCompiler icon indicating copy to clipboard operation
CMakePCHCompiler copied to clipboard

OpenGL::GL causes build failure

Open rpav opened this issue 5 years ago • 2 comments

For some reason, using target_link_libraries(... OpenGL::GL) causes -isystem /usr/include to be added to the PCH build, which apparently causes breakage:

/usr/include/c++/8.3.0/cstdlib:75:15: fatal error: stdlib.h: No such file or directory
 #include_next <stdlib.h>
               ^~~~~~~~~~
compilation terminated.

This doesn't seem to be added to the actual build (e.g., does not occur in compile_commands.json).

Minimal example

Minimal CMakeLists.txt example reproducing bug or showing requested feature:

cmake_minimum_required(VERSION 3.11)

list(APPEND CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/CMake/CMakePCHCompiler)

project(test CXX CXXPCH)

find_package(OpenGL REQUIRED COMPONENTS OpenGL)

add_executable(demo demo.cpp)
target_link_libraries(demo PRIVATE
  OpenGL::GL
  )

target_precompiled_header(demo prefix.h)

Of course, one must have some minimal prefix.h:

#include <cstdlib>

This is the only difference:

$ /usr/bin/g++   -isystem /usr/include  -O3 -DNDEBUG   -x c++-header -o CMakeFiles/demo.pch.dir/prefix.h.gch -c /home/rpav/tmp/test/prefix.h


In file included from /home/rpav/tmp/test/prefix.h:1:
/usr/include/c++/8.3.0/cstdlib:75:15: fatal error: stdlib.h: No such file or directory
 #include_next <stdlib.h>
               ^~~~~~~~~~
compilation terminated.
$ /usr/bin/g++  -O3 -DNDEBUG   -x c++-header -o CMakeFiles/demo.pch.dir/prefix.h.gch -c /home/rpav/tmp/test/prefix.h
$

Additional information

  • OS: Linux (Arch, current)
  • CMake version 3.14.4
  • Compiler type and version: gcc-8.3 (also clang-8.0)
  • Additional Libraries used: Platform GL (nvidia)

rpav avatar May 16 '19 16:05 rpav

Gah, it occurred to me after submitting to search for /usr/include .. this appears similar to #33. I assume this is a cmake bug then, as FindOpenGL is part of the official dist?

~In any case, my temporary solution is just:~ (Incomplete, see below)

get_target_property(_glincludes OpenGL::GL INTERFACE_INCLUDE_DIRECTORIES)
list(REMOVE_ITEM _glincludes "/usr/include")
set_target_properties(OpenGL::GL PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "${_glincludes}")

I assume this ought to apply to any other erroneous target.

rpav avatar May 16 '19 17:05 rpav

I spoke too soon; that alone does not fix it. However, this does:

function(FixUsrInclude TARGET)
  get_target_property(_includes ${TARGET} INTERFACE_INCLUDE_DIRECTORIES)
  list(REMOVE_ITEM _includes "/usr/include")
  set_target_properties(${TARGET} PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "${_includes}")

  get_target_property(_deps "${TARGET}" INTERFACE_LINK_LIBRARIES)

  if(NOT "${_deps}" STREQUAL "_deps-NOTFOUND")
    foreach(_dep ${_deps})
      FixUsrInclude(${_dep})
    endforeach()
  endif()
endfunction()

find_package(OpenGL REQUIRED COMPONENTS OpenGL)
FixUsrInclude(OpenGL::GL)

rpav avatar May 16 '19 17:05 rpav