How does one properly use file paths in source code (relative to the project root) when building with cmake?
Details: I have a cmake project with basically this layout:
project
|-src/
| |-main.c
| |-CMakeLists.c
|-dat/
| |-foo.txt
|-build/
main.c
basically contains a main
function that tries to open a file, specified relative to the project's root directory, i.e., dat/foo.txt
. When building with cmake, the binaries are located in project/build/src
, which is also the working directory when running the program. dat/foo.txt
is interpreted as project/build/src/dat/foo.txt
which obviously is not available. One could replace the path in the source file with ../../dat/foo.txt
, which however is not at all a clean and nice solution.
This stackoverflow post states that there is no easy way for setting the working directory of code executed by cmake (furthermore, the answer refers to a solution for Visual Studio, which I am not using).
What is the best practice for this problem?