Anaconda on Windows10 : how to call C/C++ from python?

Viewed 2398

I have installed anaconda on windows 10 to get a working python (3.8.3) environnement.

I try to understand if it's possible to call C/C++ from python with boost-python in this specific setup (anaconda + W10).

I installed boost with conda install 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 :

~> 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 ? Or is this not supported ?

UPDATE

A bit more of google-fu (add boost_system and set BOOST_PYTHON_STATIC_LIB) gives

~> 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...

~> 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 :

~> 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 ? Has this been tested / used ? Any specific tutorial ? (this is just fine on 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".

```

1 Answers

Got pybind11 to work.

~> 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. Runs OK.

~/BUILD/Debug> python
>>> import example
>>> example.add(10, 3)
13

Related