Oragon Efreet
Oragon Efreet

Reputation: 1134

Add files with target using CMake

I am working using CMake on a little C project using OpenGL. To be able to run, my executable needs to access some resources files such as 3D meshes, textures or shader program sources.

When I run the generated executable, the current folder is the directory where it is created. This directory may differ depending on the binary tree location (out of source ? insource ? anywhere in the coputer). But my resources are located near my source tree.

I would like my CMakeLists.txt to copy the resource folder in my executable output directory but I have not a good idea of the way to do that. Besides, I am not sure this is a "best practice" of CMake.

Thank you for reading :)

Upvotes: 2

Views: 1552

Answers (1)

opatry
opatry

Reputation: 151

You have 2 useful variable to do so: CMAKE_CURRENT_BINARY_DIR and CMAKE_BINARY_DIR, the former refers to the current CMakeLists.txt output directory, the latter refers to the top level project output directory.

Most of the time, you handle resources near the executable depending on it, then you'll certainly want to refer to CMAKE_CURRENT_BINARY_DIR.

configure_file(
  "MyResourceDir/myresource"
  "${CMAKE_CURRENT_BINARY_DIR}/" COPYONLY
)

This command will copy resource of the CURRENT_CMAKE_SOURCE_DIR/MyResourceDir named myresource in the directory matching the current CMakeLists.txt.

You can glob files of your MyResourceDir and loop on it (maybe there is also some function to copy directory instead of list of files).

Upvotes: 4

Related Questions