OS 是否可以使用 GLib 进行检测?

Is OS detection possible with GLib?

是否可以确定我的 Vala 应用程序在哪个平台(GNU/Linux、Win32、OS X)上 运行?

由于 Vala 是一种编译语言(与中间语言或解释语言相对),您可以使用自己喜欢的构建工具和 use conditional compilation.

确定平台

类似于:

#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

构建工具必须将 -D LINUX 等传递给编译器。

我会小心谨慎,只在万不得已时才做这样的事情,因为它可能适得其反。通常最好使用已经为您处理差异的跨平台库。

顺便说一句:另见 how this is done in C++