views:

55

answers:

2

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. make install
  3. dist/myprogram.exe

That's okay if I'm working with the commandline 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 it's data directory even if it's not where the binary is? Or do you not use out of source builds?

+1  A: 

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.

fhd
A: 

And if copying the files takes too much time (they are images...) you could make it even better by creating a "custom" data_header.h with configure_file which contains the paths to the data still in your source-directory.

This is what I do: I have a file "global_build_config.h.in" in my source, containing the following:

const char* const global_testdatapath = "@Test_Data_Path@";

and then use configure_file in CMake:

# Assume CMake knows a variable Test_Data_Path, it will be filled in automatically
# in the generated config/Global_Build_Config.h
configure_file( Global_Build_Config.h.in ${CMAKE_BINARY_DIR}/config/Global_Build_Config.h )
# The config directory should be added as a include-searchpath
include_directories( ${CMAKE_BINARY_DIR}/config/ )

I can then #include "Global_Build_Config.h" in my cpp files and refer to the fixed path.

Andre