Linux “中的跨平台动态库”;";(微软和gcc)

Linux “中的跨平台动态库”;";(微软和gcc),linux,gcc,visual-c++,dll,Linux,Gcc,Visual C++,Dll,最终,我希望得到一组编译到Windows或Linux动态库的源文件,具体取决于编译它的平台 问题似乎是Windows要求对头文件声明和源文件定义都进行注释 Linux动态链接库似乎只需要源文件定义中的注释 #if (_MSC_VER >= 1900) // (Visual Studio 2015 version 14.0) #define EXPORTED __declspec(dllexport) #else #define EXPORTED __attribute__((vi

最终,我希望得到一组编译到Windows或Linux动态库的源文件,具体取决于编译它的平台

问题似乎是Windows要求对头文件声明和源文件定义都进行注释

Linux动态链接库似乎只需要源文件定义中的注释

#if (_MSC_VER >= 1900)  // (Visual Studio 2015 version 14.0)
  #define EXPORTED __declspec(dllexport)
#else
  #define EXPORTED __attribute__((visibility("default")))
#endif
我可以定义一个预处理器字符串来处理源代码定义的差异

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

Windows和Linux都忽略空的#define语句。

您不能在Windows中使用注释。可以使用DEF文件来声明导出函数

// 
// 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 
对于linux,您需要使用注释,例如:

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

您不能在Windows中使用批注。可以使用DEF文件来声明导出函数

对于linux,您需要使用注释,例如:

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

这就解决了Windows需要函数的问题 在DLL头文件和DLL源文件中添加注释 而Linux只需要输入LIB源函数 注释。Windows和Linux都忽略空#定义 声明

// 
// 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 

这就解决了Windows需要函数的问题 在DLL头文件和DLL源文件中添加注释 而Linux只需要输入LIB源函数 注释。Windows和Linux都忽略空#定义 声明

// 
// 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 

\uuuu declspec(dllexport)/\uu declspec(dllimport)同时位于头文件和源文件中。
不,只有头文件就足够了
将导出视为不可见
为什么不#定义导出@dxiv同一源代码必须编译为Windows DLL和Linux动态库。源当前在源中定义了“\uuuuuu属性”\uuuuuu((可见性(“默认”)))。抱歉,不确定这是指我的评论的哪一部分。我的第一点是关于它的VC++方面,第二点是关于为什么不是一个空宏,而不是一个定义为另一个空宏的宏。我想这个问题可能需要更多的上下文和细节。@dxiv我正在尝试将现有的Linux动态库转换为Windows系统上的Windows DLL。现在还不清楚到底是什么不起作用。你能发一封信吗?顺便说一句,有无数的可移植库可以正常工作。
\uuu declspec(dllexport)/\uu declspec(dllimport)在头文件和源文件中。
不,只有头文件就足够了
将导出视为不可见
为什么不#定义导出@dxiv同一源代码必须编译为Windows DLL和Linux动态库。源当前在源中定义了“\uuuuuu属性”\uuuuuu((可见性(“默认”)))。抱歉,不确定这是指我的评论的哪一部分。我的第一点是关于它的VC++方面,第二点是关于为什么不是一个空宏,而不是一个定义为另一个空宏的宏。我想这个问题可能需要更多的上下文和细节。@dxiv我正在尝试将现有的Linux动态库转换为Windows系统上的Windows DLL。现在还不清楚到底是什么不起作用。你能发一封信吗?顺便说一句,有无数的便携式图书馆刚刚开始工作。