ProcessorCount — CMake 4.1.4 Documentation

# ProcessorCount

This module provides a command to determine the number of processors/cores.

Load this module in CMake with:

    include(ProcessorCount)

## Commands

This module provides the following command:

ProcessorCount

  Determines the number of logical CPU cores available on the machine:

      ProcessorCount(<variable>)

  This command sets a variable named `<variable>` to the number of logical CPU cores available on the machine, if the information can be determined. If successful, the variable is guaranteed to be set to a positive integer (>=1). If the processor count cannot be determined, it is set to 0.

  Currently, this functionality is implemented for AIX, Cygwin, FreeBSD, Haiku, HPUX, Linux, macOS, QNX, Sun and Windows.

  This command provides an approximation of the number of compute cores available on the current machine, making it useful for parallel building and testing. It is meant to help utilize as much of the machine as seems reasonable, though users should consider other workloads running on the machine before using its full capacity for parallel tasks.

  Changed in version 3.15: On Linux, returns the container CPU count instead of the host CPU count.

Note

This module relies on system-dependent commands to determine the number of processors, which may not always provide accurate information in certain environments. A more generally accurate logical CPU count can be also obtained with the `cmake_host_system_information()`:

    cmake_host_system_information(RESULT n QUERY NUMBER_OF_LOGICAL_CORES)

## Examples

In the following example this module is used in a `ctest -S` dashboard script to determine number of cores to use for a parallel CTest Build Step:

    include(ProcessorCount)
    ProcessorCount(n)
    if(NOT n EQUAL 0)
      set(CTEST_BUILD_FLAGS -j${n})
      set(ctest_test_args ${ctest_test_args} PARALLEL_LEVEL ${n})
    endif()