From 9fbfa40bab8dc4dc9c300502c5973e634bb2956f Mon Sep 17 00:00:00 2001 From: "E. G. Patrick Bos" Date: Wed, 6 Oct 2021 09:46:26 +0200 Subject: [PATCH] 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. --- CMakeLists.txt | 6 ++++++ configure.ac | 5 +++++ include/zmq.h | 4 ++++ 3 files changed, 15 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 7f2d5d86..522fcb3c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1828,3 +1828,9 @@ if(MSVC AND BUILD_STATIC) add_dependencies(libzmq-static libzmq) 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() diff --git a/configure.ac b/configure.ac index 5454f2a4..227e37b4 100644 --- a/configure.ac +++ b/configure.ac @@ -1150,6 +1150,11 @@ AC_ARG_WITH([pkgconfigdir], [pkgconfigdir='${libdir}/pkgconfig']) 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([ \ Makefile \ src/libzmq.pc \ diff --git a/include/zmq.h b/include/zmq.h index 2612c664..6becfa3e 100644 --- a/include/zmq.h +++ b/include/zmq.h @@ -59,6 +59,9 @@ extern "C" { #include /* Handle DSO symbol visibility */ +#if defined ZMQ_NO_EXPORT +#define ZMQ_EXPORT +#else #if defined _WIN32 #if defined ZMQ_STATIC #define ZMQ_EXPORT @@ -76,6 +79,7 @@ extern "C" { #define ZMQ_EXPORT #endif #endif +#endif /* Define integer types needed for event interface */ #define ZMQ_DEFINED_STDINT 1