Cmake命令之cmake_host_system_information

本文介绍了CMake中的cmake_host_system_information命令,展示了如何查询主机名、核心数、内存、处理器和操作系统信息。通过示例代码展示了如何在CMakeLists.txt中使用这些功能并获取具体结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

代码在git

原文

一、命令格式

cmake_host_system_information(RESULT <variable> QUERY <key> ...)

  查询CMake运行环境的系统信息,<key>指定待查询的信息,可以有多个,查询结果存放在<variable>中。

  <key>可以是主机名称、内存信息、CPU信息等等,<key>的所有取值可以参考本文的第三章节更多细节

二、示例

# CMakeLists.txt
cmake_minimum_required(VERSION 3.20)
project(host_info)
cmake_host_system_information(RESULT var QUERY HOSTNAME)
message("hostname: ${var}")
cmake_host_system_information(RESULT var QUERY NUMBER_OF_LOGICAL_CORES NUMBER_OF_PHYSICAL_CORES)
message("cores: ${var}")
cmake_host_system_information(RESULT var QUERY TOTAL_VIRTUAL_MEMORY TOTAL_PHYSICAL_MEMORY)
message("memory(MB): ${var}")
cmake_host_system_information(RESULT var QUERY PROCESSOR_NAME)
message("processor name: ${var}")
cmake_host_system_information(RESULT var QUERY OS_NAME OS_RELEASE OS_VERSION OS_PLATFORM)
message("os information: ${var}")

  我的是macOS,执行结果如下:

hostname: myBookAir
cores: 4;2
memory(MB): 5120;4096
processor name: Intel(R) Core(TM) i5-5250U CPU @ 1.60GHz
os information: Mac OS X;10.13.6;17G14019;x86_64

三、更多细节

  <key>的取值如下:

  • NUMBER_OF_LOGICAL_CORES:逻辑核心数量。
  • NUMBER_OF_PHYSICAL_CORES:物理核心数量。
  • HOSTNAME:主机名称。
  • FQDN:主机域名全称。
  • TOTAL_VIRTUAL_MEMORY:总虚拟内存,单位是M
  • AVAILABLE_VIRTUAL_MEMORY:可用虚拟内存,单位是M
  • TOTAL_PHYSICAL_MEMORY:总物理内存,单位是M
  • AVAILABLE_PHYSICAL_MEMORY:可用物理内存,单位是M
  • IS_64BIT:如果处理器是64位,查询结果为1
  • HAS_FPU:如果处理器拥有浮点处理单元,查询结果为1
  • HAS_MMX:如果处理器支持MMX指令集,查询结果为1
  • HAS_MMX_PLUS:如果处理器支持Ext. MMX指令集,查询结果为1
  • HAS_SSE:如果处理器支持SSE指令集,查询结果为1
  • HAS_SSE2:如果处理器支持SSE2指令集,查询结果为1
  • HAS_SSE_FP:如果处理器支持SSE FP指令集,查询结果为1
  • HAS_SSE_MMX:如果处理器支持SSE MMX指令集,查询结果为1
  • HAS_AMD_3DNOW:如果处理器支持3DNow指令集,查询结果为1
  • HAS_AMD_3DNOW_PLUS:如果处理器支持3DNow+指令集,查询结果为1
  • HAS_IA64:如果IA64处理器可以模拟X86,查询结果为1
  • HAS_SERIAL_NUMBER:如果处理器有序列号,查询结果为1
  • PROCESSOR_SERIAL_NUMBER:处理器序列号。
  • PROCESSOR_NAME:可读的处理器全称。
  • OS_NAME:操作系统名称,也就是uname -s的输出,三大操作系统对应的名称是LinuxWindowsDarwinmasOS),也可以通过CMAKE_HOST_SYSTEM_NAME变量获取。
  • OS_RELEASE:操作系统子类型,例如Windows Professional
  • OS_VERSION:操作系统构建ID
  • OS_PLATFORM:处理器架构,Windows下可以通过PROCESSOR_ARCHITECTURE变量获取,Unix/Linux/macOS等平台可以通过uname -muname -p获取。也可以通过CMAKE_HOST_SYSTEM_PROCESSOR变量获取。

