Question Details

No question body available.

Tags

c++ cmake cmake-custom-command

Answers (1)

June 15, 2025 Score: 0 Rep: 33,352 Quality: Medium Completeness: 100%

There are a few ways to go about it.

  1. Make your executable lookup with respect to a. Current working directory b. Executable's own directory
    • Advantage: works irrespective of the current working directory executable is invoked from
  2. Symlink //assets directory to binary's //build/Debug, //build/Release, etc.

My personal choice is (1)(b) I usually do a POSTBUILD step with addcustomcommand(TARGET myexec ...), hard linking the built binary to project root where there's an assets directory.

addexecutable(myexecutable
  main.cpp
)

addcustomcommand(TARGET myexecutable POSTBUILD COMMAND ${CMAKECOMMAND} -E createhardlink $ ${PROJECTSOURCE_DIR}/$ VERBATIM )

Here's an actual example from one of my projects.