获得当前操作系统的最佳方法是什么?

时间:2015-02-18 00:02:30

标签: nim

我正在寻找类似于python的sys.platform,它返回' linux',' windows'等等,甚至更好,类似于python的平台模块,为您提供操作系统,分发版本,发布版本等。

1 个答案:

答案 0 :(得分:6)

when defined windows:
  echo "I'm on Windows!"
elif defined linux:
  echo "I'm on Linux!"
else:
  echo "I'm on some other platform!"

when defined x86:
  echo "x86 specific code!"
elif defined amd64:
  echo "amd64 specific code!"
else:
  echo "generic code!"

when (NimMajor,NimMinor,NimPatch) > (0,10,2):
  echo "Such a modern Nim version!"

echo "OS: ", hostOS, ", CPU: ", hostCPU, ", cpuEndian: ", cpuEndian, ", NimVersion: ", NimVersion

这在我的系统上打印:

I'm on Linux!
amd64 specific code!
Such a modern Nim version!
OS: linux, CPU: amd64, cpuEndian: littleEndian, NimVersion: 0.10.3