问题
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