0
0
mirror of https://github.com/zeromq/libzmq.git synced 2024-12-27 07:31:03 +08:00

Problem: when linking to libzmq in my project, I want zmq symbols to remain invisible to users of my library. There is no way to control this, since visibility is set automatically through ZMQ_EXPORT based on OS and compiler.

Solution: add a preprocessor variable ZMQ_NO_EXPORT that, when set, bypasses the automatic ZMQ_EXPORT determination block and just sets ZMQ_EXPORT to empty.

By combining this solution at configuration time with manually passing -fvisibility=hidden to CXXFLAGS, I solved my visibility problem. Just passing -fvisibility=hidden is not enough, because __attribute__ ((visibility ("default"))) has higher priority.
This commit is contained in:
E. G. Patrick Bos 2021-10-06 09:46:26 +02:00 committed by Luca Boccassi
parent 5d8d857540
commit 9fbfa40bab
3 changed files with 15 additions and 0 deletions

View File

@ -1828,3 +1828,9 @@ if(MSVC
AND BUILD_STATIC) AND BUILD_STATIC)
add_dependencies(libzmq-static libzmq) add_dependencies(libzmq-static libzmq)
endif() endif()
option(ENABLE_NO_EXPORT "Build with empty ZMQ_EXPORT macro, bypassing platform-based automated detection" OFF)
if(ENABLE_NO_EXPORT)
message(STATUS "Building with empty ZMQ_EXPORT macro")
add_definitions(-DZMQ_NO_EXPORT)
endif()

View File

@ -1150,6 +1150,11 @@ AC_ARG_WITH([pkgconfigdir],
[pkgconfigdir='${libdir}/pkgconfig']) [pkgconfigdir='${libdir}/pkgconfig'])
AC_SUBST([pkgconfigdir]) AC_SUBST([pkgconfigdir])
AC_ARG_ENABLE([no-export],
[AS_HELP_STRING([--enable-no-export],
[Build libzmq with empty ZMQ_EXPORT macro, bypassing platform-based automated detection [default=no]])],
[CPPFLAGS="-DZMQ_NO_EXPORT $CPPFLAGS"])
AC_CONFIG_FILES([ \ AC_CONFIG_FILES([ \
Makefile \ Makefile \
src/libzmq.pc \ src/libzmq.pc \

View File

@ -59,6 +59,9 @@ extern "C" {
#include <stdio.h> #include <stdio.h>
/* Handle DSO symbol visibility */ /* Handle DSO symbol visibility */
#if defined ZMQ_NO_EXPORT
#define ZMQ_EXPORT
#else
#if defined _WIN32 #if defined _WIN32
#if defined ZMQ_STATIC #if defined ZMQ_STATIC
#define ZMQ_EXPORT #define ZMQ_EXPORT
@ -76,6 +79,7 @@ extern "C" {
#define ZMQ_EXPORT #define ZMQ_EXPORT
#endif #endif
#endif #endif
#endif
/* Define integer types needed for event interface */ /* Define integer types needed for event interface */
#define ZMQ_DEFINED_STDINT 1 #define ZMQ_DEFINED_STDINT 1