1

Ultimately I want to end up with a single set of source files that compiles to a Windows or Linux dynamic library depending on which platform compiled it.

The problem seems to be that Windows requires that annotations be made to both the header file declarations and the source file definitions. DLL Tutorial For Beginners

Linux dynamic link libraries seem to require annotations only in the source file definitions.

I can #define a preprocessor string to handle the difference of the source code definitions.

#if (_MSC_VER >= 1900)  // (Visual Studio 2015 version 14.0)
  #define EXPORTED __declspec(dllexport)
#else
  #define EXPORTED __attribute__((visibility("default")))
#endif

Both Windows and Linux ignore empty #define statements.

polcott
  • 143
  • 1
  • 13
  • `__declspec(dllexport) / __declspec(dllimport) to be in both the header and the source file.` No, just the header will suffice. `treat EXPORTED as invisible` Why not `#define EXPORTED`. – dxiv Jul 01 '20 at 05:15
  • @dxiv The same source has to compile as a Windows DLL and a Linux dynamic library. The source currently has "__attribute__((visibility("default")))" defined in the source. – polcott Jul 01 '20 at 05:25
  • Sorry, not sure which part of my comment this is referring to. My first point was about the VC++ side of it, and the second one about why not an empty macro, as opposed to a macro defined to another EMPTY_DEFINE. I think the question could use some more context and details. – dxiv Jul 01 '20 at 05:46
  • @dxiv I am trying to convert an existing Linux dynamic library to to compile to a Windows DLL when on a Windows system. – polcott Jul 01 '20 at 06:10
  • It isn't quite clear what exactly isn't working. Can you post a [mcve]? By the way there are zillions of portable libraries out there that just work. – n. m. could be an AI Jul 01 '20 at 16:37
  • @n.'pronouns'm. I am adapting the source code for a dynamic library that was written exclusively for Linux so that it can also produce a DLL for Windows depending on the platform it was compiled under. – polcott Jul 01 '20 at 16:55
  • Well it doesn't really matter if it's an adaptation or something written from scratch. – n. m. could be an AI Jul 01 '20 at 17:37
  • @n.'pronouns'm. The adaptation does not compile. I am working on providing a minimal reproducible example. – polcott Jul 01 '20 at 17:56

2 Answers2

0

You may not use annotations in Windows. You can use DEF file for declare export functions .def files C/C++ DLLs

For linux you need to use annotations, e.g.:

int
#ifdef __GNUC__
    __attribute__((visibility("default")))
#endif
    myfunction(int param) {
  return 0;
}
alexb
  • 169
  • 6
0

This solves the problem that Windows requires functions in the DLL header and the DLL source file be annotated and Linux requires only the LIB source functions to be annotated. Both Windows and Linux ignore empty #define statements.

// 
// Dynamic Link Library for Linux and Windows
// 
// If not >= Visual Studio 2015 (version 14.0) then 
// Linux is assumed
//
// This file is inlcluded in all of the LIB/DLL Source 
// and the LIB/DLL caller. 
//
#if (_MSC_VER >= 1900)  // (Visual Studio 2015 version 14.0)

// Windows Function Definition   (LIB/DLL Source)
  #define EXPORTED_DEF      __declspec(dllexport)  

// DLL_EXPORT is defined at top of LIB/DLL Source of exported functions 
  #if defined DLL_EXPORT                      

// Windows Function Declaration (DLL Header)
    #define EXPORTED_DEC   __declspec(dllexport)  // DLL Export

  #else
// Windows Function Declaration (Caller Header)
    #define EXPORTED_DEC   __declspec(dllimport)  // DLL Import

  #endif
#else

// Linux Function definition (LIB/DLL Source) 
  #define EXPORTED_DEF   __attribute__((visibility("default")))  
  #define EXPORTED_DEC  // Linux cannot see this
#endif 
polcott
  • 143
  • 1
  • 13