import multiple cpp files with pybind11 and cppimport

让人想犯罪 __ 提交于 2019-12-25 18:34:51

问题


How can import multiple cpp files with pybind11?

As soon as my project spans over more than a single cpp file, I get an error:

    raise LinkError(msg)
E   distutils.errors.LinkError: command 'C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Community\\VC\\Tools\\MSVC\\14.24.28314\\bin\\HostX86\\x64\\link.exe' failed with exit status 1120

Any suggestions what I can do about it?

Here an example how it could look like:

other.h

void MyFunc();

main.cpp

/*
<%
setup_pybind11(cfg)
%>
*/
#include <pybind11/pybind11.h>

namespace py = pybind11;


PYBIND11_MODULE(main, m) {
    m.def("main", &main);
}
#include "other.h"
int main() {
    MyFunc();
}

other.cpp

#include "other.h"
#include <iostream>
void MyFunc() {
    std::cout << "Ohai from another .cpp file!";
    std::cin.get();
}

You can then run the code from python:

>>> import cppimport
>>> somecode = cppimport.imp("main") #This will pause for a moment to compile the module
>>> somecode.main()

The problem will be that the linker will not be aware of other.cpp.

来源:https://stackoverflow.com/questions/59231288/import-multiple-cpp-files-with-pybind11-and-cppimport

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!