How to best handle data files with CMake?

theone picture theone · Aug 7, 2010 · Viewed 7.5k times · Source

I've got a CMake project that contains code and a few data files (images to be precise).

My directory structure is like this:

  • src
  • data

src contains the source code, data the data files. CMake suggests out of source builds, so when I invoke make, I have the executable program, but not the data files, thus I cannot execute the program.

Of course, make install would copy my data files to the required location and make it work, therefore I develop like this right now:

  1. cmake -DCMAKE_INSTALL_DIR=dist
  2. <edit source code>
  3. make install
  4. dist/myprogram.exe

That's okay if I'm working with the command line and an editor, but I recently decided to move to Eclipse CDT. Generating an Eclipse project from CMake works great, but manually executing the install target from Eclipse is not so nice.

How do you people tackle this problem? Does your program have some clever algorithms to try and find its data directory even if it's not where the binary is? Or do you not use out of source builds?

Answer

fhd picture fhd · Aug 7, 2010

configure_file should solve that problem.

I have a CMakeLists.txt file in my data directory which contains the following:

configure_file(data_file ${CMAKE_CURRENT_BINARY_DIR}/data_file COPYONLY)

This copies the specified file into the build directory when cmake is invoked, so it is available in the same location even in out of source builds.

configure_file does not support directories however while the file command does:

file(COPY assets DESTINATION ${CMAKE_CURRENT_BINARY_DIR})