Hardware Locality (hwloc): Compiling software on top of hwloc's C API

Hardware Locality (hwloc) 2.14.0rc1

Compiling software on top of hwloc's C API

A program using the hwloc C API (for instance with hwloc-hello.c presented in API Example) may be built with standard development tools. pkg-config provides easy ways to retrieve the required compiler and linker flags as described below, but it is not mandatory.

# Compiling on top of hwloc's C API with GNU Make

Here's an example of Makefile for building hwloc-hello.c with GNU Make:

    CFLAGS += $(shell pkg-config --cflags hwloc)
    LDLIBS += $(shell pkg-config --libs hwloc)

    hwloc-hello: hwloc-hello.c
            $(CC) hwloc-hello.c $(CFLAGS) -o hwloc-hello $(LDLIBS)

# Compiling on top of hwloc's C API with CMake

Here's an example de CMakeLists.txt which shows variables obtained from pkg-config and how to use them:

    cmake_minimum_required(VERSION 3.6)
    project(TEST_HWLOC C)

    include(FindPkgConfig)
    if(PKG_CONFIG_FOUND)
      pkg_search_module(HWLOC REQUIRED IMPORTED_TARGET hwloc)
    else(PKG_CONFIG_FOUND)
      message(FATAL_ERROR "FindHWLOC needs pkg-config program and PKG_CONFIG_PATH must contain the path to hwloc.pc file.")
    endif(PKG_CONFIG_FOUND)

    add_executable(hwloc-hello hwloc-hello.c)
    target_link_libraries(hwloc-hello PRIVATE PkgConfig::HWLOC)

The project may be built with:

    cmake -B build
    cmake --build build --verbose

The built binary is then available under build/hwloc-hello.