# Relocatability problem / where am I?

**URL:** <https://discourse.appimage.org/t/relocatability-problem-where-am-i/2336>\
**Category:** Creating AppImages\
**Created:** [May 9, 2021, 2:54pm UTC](https://discourse.appimage.org/t/relocatability-problem-where-am-i/2336 "2021-05-09T14:54:26Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![lynxlynxlynx](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.appimage.org/lynxlynxlynx/32/553_2.png) [@lynxlynxlynx](https://discourse.appimage.org/u/lynxlynxlynx)\
**Post date:** [May 9, 2021, 2:54pm UTC](https://discourse.appimage.org/t/relocatability-problem-where-am-i/2336/1 "2021-05-09T14:54:27Z")

</div>

Hey, I’m trying to package GemRB as an AppImage and the creation itself is pretty standard and seems to work fine:

```
$ cmake .. -DCMAKE_INSTALL_PREFIX=/usr -DCMAKE_BUILD_TYPE=Release -DSTATIC_LINK=on
$ make -j4
$ make install DESTDIR=./AppDir
$ LD_LIBRARY_PATH=./AppDir/usr/lib/gemrb linuxdeploy-x86_64.AppImage --appdir=./AppDir --output=appimage

```

The image runs, but I can’t get it to find stuff packaged in AppDir. I know AppRun [changes dir to usr](https://github.com/AppImage/AppImageKit/blob/8bbf694455d00f48d835f56afaa1dabcd9178ba6/src/AppRun.c#L159), but using a relative path doesn’t work:

```
$ strace ./GemRB-b672fc7b7-x86_64.AppImage |& grep unhardcoded
access("../share/gemrb/unhardcoded", R_OK) = -1 ENOENT (No such file or directory)
access("../share/gemrb/unhardcoded", R_OK) = -1 ENOENT (No such file or directory)
stat("../share/gemrb/unhardcoded/shared", 0x7ffd51de6208) = -1 ENOENT (No such file or directory)
[ResourceManager/WARNING]: Invalid path given: ../share/gemrb/unhardcoded/shared (shared GemRB Unhardcoded data)

```

The AppDir has two shared dirs (to be fixed later / temporary), but only the first one is key:

```
$ ls AppDir/*/*
AppDir/etc/gemrb:
GemRB.cfg GemRB.cfg.noinstall.sample GemRB.cfg.sample

AppDir/share/gemrb:
GUIScripts minimal override unhardcoded

AppDir/usr/bin:
extend2da.py gemrb

AppDir/usr/lib:
libs...

AppDir/usr/share:
applications doc icons man pixmaps

```

So since it’s supposed to be running from `AppDir/usr`, `../share/` should be correct. I also tried with `share/` and files copied to the second share dir, and deeper backtracking (since the binary is in usr/bin), but nothing works. GemRB itself doesn’t change the CWD.

Any clues?

---

<div class="post-metadata">

**Author:** ![probono](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.appimage.org/probono/32/81_2.png) [@probono](https://discourse.appimage.org/u/probono)\
**Post date:** [May 11, 2021, 5:38pm UTC](https://discourse.appimage.org/t/relocatability-problem-where-am-i/2336/2 "2021-05-11T17:38:57Z")

</div>

You can never know where the user “is” when your payload application gets executed. Hence, you should _not_ try to load resources from a path relative to the current working directory of the user (cwd) as this is unreliable.

Instead, you need to figure out at runtime where your main payload application “is”, and construct a path to the resources to be loaded using that information.

Please change the source code of the application so that it loads resources from an absolute path constructed based on the path of the main binary.  
For example, in Qt you can use `QString QCoreApplication::applicationDirPath())` to construct such a path.

---

<div class="post-metadata">

**Author:** ![lynxlynxlynx](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.appimage.org/lynxlynxlynx/32/553_2.png) [@lynxlynxlynx](https://discourse.appimage.org/u/lynxlynxlynx)\
**Post date:** [May 11, 2021, 6:21pm UTC](https://discourse.appimage.org/t/relocatability-problem-where-am-i/2336/3 "2021-05-11T18:21:46Z")

</div>

But isn’t the CWD in the appdir as far as we’re concerned? What’s the point of the cd in AppRun then?

It sounds like there’s no way to get AppImage-relative paths. I thought it worked like a chroot, hence the various assumptions. It’s odd AppRun doesn’t export `appdir` as an environmental variable, since it would eliminate the need for every payload to reimplement the same lookup. 😕

---

<div class="post-metadata">

**Author:** ![lynxlynxlynx](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.appimage.org/lynxlynxlynx/32/553_2.png) [@lynxlynxlynx](https://discourse.appimage.org/u/lynxlynxlynx)\
**Post date:** [May 21, 2021, 1:37pm UTC](https://discourse.appimage.org/t/relocatability-problem-where-am-i/2336/4 "2021-05-21T13:37:32Z")

</div>

Any further comment @probono? I’d like to avoid hardcoding too much to get appimage support, so better understanding is key.

---

<div class="post-metadata">

**Author:** ![mgord9518](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.appimage.org/mgord9518/32/459_2.png) [@mgord9518](https://discourse.appimage.org/u/mgord9518)\
**Post date:** [May 25, 2021, 8:03am UTC](https://discourse.appimage.org/t/relocatability-problem-where-am-i/2336/5 "2021-05-25T08:03:27Z")

</div>

AppImages export the environment variable “APPDIR”, which is the location the AppImage gets mounted to at runtime, is this what you’re referring to? It’ll be available in AppRun, and you can export it to the binaries, etc from there.

---

<div class="post-metadata">

**Author:** ![probono](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.appimage.org/probono/32/81_2.png) [@probono](https://discourse.appimage.org/u/probono)\
**Post date:** [June 19, 2021, 8:46am UTC](https://discourse.appimage.org/t/relocatability-problem-where-am-i/2336/6 "2021-06-19T08:46:02Z")

</div>

> [@lynxlynxlynx](#):
>
> But isn’t the CWD in the appdir as far as we’re concerned? What’s the point of the cd in AppRun then?

That’s a dirty hack for legacy applications. Don’t rely on it. Use something like `QCoreApplication::applicationDirPath()` to construct the path.

> **[QCoreApplication Class | Qt Core 6.6.0](https://doc.qt.io/qt-5/qcoreapplication.html#applicationDirPath)**
>
> The QCoreApplication class provides an event loop for Qt applications without UI.
