nghttp2 / nghttp2

nghttp2 - HTTP/2 C Library and tools
https://nghttp2.org
Other
4.62k stars 871 forks source link

nghttp2 static library linkage fails on Windows #2234

Closed rfl890 closed 4 days ago

rfl890 commented 1 month ago

Currently, if you compile static libraries with CMake on Windows (via -DBUILD_SHARED_LIBS=off + -DBUILD_STATIC_LIBS=on), the generated nghttp2.h file does not define NGHTTP2_STATICLIB resulting in such errors:

ld.lld: error: undefined symbol: __declspec(dllimport) nghttp2_option_set_no_auto_window_update
>>> referenced by lib/CMakeFiles/libcurl_object.dir/cf-h2-proxy.c.obj:(cf_h2_proxy_connect)
>>> referenced by lib/CMakeFiles/libcurl_object.dir/http2.c.obj:(cf_h2_ctx_init)
...

As a fix, we could add this to CMakeLists.txt

if(BUILD_STATIC_LIBS)
set(NGHTTP2_STATICLIB true)
endif()

and add this line to cmakeconfig.h

#cmakedefine NGHTTP2_STATICLIB

and in nghttp2.h:

#include "config.h"
// ...
#ifdef NGHTTP2_STATICLIB
// works now
#  define NGHTTP2_EXTERN
#elif defined(WIN32) || (__has_declspec_attribute(dllexport) &&                \
                         __has_declspec_attribute(dllimport))
#  ifdef BUILDING_NGHTTP2
#    define NGHTTP2_EXTERN __declspec(dllexport)
#  else /* !BUILDING_NGHTTP2 */
#    define NGHTTP2_EXTERN __declspec(dllimport)
#  endif /* !BUILDING_NGHTTP2 */
#else    /* !defined(WIN32) */
#  ifdef BUILDING_NGHTTP2
#    define NGHTTP2_EXTERN __attribute__((visibility("default")))
#  else /* !BUILDING_NGHTTP2 */
#    define NGHTTP2_EXTERN
#  endif /* !BUILDING_NGHTTP2 */
#endif   /* !defined(WIN32) */
// ...
github-actions[bot] commented 1 week ago

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 7 days.