附录:参考文档
  1. https://cmake.org/cmake/help/latest/command/cmake_host_system_information.html
gyp ERR! find VS gyp ERR! find VS msvs_version not set from command line or npm config gyp ERR! find VS VCINSTALLDIR not set, not running in VS Command Prompt gyp ERR! find VS unknown version "undefined" found at "D:\Program Files\Microsoft Visual Studio\2022\Community" gyp ERR! find VS unknown version "undefined" found at "D:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools" gyp ERR! find VS could not find a version of Visual Studio 2017 or newer to use gyp ERR! find VS looking for Visual Studio 2015 gyp ERR! find VS - not found gyp ERR! find VS not looking for VS2013 as it is only supported up to Node.js 8 gyp ERR! find VS gyp ERR! find VS ************************************************************** gyp ERR! find VS You need to install the latest version of Visual Studio gyp ERR! find VS including the "Desktop development with C++" workload. gyp ERR! find VS For more information consult the documentation at: gyp ERR! find VS https://github.com/nodejs/node-gyp#on-windows gyp ERR! find VS ************************************************************** gyp ERR! find VS gyp ERR! configure error gyp ERR! stack Error: Could not find any Visual Studio installation to use gyp ERR! stack at VisualStudioFinder.fail (D:\A_NEW_FILE\mp-weixin\mayidaijia-xiaochengxu_1.0.1\node_modules\node-gyp\lib\find-visualstudio.js:121:47) gyp ERR! stack at D:\A_NEW_FILE\mp-weixin\mayidaijia-xiaochengxu_1.0.1\node_modules\node-gyp\lib\find-visualstudio.js:74:16 gyp ERR! stack at VisualStudioFinder.findVisualStudio2013 (D:\A_NEW_FILE\mp-weixin\mayidaijia-xiaochengxu_1.0.1\node_modules\node-gyp\lib\find-visualstudio.js:351:14) gyp ERR! stack at D:\A_NEW_FILE\mp-weixin\mayidaijia-xiaochengxu_1.0.1\node_modules\node-gyp\lib\find-visualstudio.js:70:14 gyp ERR! stack at D:\A_NEW_FILE\mp-weixin\mayidaijia-xiaochengxu_1.0.1\node_modules\node-gyp\lib\find-visualstudio.js:372:16 gyp ERR! stack at D:\A_NEW_FILE\mp-weixin\mayidaijia-xiaochengxu_1.0.1\node_modules\node-gyp\lib\util.js:54:7 gyp ERR! stack at D:\A_NEW_FILE\mp-weixin\mayidaijia-xiaochengxu_1.0.1\node_modules\node-gyp\lib\util.js:33:16 gyp ERR! stack at ChildProcess.exithandler (node:child_process:406:5) gyp ERR! stack at ChildProcess.emit (node:events:527:28) gyp ERR! stack at maybeClose (node:internal/child_process:1092:16) gyp ERR! System Windows_NT 10.0.26100 gyp ERR! command "D:\\MyApp\\nodejs\\node.exe" "D:\\A_NEW_FILE\\mp-weixin\\mayidaijia-xiaochengxu_1.0.1\\node_modules\\node-gyp\\bin\\node-gyp.js" "rebuild" "--verbose" "--libsass_ext=" "--libsass_cflags=" "--libsass_ldflags=" "--libsass_library=" gyp ERR! cwd D:\A_NEW_FILE\mp-weixin\mayidaijia-xiaochengxu_1.0.1\node_modules\node-sass gyp ERR! node -v v16.15.1 gyp ERR! node-gyp -v v7.1.2
最新发布
03-17
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值