Microsoft Visual Studio 2010不起作用

时间:2014-06-05 17:07:44

标签: c++ visual-studio-2010 get cpu

我在VS 2010中创建了一个控制台应用程序,以获取核心数量: 这是代码:

#include <iostream>
#include "stdio.h"
#include "stdafx.h"
using namespace std;

int main(){
        FILE * fp;
        char res[128];
        fp = popen("/bin/cat /proc/cpuinfo |grep -c '^processor'","r");
        fread(res, 1, sizeof(res)-1, fp);
        fclose(fp);
        cout << "number of core: " << res[0] << endl;
        return 0;
}

这就是它所显示的:

**1>------ Build started: Project: project_scs, Configuration: Debug Win32 ------
1>Build started 6/5/2014 8:01:03 PM.
1>InitializeBuildStatus:
1>  Touching "Debug\project_scs.unsuccessfulbuild".
1>ClCompile:
1>  All outputs are up-to-date.
1>  project_scs.cpp
1>c:\users\rares992\documents\visual studio 2010\projects\project_scs\project_scs\project_scs.cpp(1): warning C4627: '#include <iostream>': skipped when looking for precompiled header use
1>          Add directive to 'StdAfx.h' or rebuild precompiled header
1>c:\users\rares992\documents\visual studio 2010\projects\project_scs\project_scs\project_scs.cpp(2): warning C4627: '#include "stdio.h"': skipped when looking for precompiled header use
1>          Add directive to 'StdAfx.h' or rebuild precompiled header
1>c:\users\rares992\documents\visual studio 2010\projects\project_scs\project_scs\project_scs.cpp(9): error C3861: 'popen': identifier not found
1>c:\users\rares992\documents\visual studio 2010\projects\project_scs\project_scs\project_scs.cpp(10): error C3861: 'fread': identifier not found
1>c:\users\rares992\documents\visual studio 2010\projects\project_scs\project_scs\project_scs.cpp(11): error C3861: 'fclose': identifier not found
1>c:\users\rares992\documents\visual studio 2010\projects\project_scs\project_scs\project_scs.cpp(12): error C2065: 'cout' : undeclared identifier
1>c:\users\rares992\documents\visual studio 2010\projects\project_scs\project_scs\project_scs.cpp(12): error C2065: 'endl' : undeclared identifier
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:00.08
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========**

问题是什么,我该怎么办?感谢。

2 个答案:

答案 0 :(得分:1)

首先阅读您的错误:1>c:\users\rares992\documents\visual studio 2010\projects\project_scs\project_scs\project_scs.cpp(2): warning C4627: '#include "stdio.h"': skipped when looking for precompiled header use

然后还要考虑Linux方法(阅读/proc)不太可能在可以运行Visual Studio创建的可执行文件的系统上运行。

答案 1 :(得分:1)

确实存在很多问题,但主要问题是您正在使用预编译的标头错误。检查MSDN以获取更多信息。

popen()也不是Win32 API的一部分。它在Posix中,但你没有使用它。您可能想要结帐_popenhttp://msdn.microsoft.com/en-us/library/96ayss4b%28v=vs.100%29.aspx