Search code examples
cmakefileincludeheader-filesinclude-path

How do I robustly include external header files that may be in different (unknown) directories?


I am trying to write a Makefile that will compile a C project on multiple platforms, for example: CentOS, OS X, OpenSUSE. I am running into the problem, however, that header files for external libraries are not always in the same place. I think this is due to packaging, over which I have no control.

For example: While including the BLAS file lapacke.h the include statement required to on my CentOS installation is #include<lapacke.h>. On OpenSUSE the path is #include<openblas/lapacke.h>

I can use #include<lapacke.h>, then compile with -I/usr/include/openblas, and this works on both CentOS and OpenSUSE, but I do not expect this include path to work in general because other systems may put openblas in a different location. How do I configure this project to compile on most platforms without requiring users to tinker with the Makefile manually?


Solution

  • How do I configure this project to compile on most platforms without requiring users to tinker with the Makefile manually?

    As it applies to libraries in general, this is a classic and perennial problem. It is part of the raison d'être of build configuration systems such as the Autotools and CMake, and of the pkg-config utility. A few packages provide their own utilities for reporting build details.

    None of those are foolproof. If you can't or don't want to rely on any of them then your other options are basically

    • make the user provide the needed paths (either by editing a file or by a hand-rolled script provided with the software), or
    • provide a bunch of pre-built configurations for known system types, and either auto-detect or ask the user to specify the system type.

    I have seen all of the above, individually and in various combinations.