Search code examples
c++visual-studio-2010getcpu

Microsoft Visual Studio 2010 doesn't work


I've made a console application in VS 2010 in order to get the number of cores: This is the code:

#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;
}

And this is what it shows:

**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\myUser\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\myUser\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\myUser\documents\visual studio 2010\projects\project_scs\project_scs\project_scs.cpp(9): error C3861: 'popen': identifier not found
1>c:\users\myUser\documents\visual studio 2010\projects\project_scs\project_scs\project_scs.cpp(10): error C3861: 'fread': identifier not found
1>c:\users\myUser\documents\visual studio 2010\projects\project_scs\project_scs\project_scs.cpp(11): error C3861: 'fclose': identifier not found
1>c:\users\myUser\documents\visual studio 2010\projects\project_scs\project_scs\project_scs.cpp(12): error C2065: 'cout' : undeclared identifier
1>c:\users\myUser\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 ==========**

What is the problem and what should I do? Thanks.


Solution

  • Many issues really, but your main one is that you are using precompiled headers incorrectly. Check MSDN for more info on that.

    Also popen() is not part of the Win32 API. It is in Posix but you are not using that. You might want to checkout _popen though: http://msdn.microsoft.com/en-us/library/96ayss4b%28v=vs.100%29.aspx