将C模块与Python 3.7链接。 在Windows中使用CMake

我正在尝试创建一个可以从Python调用的C库,到目前为止我已经创建了暴露模块信息和方法表的代理C文件(为简单起见,只添加了一个方法get_cycle_count ,其实现无关紧要):

 static PyMethodDef NESulator_methods[] = { {"NESulator", get_cycle_count, METH_VARARGS, "Retrieves the current cycle count"}, {NULL, NULL, 0, NULL} /* Sentinel */ }; static struct PyModuleDef NESulator_module = { PyModuleDef_HEAD_INIT, "NESulator", /* name of module */ NULL, /* module documentation, may be NULL */ -1, /* size of per-interpreter state of the module, or -1 if the module keeps state in global variables. */ NESulator_methods }; /* * Should this be called somewhere? Will the linker do something with this? */ PyMODINIT_FUNC PyInit_NESulator(void) { return PyModule_Create(&NESulator_module); } 

现在, 文档说明(显然)我的C库必须以某种方式处理,以便Python可以实际导入它。 我已经得到了创建C库而不是使用CMake的可执行文件(和CLion,而不是VS,因此编译器是来自MinGW的gcc)但是我找不到也不明白我是如何使用CMake做的那样。

这段CMake生成一个名为libNESulator.a的文件,但是没有生成dlllib文件,并且.py文件(与.a库位于同一位置)在import libNESulatorimport NESulator时无法找到它是PyModuleDef结构中定义的名称。 所以我很确定那里缺少一些东西

 project(NESulator) set(CMAKE_C_STANDARD 11) add_subdirectory(src) find_package(PythonLibs REQUIRED) include_directories(${PYTHON_INCLUDE_DIRS}) add_library(NESulator "all of my .c files in the src directory previously included" ) target_link_libraries(NESulator ${PYTHON_LIBRARIES}) 

所以……是的,您是否知道如何将libNESulator.a转换为Python可调用模块? 我需要在这个CMake中做些什么来实现它?

如果你有一个更容易的方式或任何其他方式,请随意提出它。 我对CMake和Python很陌生,所以我不知道你是否需要我的更多信息,比如项目结构等等,但如果你这样做,请告诉我。

非常感谢