0

We recently started switching over from using plain visual studio projects to using proper CMake files. Previously we would have the "Content" folder in the solution root folder to allow our executables to access content from it using a relative path like "../Tiles/tileset1.png".

How could we make sure CMake copies the files correctly, or in some other way makes sure that our executables are able to find the content folder while debugging from Visual Studio without manually setting the working directory?

Layl Conway
  • 385
  • 9
  • 17

1 Answers1

0

I can think of a few different options:

  1. Have CMake put all your executables in the same folder, as described in this question. Then you can use ../Tiles or ../../Tiles or whatever as you've been doing. Note, however, that you might want to consider setting this on a per-target basis instead of globally, e.g., using:

    set_target_properties(
      my_target
      PROPERTIES
        RUNTIME_OUTPUT_DIRECTORY
          ${CMAKE_BINARY_DIR}/bin
    )
    

    Setting CMAKE_RUNTIME_OUTPUT_DIRECTORY works fine, but some people consider it to be the 'old' way of doing it. (Depending on your needs, you might also want to set LIBRARY_OUTPUT_DIRECTORY, and possibly ARCHIVE_OUTPUT_DIRECTORY.)

  2. Use an environmental variable (e.g., CONTENT_ROOT or some-such) to locate the resources. Hard-code a default that makes sense for production, but let developers override it for their particular work flow.

  3. Look into cross-platform resource libaries (something like Qt's QRC files, but perhaps not tied to Qt).
  4. Try the CMake modules listed in this FAQ answer to change Visual Studio's working/debugging directory.

Actually, a combination of 1 and 2 is probably your best bet...

Community
  • 1
  • 1
evadeflow
  • 4,704
  • 38
  • 51
  • After further research it seemed that whatever I would do wouldn't matter because Visual Studio still defaults the Working Directory to the project file root. I've decided to go with adding changing the working directory to the quick-start guide instead. Marking this as answer for reference for other people. – Layl Conway Dec 23 '13 at 19:20
  • @LaylConway, if you just want to set Visual Studio's working directory, you could try Ryan Pavlik's CMake modules for this (see item #4 [just added] in my answer). I looked at these awhile ago, but it seemed like overkill for our particular use case. Please comment here again if you decide to try it, I'd like to hear how easy/hard it is to set up. – evadeflow Dec 27 '13 at 20:14