c++cmakeposix

How to know if a system supports "netinet/in.h" in CMake


I have noticed some embedded environments do not support the header netinet/in.h in C/C++ projects. Typically, they have some other header which defines a sockaddr_in struct in its place (e.g. in the case of compiling with ESP-IDF's build system, they specify lwip/sockets.h as the location of their sockaddr_in struct and it has many similar uses). It appears that this is part of the POSIX library so I assume that the deciding factor is whether or not the system being compiled for supports POSIX, or at least this part of it. I am not sure how to determine if I am compiling for a system that conforms to this part of POSIX.

Is it guaranteed by Unix? Should I check if I'm compiling for Unix and if so how best would that be done? A cursory search would tell me that some (or perhaps most) Linux distros, and thereby some Unix distros, aren't POSIX-conformant yet my Ubuntu has the netinet/in.h header so what gives?


Solution

  • There is a CMake module for checking if a file can be included from C code: https://cmake.org/cmake/help/latest/module/CheckIncludeFile.html

    include(CheckIncludeFile)
    
    check_include_file(netinet/in.h SUPPORTS_IN_H)
    
    if(SUPPORTS_IN_H)
      # ...
    else()
      # ...
    endif(SUPPORTS_IN_H)
    

    For C++, you can use CheckIncludeFileCXX