[英]Anaconda on Windows10 : how to call C/C++ from python?
I have installed anaconda on windows 10 to get a working python (3.8.3) environnement.我已经在 windows 10 上安装了 anaconda 以获得工作 python (3.8.3) 环境。
I try to understand if it's possible to call C/C++ from python with boost-python
in this specific setup (anaconda + W10).我试图了解是否可以在此特定设置(anaconda + W10)中使用boost-python
从 python 调用 C/C++。
I installed boost
with conda install boost
.我用conda install boost
boost
I have a hello world example:我有一个你好世界的例子:
~> more .\hello.cpp
char const* greet()
{
return "hello, world";
}
#include <boost/python/python.hpp>
BOOST_PYTHON_MODULE(hello_bp)
{
using namespace boost::python;
def("greet", greet);
}
Now I run this command line in an anaconda prompt:现在我在 anaconda 提示符下运行这个命令行:
~> g++ -std=c++11 -o hello.so -shared -fPIC -I C:\App\Anaconda3\include -I C:\App\Anaconda3\Library\include hello.cpp -L C:\App\Anaconda3\Library\lib -lboost_python
...
C:\Users\...\AppData\Local\Temp\ccRcsfsi.o:hello.cpp:(.text$_ZN5boost6python9converter23expected_pytype_for_argIPKcE10get_pytypeEv[__ZN5boost6python9converter23expected_pytype_for_argIPKcE10get_pytypeEv]+0x33): undefined reference to `_imp___ZNK5boost6python9converter12registration25expected_from_python_typeEv'
collect2.exe: error: ld returned 1 exit status
Is it possible to call C/C++ using boost
from anaconda on W10?是否可以在 W10 上使用 anaconda 的boost
调用 C/C++? Or is this not supported?还是不支持?
UPDATE更新
A bit more of google-fu (add boost_system
and set BOOST_PYTHON_STATIC_LIB
) gives更多的google-fu(添加boost_system
并设置BOOST_PYTHON_STATIC_LIB
)给出
~> g++ -std=c++11 -o hello.so -shared -fPIC -DBOOST_PYTHON_STATIC_LIB=1 -I C:\App\Anaconda3\include -I C:\App\Anaconda3\Library\include hello.cpp -L C:\App\Anacond
a3 -L C:\App\Anaconda3\Lib -L C:\App\Anaconda3\Library\lib -lboost_python -lboost_system
...
C:\Users\...\AppData\Local\Temp\cctecCB0.o:hello.cpp:(.text$_ZN5boost6python9converter23expected_pytype_for_argIPKcE10get_pytypeEv[__ZN5boost6python9converter23expected_pytype_for_argIPKcE10get_pytypeEv]+0x31): undefined reference to `boost::python::converter::registration::expected_from_python_type() const'
collect2.exe: error: ld returned 1 exit status
Do this does not work either...这样做也不行...
UPDATE更新
Last unsuccessfull try最后一次失败的尝试
~> g++ -shared -fPIC -DBOOST_PYTHON_STATIC_LIB=1 -DPY_MAJOR_VERSION=3 -DPY_MINOR_VERSION=8 -I C:\App\Anaconda3\include -I C:\App\Anaconda3\Library\include -I C:\App\Anaconda3\Library\include\boost hello.cpp -L C:\App\Anaconda3\libs -L C:\App\Anaconda3\Library\lib -lboost_python38 -lboost_system -lpython38 -o hello.so
...
C:\Users\...\AppData\Local\Temp\ccNhQFY4.o:hello.cpp:(.text+0x3b): undefined reference to `_imp___Py_Dealloc'
C:\Users\...\AppData\Local\Temp\ccNhQFY4.o:hello.cpp:(.text+0xc7): undefined reference to `boost::python::detail::init_module(PyModuleDef&, void (*)())'
C:\Users\...\AppData\Local\Temp\ccNhQFY4.o:hello.cpp:(.text$_ZNK5boost6python9type_info4nameEv[__ZNK5boost6python9type_info4nameEv]+0x18): undefined reference to `boost::python::detail::gcc_demangle(char const*)'
C:\Users\...\AppData\Local\Temp\ccNhQFY4.o:hello.cpp:(.text$_ZNK5boost6python15to_python_valueIRKPKcEclES5_[__ZNK5boost6python15to_python_valueIRKPKcEclES5_]+0x12): undefined reference to `boost::python::converter::do_return_to_python(char const*)'
C:\Users\...\AppData\Local\Temp\ccNhQFY4.o:hello.cpp:(.text$_ZNK5boost6python15to_python_valueIRKPKcE10get_pytypeEv[__ZNK5boost6python15to_python_valueIRKPKcE10get_pytypeEv]+0xa): undefined reference to `_imp__PyUnicode_Type'
...
C:\Users\...\AppData\Local\Temp\ccNhQFY4.o:hello.cpp:(.text$_ZN5boost6python9converter23expected_pytype_for_argIPKcE10get_pytypeEv[__ZN5boost6python9converter23expected_pytype_for_argIPKcE10get_pytypeEv]+0x31): undefined reference to `boost::python::converter::registration::expected_from_python_type() const'
collect2.exe: error: ld returned 1 exit status
UPDATE更新
Try using cmake
yet again without success...再次尝试使用cmake
没有成功...
~> conda install cmake
~> git clone https://github.com/TNG/boost-python-examples
~> cd boost-python-examples\01-HelloWorld\
~> mkdir BUILD
~> cd BUILD
A few modification needed in hello world example: hello world 示例中需要进行一些修改:
~> git diff
diff --git a/01-HelloWorld/CMakeLists.txt b/01-HelloWorld/CMakeLists.txt
index 66af19c..1d4f3ab 100644
--- a/01-HelloWorld/CMakeLists.txt
+++ b/01-HelloWorld/CMakeLists.txt
@@ -1,4 +1,8 @@
+CMAKE_MINIMUM_REQUIRED(VERSION 3.18)
+FIND_PACKAGE(Boost 1.73 REQUIRED COMPONENTS python)
+
+PROJECT(hello)
PYTHON_ADD_MODULE(hello hello.cpp)
FILE(COPY hello.py DESTINATION .)
ADD_TEST(NAME 01-HelloWorld COMMAND ${PYTHON_EXECUTABLE} hello.py)
diff --git a/01-HelloWorld/hello.cpp b/01-HelloWorld/hello.cpp
index 2e2315a..8955462 100644
--- a/01-HelloWorld/hello.cpp
+++ b/01-HelloWorld/hello.cpp
@@ -3,7 +3,7 @@ char const* greet()
return "hello, world";
}
-#include <boost/python.hpp>
+#include <boost/python/python.hpp>
BOOST_PYTHON_MODULE(hello)
Is it possible to use boost-python
installed by conda install boost
on windows 10?是否可以在 windows 10 上使用 conda conda install boost
安装的boost-python
? Has this been tested / used?这是否经过测试/使用? Any specific tutorial?有具体教程吗? (this is just fine on debian...) (这在 debian 上很好......)
~\boost-python-examples\01-HelloWorld\BUILD> cmake ..
CMake Error at C:/App/Anaconda3/Library/lib/cmake/Boost-1.73.0/BoostConfig.cmake:141 (find_package):
Could not find a package configuration file provided by boost_python
(requested version 1.73.0) with any of the following names:
boost_pythonConfig.cmake
boost_python-config.cmake
Add the installation prefix of boost_python to CMAKE_PREFIX_PATH or set
boost_python_DIR to a directory containing one of the above files. If
boost_python provides a separate development package or SDK, be sure it
has been installed.
~\boost-python-examples\01-HelloWorld\BUILD> cmake -DCMAKE_PREFIX_PATH=C:\App\Anaconda3\Library\lib\cmake\cmake\boost_python-1.73.0 ..
-- Building for: Visual Studio 16 2019
CMake Error at C:/App/Anaconda3/Library/lib/cmake/cmake/BoostDetectToolset-1.73.0.cmake:5 (string):
string sub-command REGEX, mode MATCHALL needs at least 5 arguments total to
command.
Call Stack (most recent call first):
C:/App/Anaconda3/Library/lib/cmake/cmake/boost_python-1.73.0/boost_python-config.cmake:27 (include)
C:/App/Anaconda3/Library/lib/cmake/Boost-1.73.0/BoostConfig.cmake:141 (find_package)
C:/App/Anaconda3/Library/lib/cmake/Boost-1.73.0/BoostConfig.cmake:258 (boost_find_component)
C:/App/Anaconda3/Library/share/cmake-3.18/Modules/FindBoost.cmake:448 (find_package)
CMakeLists.txt:3 (FIND_PACKAGE)
-- Boost toolset is unknown (compiler )
-- Found Boost: C:/App/Anaconda3/Library/lib/cmake/Boost-1.73.0/BoostConfig.cmake (found suitable version "1.73.0", minimum required is "1.73") found components: python
-- Selecting Windows SDK version 10.0.18362.0 to target Windows 10.0.15063.
-- The C compiler identification is MSVC 19.28.29333.0
-- The CXX compiler identification is MSVC 19.28.29333.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.28.29333/bin/Hostx64/x64/cl.exe - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.28.29333/bin/Hostx64/x64/cl.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
CMake Error at CMakeLists.txt:6 (PYTHON_ADD_MODULE):
Unknown CMake command "PYTHON_ADD_MODULE".
```
Got pybind11
to work.让pybind11
工作。
~> conda install pybind11
~> more example
#include <pybind11/pybind11.h>
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");
}
~> more .\CMakeLists.txt
cmake_minimum_required(VERSION 3.18)
project(example LANGUAGES CXX)
find_package(pybind11 REQUIRED)
pybind11_add_module(example example.cpp)
~> mkdir BUILD; cd BUILD; cmake ..
A sln is created, opened in Visual Studio: compile / link OK.创建了一个 sln,在 Visual Studio 中打开:compile / link OK。 Runs OK.运行正常。
~/BUILD/Debug> python
>>> import example
>>> example.add(10, 3)
13
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.