系统:Mac OS 10.12.6。Python:来自 Anconda3 的 Python 3.5.2。赛通==0.28
我正在尝试为静态 c++ 库编写包装器。这是头文件的一部分。
/* LexActivator.h */
#pragma once
#include <stdint.h>
#ifdef _WIN32
#include <windows.h>
#ifdef LEXACTIVATOR_EXPORTS
#ifdef LEXACTIVATOR__STATIC
#define LEXACTIVATOR_API extern "C"
#else
#define LEXACTIVATOR_API extern "C" __declspec(dllexport)
#endif
#else
#ifdef __cplusplus
#ifdef LEXACTIVATOR_STATIC
#define LEXACTIVATOR_API extern "C"
#else
#define LEXACTIVATOR_API extern "C" __declspec(dllimport)
#endif
#else
#ifdef LEXACTIVATOR_STATIC
#define LEXACTIVATOR_API
#else
#define LEXACTIVATOR_API __declspec(dllimport)
#endif
#endif
#endif
#if defined(USE_STDCALL_DLL) && !defined(LEXACTIVATOR_STATIC)
#define LA_CC __stdcall
#else
#define LA_CC __cdecl
#endif
typedef const wchar_t* CSTRTYPE;
typedef wchar_t* STRTYPE;
#else
#define LA_CC
typedef int32_t HRESULT;
#if __GNUC__ >= 4
#ifdef __cplusplus
#define LEXACTIVATOR_API extern "C" __attribute__((visibility("default")))
#else
#define LEXACTIVATOR_API __attribute__((visibility("default")))
#endif
#else
#ifdef __cplusplus
#define LEXACTIVATOR_API extern "C"
#else
#define LEXACTIVATOR_API
#endif
#endif
typedef const char* CSTRTYPE;
typedef char* STRTYPE;
#endif
#define LA_USER ((uint32_t)1)
#define LA_SYSTEM ((uint32_t)2)
#define LA_V_TRIAL ((uint32_t)1)
#define LA_UV_TRIAL ((uint32_t)2)
LEXACTIVATOR_API HRESULT LA_CC SetProductFile(CSTRTYPE filePath);
这是pxd
Cython 文件的一部分。
from libc.stdint cimport *
cdef extern from "LexActivator.h":
ctypedef int32_t HRESULT
ctypedef const char* CSTRTYPE
ctypedef char* STRTYPE
uint32_t LA_USER = 1
uint32_t LA_SYSTEM = 2
uint32_t LA_V_TRIAL = 1
uint32_t LA_UV_TRIAL = 2
HRESULT SetProductFile(CSTRTYPE filePath)
我pyx
为测试编写了一个简单的文件。
cimport LexActivator
def SetProductFile(filePath):
cdef bytes py_bytes = filePath.encode()
cdef const char* c_string = py_bytes
cdef int32_t status = LexActivator.SetProductFile(c_string)
print(status)
return status
安装文件
from distutils.core import setup
from distutils.extension import Extension
from Cython.Build import cythonize
ext_modules=[
Extension("LexActivator",
sources=["LexActivator.pyx"],
language='c++',
extra_objects=["libLexActivator.a"],
)
]
setup(
name = "LexActivator",
ext_modules = cythonize(ext_modules)
)
运行python setup.py build_ext --inplace
。
Error compiling Cython file:
------------------------------------------------------------
...
cimport LexActivator
def SetProductFile(filePath):
cdef bytes py_bytes = filePath.encode()
cdef const char* c_string = py_bytes
cdef int32_t status = LexActivator.SetProductFile(c_string)
^
------------------------------------------------------------
LexActivator.pyx:7:38: cimported module has no attribute 'SetProductFile'
PS:我已经成功地用 Xcode 完成了这个,只有 c 代码。