0

Exactly the title. My library depends on a JSON file, and because I would not like to require the end user to have a copy, I have a generator that converts it to an array.

In my CMake file, I wrote a function that takes a resource file and generates a build step and filenames for the resulting generated files (line 2 of the snippet below). It has no problem generating the Makefiles, but when I go to run it, Make tells me that the header is not present. Why is this?

This is the CMake list file in question:

# Resource generator
function(pancake_rsrc_gen var_outputs src_name)
  set(abs_path "${CMAKE_CURRENT_SOURCE_DIR}/${src_name}")
  set(file_name)
  get_filename_component(file_name ${src_name} NAME)
  set(gen_src "${CMAKE_CURRENT_BINARY_DIR}/pancake_rsrc_${file_name}.cpp")
  set(gen_inc "${CMAKE_CURRENT_BINARY_DIR}/pancake_rsrc_${file_name}.hpp")
  
  set(${var_outputs} ${${var_outputs}} ${gen_src} ${gen_inc} PARENT_SCOPE)
  message(STATUS "Generated filename is: ${gen_src}")
  add_custom_command(
    OUTPUT ${gen_src} ${gen_inc}
    MAIN_DEPENDENCY ${abs_path}
    COMMAND python3 "${PROJECT_SOURCE_DIR}/scripts/rsrc_gen.py" -C "${gen_src}" -H "${gen_inc}" "${abs_path}"
  )
endfunction()

# Sources
set(pancake_sources 
  "common/movie.cpp" 
  "common/dwarf.cpp" 
  "common/expr/compile.cpp" 
  "common/expr/parse.cpp" 
)
if(${CMAKE_SYSTEM_NAME} STREQUAL "Windows")
  list(APPEND pancake_sources
    "windows/sm64.cpp"
  )
elseif(${CMAKE_SYSTEM_NAME} STREQUAL "Linux")
  list(APPEND pancake_sources 
    "linux/sm64.cpp"
  )
else()
  message(FATAL_ERROR "This library can only be compiled for Windows and Linux. I don't have a Mac, so I can't build Apple software.")
endif()

# Main header
set(pancake_export "${CMAKE_CURRENT_SOURCE_DIR}/export")
set(pancake_private "${CMAKE_CURRENT_SOURCE_DIR}/private")

# Generated sources
set(macro_defns_src)
pancake_rsrc_gen(macro_defns_src "resources/sm64_macro_defns.json")
list(APPEND pancake_sources ${macro_defns_src})

add_library(pancake STATIC ${pancake_sources})
set_target_properties(pancake PROPERTIES
  # Require C++17
  CXX_STANDARD 17 
  CXX_STANDARD_REQUIRED on 
)

# Include both public/private headers
target_include_directories(pancake 
  PUBLIC ${pancake_export} 
  PRIVATE ${pancake_private}
)

target_link_libraries(pancake LIEF::dep libdwarf::dep nlohmann_json::dep)

It's included in a subdirectory (src) if that matters.

itzjackyscode
  • 970
  • 9
  • 27
  • 2
    A [mcve] would be preferrable. Especially the include statement including the generated file would be of interest. Without knowing these details my guess would be that you simply forgot to add the directory you generate the files to as include directory. Adding files header files as sources in general only results in the file occuring in the IDE project, should you use cmake to generate one; include directories need to be specified even if a header file is part of the sources of a target... – fabian Jul 27 '21 at 21:24
  • @fabian Well that's something I didn't know about CMake... I'm used to Meson, where adding a header source file automatically adds its include path. – itzjackyscode Jul 27 '21 at 22:02

0 Answers0