[英]#ifdef SWIG : When does this conditional compilation is taken into consideration?
I have in my header sample.h
this : 我的头文件sample.h
以下内容:
#include <iostream>
#ifdef SWIG
int a = 0;
#endif
And in my sample.i
this : 在我的sample.i
这是:
%module sample
%{
#include "sample.h"
%}
#include "sample.h"
But, I don't see any difference between with and without the #ifdef SWIG... 但是,无论有没有#ifdef SWIG,我都看不到任何区别...
Can you please let me know where and how I can find the difference, and what would it be?? 您能否让我知道在哪里以及如何找到差异,这会是什么?
Thank you a lot! 非常感谢!
Your .i file has an error. 您的.i文件有错误。 The last line should be %include
not #include
. 最后一行应为%include
而不是#include
。 With this change, building your wrapper (I'm using Python) produces: 进行此更改后,构建包装器(我正在使用Python)将产生:
sample_wrap.cxx
sample_wrap.cxx(3200) : error C2065: 'a' : undeclared identifier
sample_wrap.cxx(3211) : error C2065: 'a' : undeclared identifier
This is because when compiled SWIG is not defined during #include
and a
does not exist in the resulting code. 这是因为在#include
期间未定义编译的SWIG,并且在结果代码中不存在a
。 When SWIG processes %include
SWIG is defined and generates an interface for a
which does not exist. 当SWIG处理%include
SWIG被定义并生成一个接口,用于a
不存在。
Removing the #if
wrapper produces a correctly wrapped global variable: 删除#if
包装器会产生正确包装的全局变量:
>>> import sample
>>> sample.cvar.a
0
There is a solution to this: if you compile the code above in CMake, you can set the CXX_COMPILE_FLAGS for your interface file that are propagated to the attached header&c++ files. 有一个解决方案:如果您在CMake中编译以上代码,则可以为接口文件设置CXX_COMPILE_FLAGS,该文件将传播到附加的header&c ++文件中。 As such: 因此:
SET_SOURCE_FILES_PROPERTIES(sample.i PROPERTIES CPLUSPLUS ON)
if(CMAKE_BUILD_TYPE MATCHES "Release")
SET_SOURCE_FILES_PROPERTIES(sample.i PROPERTIES COMPILE_FLAGS "${CMAKE_CXX_FLAGS} -DSWIG")
SET_SOURCE_FILES_PROPERTIES(sample.i PROPERTIES CXX_COMPILE_FLAGS "${CMAKE_CXX_FLAGS} -DSWIG")
else(CMAKE_BUILD_TYPE MATCHES "Release")
SET_SOURCE_FILES_PROPERTIES(sample.i PROPERTIES COMPILE_FLAGS "${CMAKE_CXX_FLAGS_DEBUG} -DSWIG")
SET_SOURCE_FILES_PROPERTIES(sample.i PROPERTIES CXX_COMPILE_FLAGS "${CMAKE_CXX_FLAGS_DEBUG} -DSWIG")
endif()
SWIG_ADD_MODULE(sample python sample.i sample.cpp)
SWIG_LINK_LIBRARIES(Vector ${PYTHON_LIBRARIES})
In that case, the compilation is conditional (if you leave out the CXX_COMPILE_FLAGS part, there's no "a"), but if you want it it's there. 在这种情况下,编译是有条件的(如果您省略了CXX_COMPILE_FLAGS部分,则不会有“ a”),但是如果您愿意,它就在那里。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.