c++ - #ifdef SWIG : When does this conditional compilation is taken into consideration?

标签 c++ wrapper swig

我的 header sample.h 中有这样的内容:

#include <iostream>
#ifdef SWIG
int a = 0;
#endif

在我的sample.i中:

%module sample
%{
   #include "sample.h"

%}
#include "sample.h"

但是,我认为使用和不使用 #ifdef SWIG 之间没有任何区别...

您能否告诉我在哪里以及如何找到差异,以及差异是什么?

非常感谢!

最佳答案

您的 .i 文件有错误。最后一行应该是 %include 而不是 #include。通过此更改,构建包装器(我使用的是 Python)会产生:

sample_wrap.cxx
sample_wrap.cxx(3200) : error C2065: 'a' : undeclared identifier
sample_wrap.cxx(3211) : error C2065: 'a' : undeclared identifier

这是因为编译时 #include 期间未定义 SWIG,并且生成的代码中不存在 a。当 SWIG 处理 %include 时,SWIG 被定义并为不存在的 a 生成一个接口(interface)。

删除#if包装器会生成正确包装的全局变量:

>>> import sample
>>> sample.cvar.a
0

关于c++ - #ifdef SWIG : When does this conditional compilation is taken into consideration?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22119641/

相关文章:

c++ - 在两个 Qt 窗口之间发送事件

c++ - tinyXml 如何创建xml

c++ - 如何将列放入文件中

java - 创建 Java 注释包装器

css - 切换显示 : none; on several related items individually, 还是将它们包装在另一个 div 中并切换它更好?

html - global wrapper div min-height 100vh 打破固定在移动设备上的 child

c++ - .doc 到纯文本转换器

c# - 在 Windows 上用 C# 加载 linux 动态库 (.so)

java - 自定义 swig 生成的代码

c++ - 为静态库编译 SWIG Python 包装器?