# How for a binary executable to locate a config file?

**URL:** <https://discourse.cmake.org/t/how-for-a-binary-executable-to-locate-a-config-file/5591>\
**Category:** Code\
**Created:** [May 2, 2022, 11:31am UTC](https://discourse.cmake.org/t/how-for-a-binary-executable-to-locate-a-config-file/5591 "2022-05-02T11:31:31Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ronald](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/r/ea666f/32.png) [@Ronald](https://discourse.cmake.org/u/Ronald)\
**Post date:** [May 2, 2022, 11:31am UTC](https://discourse.cmake.org/t/how-for-a-binary-executable-to-locate-a-config-file/5591/1 "2022-05-02T11:31:31Z")

</div>

When developing the config file is in one path, and after installation the config file is in another path.  
It’s a binary who locates the config file, so `configure_file` will not help.  
How to locate the config file in both cases?  
Hard coding all the possible locations for the config file into the binary should help,  
but that means the path for the config file when developing also should be hard coded, which makes no sense after installation.

---

<div class="post-metadata">

**Author:** ![fenrir](https://discourse.cmake.org/user_avatar/discourse.cmake.org/fenrir/32/734_2.png) [@fenrir](https://discourse.cmake.org/u/fenrir)\
**Post date:** [May 2, 2022, 11:46am UTC](https://discourse.cmake.org/t/how-for-a-binary-executable-to-locate-a-config-file/5591/2 "2022-05-02T11:46:44Z")

</div>

It’s not really a CMake question, I guess…  
Anyway, I tend to provide a command line option or an environment variable so that one can simply tell the executable where to look for. If nothing is provided, the executable looks in predefined locations (like a path relative to itself, /etc/…, $XDG\_CONFIG\_HOME…

---

<div class="post-metadata">

**Author:** ![Ronald](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/r/ea666f/32.png) [@Ronald](https://discourse.cmake.org/u/Ronald)\
**Post date:** [May 2, 2022, 12:19pm UTC](https://discourse.cmake.org/t/how-for-a-binary-executable-to-locate-a-config-file/5591/3 "2022-05-02T12:19:05Z")

</div>

> [@fenrir](#):
>
> Anyway, I tend to provide a command line option or an environment variable so that one can simply tell the executable where to look for. If nothing is provided, the executable looks in predefined locations (like a path relative to itself, /etc/…, $XDG\_CONFIG\_HOME…

I think predefined locations are more self-contained, but predefined locations should be different for developing and deploying.

---

<div class="post-metadata">

**Author:** ![fenrir](https://discourse.cmake.org/user_avatar/discourse.cmake.org/fenrir/32/734_2.png) [@fenrir](https://discourse.cmake.org/u/fenrir)\
**Post date:** [May 2, 2022, 1:19pm UTC](https://discourse.cmake.org/t/how-for-a-binary-executable-to-locate-a-config-file/5591/4 "2022-05-02T13:19:52Z")

</div>

As I said - I just use parameters / environment variables to override. During development one has to constantly fiddle with config files, so it makes sense to have multiple and switch without copying files around.  
And ability to override config location in production is also usually a good idea.

In any case this is something YOU decide how to implement in YOUR code. You can have a #define in you code that hardcodes some location when you pass it in your CMake invocation or something like that.

---

<div class="post-metadata">

**Author:** ![Ronald](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/r/ea666f/32.png) [@Ronald](https://discourse.cmake.org/u/Ronald)\
**Post date:** [May 2, 2022, 5:21pm UTC](https://discourse.cmake.org/t/how-for-a-binary-executable-to-locate-a-config-file/5591/5 "2022-05-02T17:21:10Z")

</div>

Clear enough  
Thanks

---

<div class="post-metadata">

**Author:** ![triddlelover69](https://discourse.cmake.org/user_avatar/discourse.cmake.org/triddlelover69/32/1326_2.png) [@triddlelover69](https://discourse.cmake.org/u/triddlelover69)\
**Post date:** [May 20, 2022, 1:07pm UTC](https://discourse.cmake.org/t/how-for-a-binary-executable-to-locate-a-config-file/5591/6 "2022-05-20T13:07:17Z")

</div>

@fenrir Your answers are clear and good enough. Thanks!
