I’m writing a small library for an image analysis task where I need to initialise some objects according to informations stored in files. Those files are shipped in with the library. However, in my code, I don’t know a proper way of referring to those external configuration files. Obviously I cannot refer to the full path of the configuration file. But the relative path seems also not an option, because I’m not sure whether this path should be relative to the location of the shared library, or to the location of the executable that is linked with the shared library, or again the working directory from which the executable is launched.
At the moment, my code would look like that:
void
myImageAnalysisFunctionExportedInASharedLibrary(cv::Mat &input, cv::Mat &output)
{
const std::string configurationFile = "resources/analyserConfig.txt";
Analyser a;
a.setConfiguration(configurationFile);
a.analyse(input, output);
}
I am coding in C++ under a Linux/Windows environment with OpenCV. I would rather like a portable solution which does not require reference to Boost or Qt. I use CMake as a project building environment.
regarding directory paths, under Windows put and read your config files from the %APPDATA% folder… it’s best to create your own subfolder there. for Linux, use $HOME… again create a subfolder there, and it’s best to prepend it with a period to make it hidden. both of these allow application-level permissions, whereas relative paths to application installation location is rarely allowed any more.