Learning CMake Cookbook Chapter02 Part02
检测处理器指令集
这里使用的大致方法一致,我们在源码中要输出一些指示处理器指令集的信息,但这些信息的define既没有在源码中给出也没有在CMakeLists中直接指明定义,而是通过间接生成头文件的形式被包含。
#include "config.h"
#include <cstdlib>
#include <iostream>
int main() {
std::cout << "Number of logical cores: " << NUMBER_OF_LOGICAL_CORES << std::endl;
std::cout << "Number of physical cores: " << NUMBER_OF_PHYSICAL_CORES << std::endl;
std::cout << "Total virtual memory in megabytes: " << TOTAL_VIRTUAL_MEMORY
<< std::endl;
std::cout << "Available virtual memory in megabytes: " << AVAILABLE_VIRTUAL_MEMORY
<< std::endl;
std::cout << "Total physical memory in megabytes: " << TOTAL_PHYSICAL_MEMORY
<< std::endl;
std::cout << "Available physical memory in megabytes: "
<< AVAILABLE_PHYSICAL_MEMORY << std::endl;
std::cout << "Processor is 64Bit: " << IS_64BIT << std::endl;
std::cout << "Processor has floating point unit: " << HAS_FPU << std::endl;
std::cout << "Processor supports MMX instructions: " << HAS_MMX << std::endl;
std::cout << "Processor supports Ext. MMX instructions: " << HAS_MMX_PLUS
<< std::endl;
std::cout << "Processor supports SSE instructions: " << HAS_SSE << std::endl;
std::cout << "Processor supports SSE2 instructions: " << HAS_SSE2 << std::endl;
std::cout << "Processor supports SSE FP instructions: " << HAS_SSE_FP << std::endl;
std::cout << "Processor supports SSE MMX instructions: " << HAS_SSE_MMX
<< std::endl;
std::cout << "Processor supports 3DNow instructions: " << HAS_AMD_3DNOW
<< std::endl;
std::cout << "Processor supports 3DNow+ instructions: " << HAS_AMD_3DNOW_PLUS
<< std::endl;
std::cout << "IA64 processor emulating x86 : " << HAS_IA64 << std::endl;
std::cout << "OS name: " << OS_NAME << std::endl;
std::cout << "OS sub-type: " << OS_RELEASE << std::endl;
std::cout << "OS build ID: " << OS_VERSION << std::endl;
std::cout << "OS platform: " << OS_PLATFORM << std::endl;
return EXIT_SUCCESS;
}
注意以下的cpp源文件目录下最初没有config.h这个文件。而在目录中存在一个config.h.in的文件,如下:
#pragma once
#define NUMBER_OF_LOGICAL_CORES @_NUMBER_OF_LOGICAL_CORES@
#define NUMBER_OF_PHYSICAL_CORES @_NUMBER_OF_PHYSICAL_CORES@
#define TOTAL_VIRTUAL_MEMORY @_TOTAL_VIRTUAL_MEMORY@
#define AVAILABLE_VIRTUAL_MEMORY @_AVAILABLE_VIRTUAL_MEMORY@
#define TOTAL_PHYSICAL_MEMORY @_TOTAL_PHYSICAL_MEMORY@
#define AVAILABLE_PHYSICAL_MEMORY @_AVAILABLE_PHYSICAL_MEMORY@
#define IS_64BIT @_IS_64BIT@
#define HAS_FPU @_HAS_FPU@
#define HAS_MMX @_HAS_MMX@
#define HAS_MMX_PLUS @_HAS_MMX_PLUS@
#define HAS_SSE @_HAS_SSE@
#define HAS_SSE2 @_HAS_SSE2@
#define HAS_SSE_FP @_HAS_SSE_FP@
#define HAS_SSE_MMX @_HAS_SSE_MMX@
#define HAS_AMD_3DNOW @_HAS_AMD_3DNOW@
#define HAS_AMD_3DNOW_PLUS @_HAS_AMD_3DNOW_PLUS@
#define HAS_IA64 @_HAS_IA64@
#define OS_NAME "@_OS_NAME@"
#define OS_RELEASE "@_OS_RELEASE@"
#define OS_VERSION "@_OS_VERSION@"
#define OS_PLATFORM "@_OS_PLATFORM@"
该文件将结合CMakeLists文件,在cmake阶段动态生成我们源文件中所需要引用的头文件config.h,CMakeLists文件如下:
cmake_minimum_required(VERSION 3.10 FATAL_ERROR)
project(recipe-05 LANGUAGES CXX)
add_executable(processor-info "")
target_sources(processor-info
PRIVATE
processor-info.cpp
)
target_include_directories(processor-info
PRIVATE
${PROJECT_BINARY_DIR}
)
foreach(key
IN ITEMS
NUMBER_OF_LOGICAL_CORES
NUMBER_OF_PHYSICAL_CORES
TOTAL_VIRTUAL_MEMORY
AVAILABLE_VIRTUAL_MEMORY
TOTAL_PHYSICAL_MEMORY
AVAILABLE_PHYSICAL_MEMORY
IS_64BIT
HAS_FPU
HAS_MMX
HAS_MMX_PLUS
HAS_SSE
HAS_SSE2
HAS_SSE_FP
HAS_SSE_MMX
HAS_AMD_3DNOW
HAS_AMD_3DNOW_PLUS
HAS_IA64
OS_NAME
OS_RELEASE
OS_VERSION
OS_PLATFORM
)
cmake_host_system_information(RESULT _${key} QUERY ${key})
endforeach()
configure_file(config.h.in config.h @ONLY)
Eigen库相关
这个部分不做过多介绍。
安装好Eigen库,并完成配置可以编译通过即可。
|