在C ++文件中编译PYBIND11_MODULE之后的下一步是什么 [英] What are the next step after compiling PYBIND11_MODULE in C++ file
问题描述
我试图使用PYbind 11将以下小型C ++测试程序包装到Python模块中,因此我可以从python文件中调用test C ++函数。
我的问题是:虽然C ++文件已成功编译,但是我不知道接下来要采取什么步骤将新创建的模块文件导入python。
我尝试运行 from example import add在Spyder中的测试Python文件中,但收到错误消息,提示没有名为example的模块。
我正在使用Windows10(x64位),Python3.7和Visual Studio 2017社区。
有人可以帮忙吗?
#include
//#include< Aspose.Cells.h>非常感谢!
#include< pybind11 / pybind11.h>
void print(const char *);
int add(int i,int j){
return i + j;
}
PYBIND11_MODULE(example,m){
m.doc()= pybind11示例插件; //可选模块docstring
m.def( add,& add,将两个数字相加的函数);
}
int main()
{
const char * x = C Plus plus很棒。
char * z;
char b ='z';
z =& b;
int num = 10;
int * a = 0;
print(x);
}
void print(const char * z)
{
std :: cout<< 指针z为 << z<< \n;
std :: cin.get();
}
更新:构建测试c ++程序时,我遵循了Stuart的建议。我做了两次尝试:在第一次尝试中,我将目标扩展名更改为 .pyd。而在第二次尝试中,我将目标扩展名保持为 dll。
在两次尝试中,我都从Visual Studio收到了相同的错误消息,这似乎表明DLL文件正在无法启动内置命令(如后面的屏幕快照所示)
。
如我的更新的屏幕截图所示,最初,我的 example.pyd的类型为文件只是文件。我设法通过添加 cp35-win_amd64将其转换为Python扩展模块。在文件扩展名中,得到文件名 examplelib.cp35-win_amd64.pyd,然后删除添加的相同文本。
I was trying to use PYbind 11 to wrap the following small C++ test programme into a Python module, so I can call the test C++ function from python files. My problem is : while the C++ file compiled successfully, I have no clue as to what steps to take next to import the newly created module file in python. I tried to run "from example import add" in a test Python file in Spyder but received error message saying there is no module named example. I'm using Windows10 (x64bit), Python3.7 and Visual studio 2017 community. Can some one please help? Thank you very much!
#include
//#include <Aspose.Cells.h>
#include <pybind11/pybind11.h>
void print(const char*);
int add(int i, int j) {
return i + j;
}
PYBIND11_MODULE(example, m) {
m.doc() = "pybind11 example plugin"; // optional module docstring
m.def("add", &add, "A function which adds two numbers");
}
int main()
{
const char *x = "C Plus plus is wonderful.";
char *z;
char b = 'z';
z = &b;
int num = 10;
int* a = 0;
print(x);
}
void print(const char* z)
{
std::cout << "pointer z is" << z << "\n";
std::cin.get();
}
UPDATE: I followed Stuart's suggestion below when building my test c++ programme. I made two attempts: at the first attempt, I changed Target Extension to ".pyd"; whereas at the second attempt, I kept Target Extension as "dll".
In both attempts, I received the same error message from Visual Studio, which seem to suggest that the DLL file being built can not be started (as shown in the screenshot that immediately follows) Error Messages for Starting DLL Programme However, the actual building of dll file seemed successful, as I can see one dll file and one Python Extension Module file, with filenames and path listed as follows: C:\Users\rmili\source\repos\ConsoleApplication5\x64\Debug\ConsoleApplication5.dll and C:\Users\rmili\source\repos\ConsoleApplication5\x64\Debug\ConsoleApplication5
The Visual Studio output message upon building is pasted at the end.
My problem is: I created a Test.py file in the same directory (C:\Users\rmili\source\repos\ConsoleApplication5\x64\Debug) and tried to run it in Spyder after including just one line command "import example ". Sypder returned a error message saying " No MODULE Named Example".
Can anyone please help? Thanks a lot!
1>------ Build started: Project: ConsoleApplication5, Configuration: Debug x64 ------
1>LINK : C:\Users\rmili\source\repos\ConsoleApplication5\x64\Debug\ConsoleApplication5.dll not found or not built by the last incremental link; performing full link
1> Creating library C:\Users\rmili\source\repos\ConsoleApplication5\x64\Debug\ConsoleApplication5.lib and object C:\Users\rmili\source\repos\ConsoleApplication5\x64\Debug\ConsoleApplication5.exp
1>ConsoleApplication5.vcxproj -> C:\Users\rmili\source\repos\ConsoleApplication5\x64\Debug\ConsoleApplication5.dll
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========
UPDATE2: I followed every details of Stuart's instructions in his Update2. I got example.pyd (as shown in the following screenshot) However, I got error message when running in Spyder, as follows: (Sorry I only managed to copy the second half of Spyder output message as its very hard to do text selection in Spyder console)
File "C:\Users\rmili\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "C:/Users/rmili/source/repos/ConsoleApplication5/x64/Debug/Test.py", line 9
d = "C:\Users\rmili\source\repos\ConsoleApplication5\x64\Debug"
^
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape
I changed backward slash "" to forward slash"/" in the value that's being assigned to "d", and got the following error again:
File "C:\Users\rmili\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "C:/Users/rmili/source/repos/ConsoleApplication5/x64/Debug/Test.py", line 12, in <module>
import example
ModuleNotFoundError: No module named 'example'
UPDATE3: As shown in the following screenshot, example module can not be found in Windows command line prompt. [Unable to find example module in Windowscommand line prompt]3
I have found answer to my problem:
- Make sure all steps I described previously in my post are done
- this is what I missed -` It is important to make sure the file type of "example" is Python Extension Module, as shown in the following screenshot . As shown in screenshots of my Updates, initially the Type of my "example.pyd" file was just "File". I managed to convert it to Python Extension Module by adding "cp35-win_amd64." in the file extension, resulting in file name "examplelib.cp35-win_amd64.pyd", and then remove the same texts that were added.
这篇关于在C ++文件中编译PYBIND11_MODULE之后的下一步是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!