0

I've got shader files that are a part of library's source tree. It means that these files will be read at library's initialization stage. Although, when launching an application that uses this library - shader files can't be read. It is expected as shader files are not located in application's working directory. How should I solve this problem? I've tried copying built in shaders using this CMake custom command:

add_custom_command(
   TARGET ${SANDBOX_TARGET_NAME} POST_BUILD 
   COMMAND ${CMAKE_COMMAND} -E copy_directory
   ${CMAKE_CURRENT_SOURCE_DIR}/assets $<TARGET_FILE_DIR:${SANDBOX_TARGET_NAME}>/assets
)

This approach isn't that great because projects that will depend on my library should also always copy shader files to application working directory in order for my library to initilize properly.

In root CMakeLists.txt I set output directories for library and application:

set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) # library and built in shader files will be located here
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) # application will be located here

Built in shader files are added as library's sources and are copied to lib/ directory:

set(LIBRARY_SHADERS_SRCS
    ${CMAKE_CURRENT_SOURCE_DIR}/shader.frag
    ${CMAKE_CURRENT_SOURCE_DIR}/shader.vert
)

target_sources(${LIBRARY_TARGET_NAME} PRIVATE ${LIBRARY_SHADERS_SRCS})
  • That's not a cmake problem, but a problem your choice of locating the assets. Even if a program does come with the assets in a specific location relative to the exe, this won't help, since noone will prevent the user from running the program from a different working directory, e.g. by running it from the command line using `../../somedir/someprogram.exe`. [You can get the dll location at runtime though.](https://stackoverflow.com/questions/6924195/get-dll-path-at-runtime) Depending on whether the data can only be provided via files or as buffers too, the data could be stored in the dll too... – fabian Sep 07 '22 at 20:09
  • @fabian that doesn't really solve my problem as I will use it as static library. What is the industry's standard in shipping shaders with a game engine/game? – RafalMaziejuk Sep 09 '22 at 07:11

0 Answers0