Skip to content

Building nghttp2 library on Windows: clumsy management of ssize_t. #616

@mtm31415926

Description

@mtm31415926

Windows C environment does not offer ssize_t type out of the box. Therefore the nghhtp2 library offers to define it in the compiler flags like:

CFLAGS = ... -Dssize_t=long ...

This is a questionable way to go for a number of reasons:

  • Definition of the data type in the compilation command complicates use of the library in software development: one should not forget to define ssize_t in the same way, which is pretty inconvenient and not flexible;
  • Although ssize_t is not defined out of the box on Windows, it is too well known type name to define it in a specific library: a chance of conflict with other code is high;
  • Finally, definition of ssize_t as long does not look correct for 64-bit builds; sizeof(long) is 4 for 64-bit Windows while 8-byte data would be more appropriate. Windows introduces SSIZE_T type though.

Looks like the best solution would be introduction of a kind of nghhtp2_ssize_t type, which would be properly typedef'ed for each platform flavor. As a quick workaround, the following method works:

  1. The Windows complication flags (locally for the library build only) are changed to
  2. CFLAGS = ... -Dssize_t=SSIZE_T ...
  3. The following code is added to file 'includes/nghttp2/nghttp2.h' (line ~32; after ensuring WIN32 is defined):
    #if defined(WIN32)
    #  pragma once
    #  include <basetsd.h>
    typedef SSIZE_T NGHTTP2_SSIZE_T;
    #  if !defined(BUILDING_NGHTTP2)
    #    define ssize_t NGHTTP2_SSIZE_T
    #  endif
    #endif
  4. The following code is added to file 'includes/nghttp2/nghttp2.h' (line ~4965; before the final #endif):
    #if defined(WIN32)
    #  if !defined(BUILDING_NGHTTP2)
    #    undef ssize_t
    #  endif
    #endif

This does not require massive code changes, defines ssize_t properly and consistently for x86/x64 Windows platforms, and encapsulates ssize_t definition inside the library code offering NGHTTP2_SSIZE_T to one who uses this library in their own code.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions