Skip to content

Commit

Permalink
Problem: when linking to libzmq in my project, I want zmq symbols to …
Browse files Browse the repository at this point in the history
…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.
  • Loading branch information
egpbos committed Oct 7, 2021
1 parent 5d8d857 commit f558eb5
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 0 deletions.
6 changes: 6 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
5 changes: 5 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
4 changes: 4 additions & 0 deletions include/zmq.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ extern "C" {
#include <stdio.h>

/* Handle DSO symbol visibility */
#if defined ZMQ_NO_EXPORT
#define ZMQ_EXPORT
#else
#if defined _WIN32
#if defined ZMQ_STATIC
#define ZMQ_EXPORT
Expand All @@ -76,6 +79,7 @@ extern "C" {
#define ZMQ_EXPORT
#endif
#endif
#endif

/* Define integer types needed for event interface */
#define ZMQ_DEFINED_STDINT 1
Expand Down

0 comments on commit f558eb5

Please sign in to comment.