-1

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\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 ==========**

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

  • 1
    `#include "stdafx.h"` should appear in the very 1st line before any other `#include` directives. – πάντα ῥεῖ Jun 05 '14 at 17:09
  • thanks. Now there is just 1 error left: error C3861: 'popen': identifier not found –  Jun 05 '14 at 17:13
  • Dupe? http://stackoverflow.com/questions/2619198/how-to-get-number-of-cores-in-win32 – chris Jun 05 '14 at 17:15
  • 2
    `/bin/cat /proc/cpuinfo` Did you type in that command in a Windows command prompt to see if it actually works? – PaulMcKenzie Jun 05 '14 at 17:17
  • 1
    well, I know now that it is for Linux. I searched on Internet and I've found that for getting CPUINFO is used "C:\>wmic cpu get caption" which doesn't work either. Do you know which is the actual path that must be used? Thanks. –  Jun 05 '14 at 17:33

2 Answers2

1

Start by reading your errors: 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.

Then also consider that the Linux approach (reading /proc) is unlikely to work on a system that can run an executable created by Visual Studio.

Michael Urman
  • 15,737
  • 2
  • 28
  • 44
1

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

Dennis
  • 3,683
  • 1
  • 21
  • 43
  • Thankyou for the advice. What can I use to replace popen() ? –  Jun 05 '14 at 17:15
  • @user3677169 - See the updated answer. Also upvote my answer if you want to thank me :) – Dennis Jun 05 '14 at 17:16
  • 1
    I see that there is used _popen instead of popen. Which still doesn't work for me.. –  Jun 05 '14 at 17:37