Frage

In my GNU/Linux environment, my software would have all of its graphical resources installed under /usr/share/pixmaps. In Wintendo everything would go into C:\Program files\myappname. In a different OS, somewhere else.

What are the most common ways, conventions, good practices so I can abstract my way to refer to the files I open in my code in a paths/installation/build independant way? Basically a portable way to refer to resources paths.

War es hilfreich?

Lösung

Create a preprocessor ifelse chain of OS defines that #defines a function name mapped to a real function call that retrieves the correct path:

#if _WIN32
#define GetResourcesFolderString GetResourcesFolderStringW
#elif ((macintosh) || (Macintosh) || (__APPLE__ & __MACH__))
#define GetResourcesFolderString GetResourcesFolderStringM
#elif __gnu_linux__
#define GetResourcesFolderString GetResourcesFolderStringU
//...and so on
#endif

//...Later...

std::string GetResourcesFolderStringW() {
    return "C:/Program files/myappname/"
}
std::string GetResourcesFolderStringM() {
    return "Mac folder path here";
}
std::string GetResourcesFolderStringU() {
    return "/usr/share/pixmaps/";
}

This way you only call one function, and depending on your build target the correct function is called with no change in code.

Hard-coding the paths is generally not a good idea and is used here for illustration purposes only. You should probably set up a configuration file or similar to allow for individual systems to have separate install paths and still be able to find the resources.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top