奇怪的C ++头文件功能声明

时间:2019-05-17 15:30:23

标签: python function c++11 header swig

当我尝试swig -c++ -python my_interface.i时,我正在读取一些试图编译为python绑定的源代码。

我知道源代码是用C ++ 11编写的。

// module.hpp
#pragma once

namespace module
{
    MODULE_API void getVersion (unsigned &params);
}
// my_interface.i
%module my_api
%{
#include "module.hpp"
%}

%include "module.hpp"

我无权访问源文件,只有标题和我们可以称为modulelib.so的共享库。

有人知道函数返回类型之前的MODULE_API是什么意思吗?

当我尝试立即编译时,出现错误module.hpp:29: Error: Syntax error in input(1) 当我删除MODULE_API时,输出将移至下一个函数声明,因为它们都具有这种声明风格,因此我将它们全部删除,但是我觉得那会破坏某些东西。

现在,当我运行swig -v -wall -c++ -python my_interface.i时,我收到一个错误:

Language subdirectory: python
Search paths:
   ./
   ./swig_lib/python/
   /usr/share/swig3.0/python/
   ./swig_lib/
   /usr/share/swig3.0/
Preprocessing...
Starting language-specific parse...
module.hpp:6: Error: Syntax error in input(1).

1 个答案:

答案 0 :(得分:2)

这是处理符号可见性的常见模式。例如,Windows要求您在编译库时使用__declspec(dllexport)定义函数(因此链接器知道使相应的符号在外部可见),在{em时使用__declspec(dllimport)定义函数。 >使用/链接一个库(因此链接器知道这些符号将从DLL中导入)。

为避免必须具有两个不同的头(一个用于编译,一个用于使用库),使用条件定义:

#ifdef WIN32

  #ifdef MODULE_EXPORTS
    #define MODULE_API __declspec(dllexport)
  #else
    #define MODULE_API __declspec(dllimport)
  #endif

#else
  #define MODULE_API
#endif

这仅仅是一个例子。 根本没有MODULE_API。有些项目使用GCC的__attribute__((visibility("default")))__attribute__((visibility("hidden"))),但是Windows是该方案的起源,因为Windows的 default hide 符号。