当前位置: 首页 > news >正文

C++系统相关操作4 - 获取CPU(指令集)架构类型

  • 1. 关键词
  • 2. sysutil.h
  • 3. sysutil.cpp
  • 4. 测试代码
  • 5. 运行结果
  • 6. 源码地址

1. 关键词

关键词:

C++ 系统调用 CPU架构 指令集 跨平台

实现原理:

  • Unix-like 系统: 可以通过 uname -m 命令获取 CPU 架构类型。
  • Windows 系统: 可以通过环境变量 PROCESSOR_ARCHITECTURE 获取 CPU 架构类型。

2. sysutil.h

#pragma once#include <cstdint>
#include <string>namespace cutl
{/*** @brief Get the architecture of the cpu processor(or operating system).** @return std::string the architecture.*/std::string architecture();
} // namespace cutl

3. sysutil.cpp


#include <map>
#include <iostream>
#include <strutil.h>
#include <cstdlib>
#include "sysutil.h"
#include "inner/logger.h"
#include "inner/system_util.h"
#include "inner/filesystem.h"namespace cutl
{std::string architecture(){
#if defined(_WIN32) || defined(__WIN32__)return cutl::getenv("PROCESSOR_ARCHITECTURE", "");
#elsestatic std::string arch;if (arch.empty()){std::string cmd = "uname -m";callcmd(cmd, arch);CUTL_DEBUG("cmd: " + cmd + ", result: " + arch);}return arch;
#endif}
} // namespace cutl

4. 测试代码

#include "common.hpp"
#include "sysutil.h"void TestArchitecture()
{PrintSubTitle("TestArchitecture");std::cout << "OS architecture: " << cutl::architecture() << std::endl;
}

5. 运行结果

------------------------------------------TestArchitecture------------------------------------------
OS architecture: x86_64

6. 源码地址

更多详细代码,请查看本人写的C++ 通用工具库: common_util, 本项目已开源,代码简洁,且有详细的文档和Demo。

本文由博客一文多发平台 OpenWrite 发布!

http://www.lryc.cn/news/385182.html

相关文章:

  • whisper 实现语音转文字
  • 使用VLLM部署llama3量化版
  • 计算机缺失OpenCL.dll怎么办,OpenCL.dll丢失的多种解决方法
  • git 本地代码管理
  • Docker(九)-Docker运行redis6.0.8容器实例
  • 似然 与 概率
  • Tableau数据可视化与仪表盘搭建
  • web前端——HTML
  • C++的模板(九):模板的实例化问题
  • Clickhouse Projection
  • 放烟花短视频素材去哪里找?去哪里下载?烟花素材网分享
  • 爬虫笔记14——爬取网页数据写入MongoDB数据库,以爱奇艺为例
  • Jenkins教程-10-发送飞书测试报告通知
  • Swift开发——简单App设计
  • Python操作mysql
  • 监控易产品升级动态:V7.6.6.15版本全面升级
  • Vue3 + Element-plus + TS —— 动态表格自由编辑
  • 虚拟机配置桥接模式
  • 星戈瑞DSPE-SS-PEG-CY7近红外花菁染料
  • LeetCode:503. 下一个更大元素 II(Java 单调栈)
  • 代码重构:解读重构概念及重构实战
  • java.util.Optional类介绍
  • PhotoShop自动生成号码牌文件
  • 02逻辑代数与硬件描述语言基础
  • OpenGL3.3_C++_Windows(21)
  • clickhouse学习
  • MySQL高级-索引-使用规则-前缀索引
  • 外星生命在地球的潜在存在:科学、哲学与社会的交织
  • 使用FRP 0.58版本进行内网穿透的详细教程
  • 0000电子技术基础概述