Search code examples
clinuxwindowserror-handlingheader-files

Make a custom error in C if header file not found


Is there a way to make C code show a custom error if a header file is not found? I feel this would particuraly useful for trying to compile windows code on linux. For example, instead of "Windows.h not found". the error would be "This code can only compile on windows".


Solution

  • You can check the OS and use the preprocessor directive #error if it doesn't match:

    #ifdef _WIN32
    #include <windows.h>
    #else
    #error "This code can only compile on windows"
    #endif
    

    Or if you prefer, in a Makefile:

    ifneq ($(OS),Windows_NT) 
    $(error This code can only compile on windows)
    endif