Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HEDLEY_IMPORT with C++ classes #35

Open
mattyclarkson opened this issue Mar 26, 2020 · 4 comments
Open

HEDLEY_IMPORT with C++ classes #35

mattyclarkson opened this issue Mar 26, 2020 · 4 comments

Comments

@mattyclarkson
Copy link

HEDLEY_IMPORT is specificed as extern which fails when used as so:

#ifdef PROJECT_COMPILATION
#define PROJECT_API HEDLEY_PUBLIC
#else
#define PROJECT_API HEDLEY_IMPORT
#endif

class PROJECT_API example {};

We would expect that example symbols are exported. However, when importing the result is class extern example {}; which is invalid.

A possible solution is:

#ifdef __cplusplus
#define HEDLEY_IMPORT
#else
#define HEDLEY_IMPORT extern
#endif
@mattyclarkson
Copy link
Author

@nemequ do you have any thoughts on this?

@SFGrenade
Copy link

alternatively, since c++ can still have functions:

#define HEDLEY_IMPORT extern
#ifdef __cplusplus
#define HEDLEY_IMPORT_CLASS
#endif

@nemequ
Copy link
Owner

nemequ commented Sep 28, 2024

I've been digging into this a bit, and I think @SFGrenade's suggestion is on the right track; it doesn't seem like we can reuse the same macro for both cases. Having a second macro for classes would make sense to me.

That said, I'm a bit confused about what this new macro would be defined to if not nothing? I know in MSVC you can put __declspec(dllexport)/__declspec(dllimport), is that the behavior your want to achieve, or is there something else?

@SFGrenade
Copy link

my homecooked solution is https://github.com/SFGrenade/ZmqPb-Cpp/blob/master/include/zmqPb/_export.hpp, so probably something like

#if defined(HEDLEY_PRIVATE)
#  undef HEDLEY_PRIVATE
#endif
#if defined(HEDLEY_PUBLIC)
#  undef HEDLEY_PUBLIC
#endif
#if defined(HEDLEY_IMPORT)
#  undef HEDLEY_IMPORT
#endif
#if defined(HEDLEY_IMPORT_CLASS)
#  undef HEDLEY_IMPORT_CLASS
#endif
#if defined(_WIN32) || defined(__CYGWIN__)
#  define HEDLEY_PRIVATE
#  define HEDLEY_PUBLIC         __declspec(dllexport)
#  define HEDLEY_IMPORT         __declspec(dllimport)
#  ifdef __cplusplus
#    define HEDLEY_IMPORT_CLASS  HEDLEY_IMPORT
#  endif
#else
#  if \
    HEDLEY_HAS_ATTRIBUTE(visibility) || \
    HEDLEY_GCC_VERSION_CHECK(3,3,0) || \
    HEDLEY_SUNPRO_VERSION_CHECK(5,11,0) || \
    HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
    HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
    HEDLEY_IBM_VERSION_CHECK(13,1,0) || \
    ( \
      defined(__TI_EABI__) && \
      ( \
        (HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
        HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) \
      ) \
    ) || \
    HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
#    define HEDLEY_PRIVATE __attribute__((__visibility__("hidden")))
#    define HEDLEY_PUBLIC  __attribute__((__visibility__("default")))
#  else
#    define HEDLEY_PRIVATE
#    define HEDLEY_PUBLIC
#  endif
#  define HEDLEY_IMPORT    extern
#  ifdef __cplusplus
#    define HEDLEY_IMPORT_CLASS
#  endif
#endif

at https://github.com/nemequ/hedley/blob/master/hedley.h#L1641-L1677

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants