Python ImportError:动态模块未定义模块导出函数(PyInit_库)
我正试图编写一个用于Python的C模块,但是我很难编译程序。这是我的密码 图书馆cPython ImportError:动态模块未定义模块导出函数(PyInit_库),python,ctypes,Python,Ctypes,我正试图编写一个用于Python的C模块,但是我很难编译程序。这是我的密码 图书馆c #include <stdio.h> #include "/usr/include/python3.5/Python.h" #include "library.h" bool CaddBook(struct Library *lib, char *bookname, char * booktype, char *bookauthor, int numOfPages) { if (lib
#include <stdio.h>
#include "/usr/include/python3.5/Python.h"
#include "library.h"
bool CaddBook(struct Library *lib, char *bookname, char * booktype, char *bookauthor, int numOfPages) {
if (lib->booksStored == 0) {
lib->Books[0].book_name = bookname;
lib->Books[0].book_author = bookauthor;
lib->Books[0].book_type = booktype;
lib->Books[0].numOfPages = numOfPages;
lib->booksStored++;
return true;
}
int counter = lib->booksStored;
lib->Books[counter].book_name = bookname;
lib->Books[counter].book_author = bookauthor;
lib->Books[counter].book_type = booktype;
lib->Books[counter].numOfPages = numOfPages;
lib->booksStored++;
return true;
}
char * CsearchForBookByName(struct Library *lib, char *bkname) {
int storedBks = lib->booksStored;
for (int i = 0; i < storedBks; i++) {
if (lib->Books->book_name == bkname) {
return "Found";
}
}
return "Not Found";
}
char * CsearchForBookByAuthor(struct Library *lib, char *bkauthor) {
int storedBks = lib->booksStored;
for (int i = 0; i < storedBks; i++) {
if (lib->Books->book_author == bkauthor) {
return "Found";
}
}
return "Not Found";
}
// in python creating a python class(Structure) with attributes which then could be happily passed
// to any of the functions above
// getting return types of SeaFoBook and SeaBookAuthor and printing them
static PyObject* addBook(PyObject* self, PyObject* args)
{
struct Library *lib; char *bookname; char * booktype; char *bookauthor; int numOfPages;
if (!PyArg_ParseTuple(args, "Osssi", &lib, &bookname, &booktype, &bookauthor, &numOfPages))
return NULL;
return Py_BuildValue("p", CaddBook(lib, bookname, booktype, bookauthor, numOfPages));
}
static PyObject* searchForBookByName(PyObject* self, PyObject* args)
{
struct Library *lib; char *bookname;
if (!PyArg_ParseTuple(args, "O&s", &lib, &bookname))
return NULL;
return Py_BuildValue("s", CsearchForBookByName(lib, bookname));
}
static PyObject* searchForBookByAuthor(PyObject* self, PyObject* args)
{
struct Library *lib; char *bookauthor;
if (!PyArg_ParseTuple(args, "O&s", &lib, &bookauthor))
return NULL;
return Py_BuildValue("s", CsearchForBookByAuthor(lib, bookauthor));
}
static PyObject* version(PyObject* self)
{
return Py_BuildValue("s", "Version 1.0");
}
static PyMethodDef myMethods[] = {
{"addBook", addBook, METH_VARARGS, "This is adding a book to the library"},
{"searchForBookByName", searchForBookByName, METH_VARARGS, "Returns whether book name is found or not."},
{"searchForBookByAuthor", searchForBookByAuthor, METH_VARARGS, "Returns whether book is found or not based on author."},
{"version", (PyCFunction)version, METH_NOARGS, "Returns the version."},
{ NULL, NULL, 0, NULL}
};
static struct PyModuleDef myModule = {
PyModuleDef_HEAD_INIT,
"library",
"library Module",
-1,
myMethods
};
PyMODINIT_FUNC PyInit_myModule(void)
{
Py_Initialize();
return PyModule_Create(&myModule);
}
Library.h
#ifndef LIBRARY_LIBRARY_H
#define LIBRARY_LIBRARY_H
#include <stdbool.h>
typedef struct Book {
char *book_name;
char *book_type;
char *book_author;
int numOfPages;
};
typedef struct Library {
struct Book Books[100];
int booksStored;
};
bool CaddBook(struct Library *lib, char *bookname, char * booktype, char *bookauthor, int numOfPages);
char * CsearchForBookByName(struct Library *lib, char *bkname);
char * CsearchForBookByAuthor(struct Library *lib, char *bkauthor);
#endif //LIBRARY_LIBRARY_H
然后使用sudo python3 setup.py build\u ext--inplace,然后是cd build/(so\u location),然后是python3->import library从cmd行运行
老实说,我并不完全明白,任何建议都是有用的 您没有编写Python扩展。这是直截了当的C。看。如果您只想使用
ctypes
调用DLL,请编写一个常规DLL并读取.Hi标记,感谢这些链接一直在使用这些链接添加在C链接代码中/*添加一个内置模块,在Py_初始化之前*/PyImport_AppendInittab(“spam”,PyInit_myModule);第节和一直在跟踪,但似乎仍然有相同的消息和在终端中运行build and install cmd的剩余消息的问题。我曾尝试从文档中创建一个DLL并使用它,但在CLION告诉我PyInit_myModule'undeclared(此函数首次使用)时,我遇到了错误。我不知道为什么我的SO也会在2.7的目录中,因为我使用的是3.5。当在我的终端上运行python时,它默认为2.7,因为我被告知我不能rm软链接并更新到3.5,因为这可能会在需要2.7模块的程序上造成问题。你知道我真正了解的Boost python吗如果您有时间让我继续学习CPP和Python,那么非常感谢您的帮助?非常感谢。
from distutils.core import setup, Extension
module = Extension('library', sources = ['library.c'])
setup (name = 'PackageName',
version = '1.0',
description = 'This is a package for library',
ext_modules = [module])