Determine the number of cores at compile time in C / C ++

Is there a way to determine how many physical cores the target machine has at compile time in C / C ++ on Linux under GCC?

I am aware of other methods such as td::thread::hardware_concurrency()

in C ++ 11 or sysconf(_SC_NPROCESSORS_ONLN)

, but I am curious to know if there is a way to get this information at compile time.

+3


source to share


1 answer


You can get this information from the terminal during build processes and pass it to the program as a definition before the processor.

Example:

g++ main.cpp -D PROC_COUNT=$(grep -c ^processor /proc/cpuinfo)



where main.cpp

-

#include <iostream>
int main() {
    std::cout << PROC_COUNT << std::endl;
    return 0;
}

      

+6


source







All Articles