我正在尝试使用Visual Studio 2018和Python 3.7以及swig 3.0.12来简化一个简单的C ++类。 我的C ++标题
#ifndef atATObjectH
#define atATObjectH
#include "core/atCoreExporter.h"
#include <string>
//--------------------------------------------------------------------------
using std::string;
class AT_CORE ATObject
{
public:
ATObject();
virtual ~ATObject();
virtual const string getTypeName() const;
};
#endif
atCoreExporter.h:
#ifndef atCoreExporterH
#define atCoreExporterH
#if defined (_WIN32)
#if defined(AT_STATIC)
#define AT_CORE
#else
#if defined(AT_EXPORT_CORE)
#define AT_CORE __declspec(dllexport)
#else
#define AT_CORE __declspec(dllimport)
#endif
#endif
#else
#define AT_CORE
#endif
#endif
我创建了一个定义了 AT_EXPORT_CORE 的DLL。
我使用CMake生成swigged pyd模块。这是接口文件:
// atexplorer.i
%include "std_string.i"
%include "windows.i"
%module atexplorer
%{
#include "atATObject.h"
%}
//Expose class ATObject to Python
%include "atATObject.h"
这是CMake文件
FIND_PACKAGE(SWIG REQUIRED)
INCLUDE(${SWIG_USE_FILE})
FIND_PACKAGE(PythonLibs)
INCLUDE_DIRECTORIES(${PYTHON_INCLUDE_PATH})
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR})
SET(CMAKE_SWIG_FLAGS "")
SET_SOURCE_FILES_PROPERTIES(atexplorer.i PROPERTIES CPLUSPLUS ON)
SWIG_ADD_LIBRARY(atexplorer LANGUAGE python SOURCES atexplorer.i)
INCLUDE_DIRECTORIES(
.
${ATAPI_ROOT}
${ATAPI_ROOT}/source
${ATAPI_ROOT}/source/core
)
link_directories(${LIBRARY_OUTPUT_PATH})
SWIG_LINK_LIBRARIES (atexplorer
atCore
${PYTHON_LIBRARIES}
)
SET(ATEXPLORER_PACKAGE_DIR site-package)
SET(python_files_path ${CMAKE_BINARY_DIR}/wrappers/python/atexplorer)
INSTALL(
TARGETS _atexplorer
DESTINATION ${ATEXPLORER_PACKAGE_DIR}
COMPONENT python_module
)
INSTALL(
FILES ${python_files_path}/atexplorer.py __init__.py
DESTINATION ${ATEXPLORER_PACKAGE_DIR}
COMPONENT python_module
)
我遇到的问题是导出/导入宏。上面的接口文件无法编译,我从生成的SWIG代码中看到了这样的编译错误
AT_CORE * temp;
temp = reinterpret_cast< AT_CORE * >(argp);
ATObject = *temp;
其中AT_CORE是上面atCoreExporter.h中定义的导出/导入宏。
什么是正确的解决方案来解决这个问题?
答案 0 :(得分:0)
在使用宏之前,请确保swig接口文件包含宏头。我可以通过在.i文件中添加“%include”来解决此问题。
// atexplorer.i
%include "std_string.i"
%include "windows.i"
%module atexplorer
%{
#include "atATObject.h"
%}
//Expose class ATObject to Python
%include "atCoreExporter.h"
%include "atATObject.h"