在爲Windows編寫新代碼時,我偶然發現了Windows API的_cpuinfo()
。由於我主要處理Linux環境(GCC),我想要訪問CPUInfo。如何在Linux中調用「cpuid」?
我曾嘗試以下:
#include <iostream>
int main()
{
int a, b;
for (a = 0; a < 5; a++)
{
__asm ("mov %1, %%eax; " // a into eax
"cpuid;"
"mov %%eax, %0;" // eax into b
:"=r"(b) // output
:"r"(a) // input
:"%eax","%ebx","%ecx","%edx" // clobbered register
);
std::cout << "The code " << a << " gives " << b << std::endl;
}
return 0;
}
此使用匯編,但我不想重新發明輪子。有沒有其他的方式來實現沒有彙編的CPUInfo?
編譯器錯誤:
[email protected]:~/Desktop/prog$ g++ -Wall CPUInfo.cpp
CPUInfo.cpp: In function ‘int main()’:
CPUInfo.cpp:10:22: error: expected ‘)’ before ‘;’ token
CPUInfo.cpp:10:23: error: expected primary-expression before ‘)’ token
CPUInfo.cpp:10:23: error: expected ‘;’ before ‘)’ token
CPUInfo.cpp:8:8: warning: unused variable ‘b’ [-Wunused-variable]
CPUInfo.cpp:12:8: error: expected ‘}’ at end of input
所以,這是g ++我猜。和x86?你應該說。無論如何,問題是什麼?你告訴我們你在做什麼,並顯示了代碼。但毫無疑問。 –
@JerryCoffin我正在運行Linux(因爲在這裏將確定執行代碼的計算機的CPU的Linux部分)。我已經在Win API中使用了_cpuinfo()函數的Windows版本/部分。這是我的觀點。 – TheBlueCat
@DavidHeffernan看到我的更新後,我忽略了添加編譯器錯誤。 – TheBlueCat