8

My project's directory structure is basically as follows:

root/src

root/assets

root/library

I currently have CMake set up to compile the source, compile the library, and then link them, by calling make from the root directory.

I then have to manually move the executable into the original assets directory to get it to run, since that's where it expects to be (and we want to test with our directory structure in assets as close to what we expect it to be when it's done).

So, is there any way to tell CMake to automatically stick the compiled binary in that directory, as well as copy the assets over? Since we're doing out of source builds, sticking the executable back into the original project source's assets folder seems odd.

In short, two questions: Is there any way to get CMake to copy assets as well as code, and is there any way to have it copy the generated executable to a specific location in the build tree?

Any help would be appreciated --- thank you!

Kozaki
  • 615
  • 2
  • 7
  • 13

1 Answers1

10

Here's a simple example with a structure like yours:

  • root/src/main.cpp (only source file)
  • root/assets (where I want the executable to go)

Here's the cmake file:

PROJECT(HelloCMake)
SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${HelloCMake_SOURCE_DIR}/assets)
add_executable (HelloCMake src/main.cpp) 

When I build against this using Visual Studio I get the output placed in root/assets/debug. I'd have to dig to figure out how to get rid of the extra configuration folder (debug). Not perfect, but hopefully that gets you on the right track.

Edit...Even better:

INSTALL(TARGETS HelloCMake DESTINATION ${HelloCMake_SOURCE_DIR}/assets)
Darryl
  • 5,907
  • 1
  • 25
  • 36
  • This seems to work perfectly for our project. "make install" and VS both stick the stuff in the right place, it seems! (Only very minor thought about it is that it seems like it's mis-using the install command, which I'm somewhat unfamiliar with, but it works! Not sure how else it'd be done.) – Kozaki Feb 09 '11 at 06:07
  • This is gold for Cmake projects with certain complexity – CoffeDeveloper Oct 20 '15 at 14:57