# What is the best way to make data files available to my executable at runtime?

**URL:** <https://discourse.cmake.org/t/what-is-the-best-way-to-make-data-files-available-to-my-executable-at-runtime/4113>\
**Category:** Code\
**Tags:** os:linux\
**Created:** [September 19, 2021, 2:35am UTC](https://discourse.cmake.org/t/what-is-the-best-way-to-make-data-files-available-to-my-executable-at-runtime/4113 "2021-09-19T02:35:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chrinkus](https://discourse.cmake.org/user_avatar/discourse.cmake.org/chrinkus/32/1801_2.png) [@Chrinkus](https://discourse.cmake.org/u/Chrinkus)\
**Post date:** [September 19, 2021, 2:35am UTC](https://discourse.cmake.org/t/what-is-the-best-way-to-make-data-files-available-to-my-executable-at-runtime/4113/1 "2021-09-19T02:35:47Z")

</div>

I have a project that wants to read PNGs from a /images folder. I’ve done a few different things in the past but they all depend on where the executable is called from. My most recent attempt should make this clear:

```auto
Project tree:
- build/
- images/
    - cool_image.png
- src/
    - CMakeLists.txt
    - main.c
- CMakeLists.txt
- README.md

```

To build and run I:

```auto
$ cd build
$ cmake ..
$ cmake --build .
$ ./src/executable
ERROR: could not find /images/cool_image.png

```

My recent fix to this was to add the following line to my /src/CMakeLists.txt:

```auto
file(CREATE_LINK "${PROJECT_SOURCE_DIR}/images"
        "${CMAKE_CURRENT_BINARY_DIR}/images" SYMBOLIC)

```

This gets part of the way but it still requires me to be in a specific directory to run, namely the /build/src directory.

In the end, this automates the previous solution I had of just manually putting symlinks into my build folders. Is there a way to make these files available to my app no matter where it’s called from?

---

<div class="post-metadata">

**Author:** ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)\
**Post date:** [September 19, 2021, 11:40am UTC](https://discourse.cmake.org/t/what-is-the-best-way-to-make-data-files-available-to-my-executable-at-runtime/4113/2 "2021-09-19T11:40:24Z")

</div>

CMake can’t help much here itself. You can get the path to the current executable from platform-specific APIs. Once you have that, you can make sure that the resources are always at a static location relative to that binary in the build tree and the install tree. See `dlsym` on Unix and I can’t remember the Windows one off-hand.

---

<div class="post-metadata">

**Author:** ![Chrinkus](https://discourse.cmake.org/user_avatar/discourse.cmake.org/chrinkus/32/1801_2.png) [@Chrinkus](https://discourse.cmake.org/u/Chrinkus)\
**Post date:** [September 20, 2021, 2:09pm UTC](https://discourse.cmake.org/t/what-is-the-best-way-to-make-data-files-available-to-my-executable-at-runtime/4113/3 "2021-09-20T14:09:34Z")

</div>

Ah ok, so this is more of a thing where I would get the absolute path during install and hard code that. The location would be platform dependent. Gotcha.
