Is OS detection possible with GLib?

谁说我不能喝 提交于 2019-12-31 03:19:30

问题


Is it possible to determine on which platform (GNU/Linux, Win32, OS X) my Vala app is running?


回答1:


As Vala is a compiled language (as opposed to intermediate or interpreted) you can determine the platform using your favorite build tool and use conditional compilation.

Something like:

#if WINDOWS
    message ("Running on Windows");
#elif OSX
    message ("Running on OS X");
#elif LINUX
    message ("Running on GNU/Linux");
#elif POSIX
    message ("Running on other POSIX system");
#else
    message ("Running on unknown OS");
#endif

The build tool would have to pass -D LINUX, etc to the compiler.

I would be careful and only do something like this as a last resort, because it can backfire. Usually it's better to use cross platform libraries that already handle the differences for you.

BTW: See also how this is done in C++.



来源:https://stackoverflow.com/questions/29634474/is-os-detection-possible-with-glib

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!