简体   繁体   中英

Linking python and c++ on windows

I'm trying to create a python library, written in c++ , using boost-python , and mingw to compile.

c++ Code:

char const* greet()
{
   return "hello, world";
}

#include <boost/python.hpp>

BOOST_PYTHON_MODULE(hello)
{
    using namespace boost::python;
    def("greet", greet);
}

So far this is what I have done:

  • python3.9 already installed
  • Install mingw/g++
  • download boost zip
  • run the following two commands:

g++ -o middle -m64 -D MS_WIN64 -c -I path\to\boost_1_79_0 -I path\to\Python39\include" hello.cpp

Output (warnings i think):

In file included from C:\Users\Pichi\Downloads\boost_1_79_0/boost/python/detail/is_xxx.hpp:8,
                 from C:\Users\Pichi\Downloads\boost_1_79_0/boost/python/detail/is_auto_ptr.hpp:9,
                 from C:\Users\Pichi\Downloads\boost_1_79_0/boost/python/detail/copy_ctor_mutates_rhs.hpp:8,
                 from C:\Users\Pichi\Downloads\boost_1_79_0/boost/python/detail/value_arg.hpp:7,
                 from C:\Users\Pichi\Downloads\boost_1_79_0/boost/python/object/forward.hpp:10,
                 from C:\Users\Pichi\Downloads\boost_1_79_0/boost/python/object/pointer_holder.hpp:16,
                 from C:\Users\Pichi\Downloads\boost_1_79_0/boost/python/to_python_indirect.hpp:10,
                 from C:\Users\Pichi\Downloads\boost_1_79_0/boost/python/converter/arg_to_python.hpp:10,
                 from C:\Users\Pichi\Downloads\boost_1_79_0/boost/python/call.hpp:15,
                 from C:\Users\Pichi\Downloads\boost_1_79_0/boost/python/object_core.hpp:14,
                 from C:\Users\Pichi\Downloads\boost_1_79_0/boost/python/args.hpp:22,
                 from C:\Users\Pichi\Downloads\boost_1_79_0/boost/python.hpp:11,
                 from hello.cpp:6:
C:\Users\Pichi\Downloads\boost_1_79_0/boost/python/detail/is_auto_ptr.hpp:17:40: warning: 'template<class> class std::auto_ptr' is deprecated: use 'std::unique_ptr' instead [-Wdeprecated-declarations]
   17 | BOOST_PYTHON_IS_XXX_DEF(auto_ptr, std::auto_ptr, 1)
      |                                        ^~~~~~~~
C:\Users\Pichi\Downloads\boost_1_79_0/boost/detail/is_xxx.hpp:20:4: note: in definition of macro 'BOOST_DETAIL_IS_XXX_DEF'
   20 |    qualified_name< BOOST_PP_ENUM_PARAMS_Z(1, nargs, T) >        \
      |    ^~~~~~~~~~~~~~
C:\Users\Pichi\Downloads\boost_1_79_0/boost/python/detail/is_auto_ptr.hpp:17:1: note: in expansion of macro 'BOOST_PYTHON_IS_XXX_DEF'
   17 | BOOST_PYTHON_IS_XXX_DEF(auto_ptr, std::auto_ptr, 1)
      | ^~~~~~~~~~~~~~~~~~~~~~~
In file included from C:/msys64/mingw64/include/c++/11.2.0/memory:76,
                 from C:\Users\Pichi\Downloads\boost_1_79_0/boost/function/function_base.hpp:16,
                 from C:\Users\Pichi\Downloads\boost_1_79_0/boost/function/detail/prologue.hpp:17,
                 from C:\Users\Pichi\Downloads\boost_1_79_0/boost/function/function_template.hpp:13,
                 from C:\Users\Pichi\Downloads\boost_1_79_0/boost/function/detail/maybe_include.hpp:15,
                 from C:\Users\Pichi\Downloads\boost_1_79_0/boost/function/function0.hpp:11,
                 from C:\Users\Pichi\Downloads\boost_1_79_0/boost/python/errors.hpp:13,
                 from C:\Users\Pichi\Downloads\boost_1_79_0/boost/python/handle.hpp:11,
                 from C:\Users\Pichi\Downloads\boost_1_79_0/boost/python/args_fwd.hpp:10,
                 from C:\Users\Pichi\Downloads\boost_1_79_0/boost/python/args.hpp:10,
                 from C:\Users\Pichi\Downloads\boost_1_79_0/boost/python.hpp:11,
                 from hello.cpp:6:
C:/msys64/mingw64/include/c++/11.2.0/bits/unique_ptr.h:57:28: note: declared here
   57 |   template<typename> class auto_ptr;
      |                            ^~~~~~~~
In file included from C:\Users\Pichi\Downloads\boost_1_79_0/boost/python/converter/pyobject_traits.hpp:9,
                 from C:\Users\Pichi\Downloads\boost_1_79_0/boost/python/converter/object_manager.hpp:10,
                 from C:\Users\Pichi\Downloads\boost_1_79_0/boost/python/to_python_value.hpp:19,
                 from C:\Users\Pichi\Downloads\boost_1_79_0/boost/python/detail/invoke.hpp:19,
                 from C:\Users\Pichi\Downloads\boost_1_79_0/boost/python/detail/caller.hpp:16,
                 from C:\Users\Pichi\Downloads\boost_1_79_0/boost/python/object/function_handle.hpp:8,
                 from C:\Users\Pichi\Downloads\boost_1_79_0/boost/python/converter/arg_to_python.hpp:19,
                 from C:\Users\Pichi\Downloads\boost_1_79_0/boost/python/call.hpp:15,
                 from C:\Users\Pichi\Downloads\boost_1_79_0/boost/python/object_core.hpp:14,
                 from C:\Users\Pichi\Downloads\boost_1_79_0/boost/python/args.hpp:22,
                 from C:\Users\Pichi\Downloads\boost_1_79_0/boost/python.hpp:11,
                 from hello.cpp:6:
C:\Users\Pichi\Downloads\boost_1_79_0/boost/python/converter/pyobject_type.hpp:13:11: warning: inline function 'PyObject* boost::python::converter::checked_downcast_impl(PyObject*, PyTypeObject*)' declared as dllimport: attribute ignored [-Wattributes]
   13 | PyObject* checked_downcast_impl(PyObject *obj, PyTypeObject *type)
      |           ^~~~~~~~~~~~~~~~~~~~~
In file included from C:\Users\Pichi\Downloads\boost_1_79_0/boost/preprocessor/iteration/detail/iter/limits/forward1_256.hpp:14,
                 from C:\Users\Pichi\Downloads\boost_1_79_0/boost/preprocessor/iteration/detail/iter/forward1.hpp:1343,
                 from C:\Users\Pichi\Downloads\boost_1_79_0/boost/python/call_method.hpp:32,
                 from C:\Users\Pichi\Downloads\boost_1_79_0/boost/python.hpp:17,
                 from hello.cpp:6:
C:\Users\Pichi\Downloads\boost_1_79_0/boost/python/call_method.hpp: In function 'typename boost::python::detail::returnable<T>::type boost::python::call_method(PyObject*, const char*, boost::type<Target>*)':
C:\Users\Pichi\Downloads\boost_1_79_0/boost/python/call_method.hpp:61:26: warning: 'PyObject* PyEval_CallMethod(PyObject*, const char*, const char*, ...)' is deprecated [-Wdeprecated-declarations]
   61 |     PyObject* const result =
      |         ~~~~~~~~~~~~~~~~~^~~
   62 |         PyEval_CallMethod(
      |         ~~~~~~~~~~~~~~~~~~
   63 |             self
      |             ~~~~
   64 |             , const_cast<char*>(name)
      |             ~~~~~~~~~~~~~~~~~~~~~~~~~
   65 |             , const_cast<char*>("(" BOOST_PP_REPEAT_1ST(N, BOOST_PYTHON_FIXED, "O") ")")
      |             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   66 |             BOOST_PP_REPEAT_1ST(N, BOOST_PYTHON_FAST_ARG_TO_PYTHON_GET, nil)
      |             ~
In file included from C:\Program Files\Python39\include/Python.h:154,
                 from C:\Users\Pichi\Downloads\boost_1_79_0/boost/python/detail/wrap_python.hpp:178,
                 from C:\Users\Pichi\Downloads\boost_1_79_0/boost/python/detail/prefix.hpp:13,
                 from C:\Users\Pichi\Downloads\boost_1_79_0/boost/python/args.hpp:8,
                 from C:\Users\Pichi\Downloads\boost_1_79_0/boost/python.hpp:11,
                 from hello.cpp:6:
C:\Program Files\Python39\include/ceval.h:28:43: note: declared here
   28 | Py_DEPRECATED(3.9) PyAPI_FUNC(PyObject *) PyEval_CallMethod(
      |                                           ^~~~~~~~~~~~~~~~~

g++ -shared -o hello.so -L "C:\Program Files\Python39\libs" -lpython39 middle

Output (ends in error):

C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: middle:hello.cpp:(.text+0x60): undefined reference to `__imp__Py_Dealloc'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: middle:hello.cpp:(.text+0x109): undefined reference to `__imp__ZN5boost6python6detail11init_moduleER11PyModuleDefPFvvE'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: middle:hello.cpp:(.text$_ZNK5boost6python9type_info4nameEv[_ZNK5boost6python9type_info4nameEv]+0x21): undefined reference to `__imp__ZN5boost6python6detail12gcc_demangleEPKc'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: middle:hello.cpp:(.text$_ZNK5boost6python15to_python_valueIRKPKcEclES5_[_ZNK5boost6python15to_python_valueIRKPKcEclES5_]+0x1d): undefined reference to `__imp__ZN5boost6python9converter19do_return_to_pythonEPKc'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: middle:hello.cpp:(.text$_ZNK5boost6python15to_python_valueIRKPKcE10get_pytypeEv[_ZNK5boost6python15to_python_valueIRKPKcE10get_pytypeEv]+0xb): undefined reference to `__imp_PyUnicode_Type'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: middle:hello.cpp:(.text$_ZN5boost6python3api6objectC2Ev[_ZN5boost6python3api6objectC2Ev]+0x16): undefined reference to `__imp__Py_NoneStruct'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: middle:hello.cpp:(.text$_ZN5boost6python3defIPFPKcvEEEvS3_T_[_ZN5boost6python3defIPFPKcvEEEvS3_T_]+0x3e): undefined reference to `__imp__ZN5boost6python6detail17scope_setattr_docEPKcRKNS0_3api6objectES3_'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: middle:hello.cpp:(.text$_ZN5boost6python6detail17make_function_auxIPFPKcvENS0_21default_call_policiesENS_3mpl7vector1IS4_EEEENS0_3api6objectET_RKT0_RKT1_[_ZN5boost6python6detail17make_function_auxIPFPKcvENS0_21default_call_policiesENS_3mpl7vector1IS4_EEEENS0_3api6objectET_RKT0_RKT1_]+0x4c): undefined reference to `__imp__ZN5boost6python7objects15function_objectERKNS1_11py_functionE'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: middle:hello.cpp:(.text$_ZN5boost6python7objects21py_function_impl_baseC2Ev[_ZN5boost6python7objects21py_function_impl_baseC2Ev]+0xb): undefined reference to `__imp__ZTVN5boost6python7objects21py_function_impl_baseE'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: middle:hello.cpp:(.rdata$_ZTVN5boost6python7objects23caller_py_function_implINS0_6detail6callerIPFPKcvENS0_21default_call_policiesENS_3mpl7vector1IS6_EEEEEE[_ZTVN5boost6python7objects23caller_py_function_implINS0_6detail6callerIPFPKcvENS0_21default_call_policiesENS_3mpl7vector1IS6_EEEEEE]+0x30): undefined reference to `boost::python::objects::py_function_impl_base::max_arity() const'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: middle:hello.cpp:(.text$_ZN5boost6python7objects23caller_py_function_implINS0_6detail6callerIPFPKcvENS0_21default_call_policiesENS_3mpl7vector1IS6_EEEEED1Ev[_ZN5boost6python7objects23caller_py_function_implINS0_6detail6callerIPFPKcvENS0_21default_call_policiesENS_3mpl7vector1IS6_EEEEED1Ev]+0x24): undefined reference to `__imp__ZN5boost6python7objects21py_function_impl_baseD2Ev'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: middle:hello.cpp:(.text$_ZN5boost6python9converter23expected_pytype_for_argIPKcE10get_pytypeEv[_ZN5boost6python9converter23expected_pytype_for_argIPKcE10get_pytypeEv]+0x1d): undefined reference to `__imp__ZN5boost6python9converter8registry5queryENS0_9type_infoE'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: middle:hello.cpp:(.text$_ZN5boost6python9converter23expected_pytype_for_argIPKcE10get_pytypeEv[_ZN5boost6python9converter23expected_pytype_for_argIPKcE10get_pytypeEv]+0x38): undefined reference to `__imp__ZNK5boost6python9converter12registration25expected_from_python_typeEv'
collect2.exe: error: ld returned 1 exit status

I haven't been able to find the solution anywhere. Somebody could help me?

EDIT In the effort of trying to solve my problem, I tried cmake instead of g++ .

i did the following:

  • install cmake .
  • install make .
  • create the "build" folder, inside my project.
  • create MyProject.py script.
  • create CMakeCache.txt file.
  • run two commands.

MyProject.py script:

import hello
print(hello.greet())

CMakeCache.txt file:

cmake_minimum_required(VERSION 2.8.3)
FIND_PACKAGE(PythonInterp)
FIND_PACKAGE(PythonLibs)

set(Boost_NO_SYSTEM_PATHS TRUE)
if (Boost_NO_SYSTEM_PATHS)
  set(BOOST_ROOT "C:/boost/1.79.0")
  set(BOOST_INCLUDE_DIRS "${BOOST_ROOT}/include")
  set(BOOST_LIBRARY_DIRS "${BOOST_ROOT}/lib")
endif (Boost_NO_SYSTEM_PATHS)

FIND_PACKAGE(Boost COMPONENTS python39)

INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIRS} ${PYTHON_INCLUDE_DIRS})

PYTHON_ADD_MODULE(NativeLib hello)
FILE(COPY MyProject.py DESTINATION .)

(inside "build" dir) command 1: cmake -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Release..

output:

CMake Warning (dev) in CMakeLists.txt:
  No project() command is present.  The top-level CMakeLists.txt file must
  contain a literal, direct call to the project() command.  Add a line of
  code such as

    project(ProjectName)

  near the top of the file, but after cmake_minimum_required().

  CMake is pretending there is a "project(Project)" command on the first
  line.
This warning is for project developers.  Use -Wno-dev to suppress it.

-- The C compiler identification is GNU 11.2.0
-- The CXX compiler identification is GNU 11.2.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: C:/msys64/mingw64/bin/cc.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:/msys64/mingw64/bin/c++.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
CMake Deprecation Warning at CMakeLists.txt:1 (cmake_minimum_required):
  Compatibility with CMake < 2.8.12 will be removed from a future version of
  CMake.

  Update the VERSION argument <min> value or use a ...<max> suffix to tell
  CMake that the project does not need compatibility with older versions.


-- Found PythonInterp: C:/Program Files/Python39/python.exe (found version "3.9.7")
-- Found PythonLibs: C:/Program Files/Python39/libs/python39.lib (found version "3.9.7")
-- Could NOT find Boost: missing: python39 (found C:/boost/1.79.0/lib/cmake/Boost-1.79.0/BoostConfig.cmake (found version "1.79.0"))
-- Configuring done
CMake Warning (dev) at C:/Program Files/CMake/share/cmake-3.23/Modules/FindPythonLibs.cmake:337 (add_library):
  Policy CMP0115 is not set: Source file extensions must be explicit.  Run
  "cmake --help-policy CMP0115" for policy details.  Use the cmake_policy
  command to set the policy and suppress this warning.

  File:

    C:/Users/Pichi/Desktop/New folder/hello.cpp
Call Stack (most recent call first):
  CMakeLists.txt:16 (PYTHON_ADD_MODULE)
This warning is for project developers.  Use -Wno-dev to suppress it.

-- Generating done
-- Build files have been written to: C:/Users/Pichi/Desktop/New folder/build

command 2: make

output:

[ 50%] Building CXX object CMakeFiles/NativeLib.dir/hello.cpp.obj
In file included from C:/boost/1.79.0/include/boost-1_79/boost/python/detail/is_xxx.hpp:8,
                 from C:/boost/1.79.0/include/boost-1_79/boost/python/detail/is_auto_ptr.hpp:9,
                 from C:/boost/1.79.0/include/boost-1_79/boost/python/detail/copy_ctor_mutates_rhs.hpp:8,
                 from C:/boost/1.79.0/include/boost-1_79/boost/python/detail/value_arg.hpp:7,
                 from C:/boost/1.79.0/include/boost-1_79/boost/python/object/forward.hpp:10,
                 from C:/boost/1.79.0/include/boost-1_79/boost/python/object/pointer_holder.hpp:16,
                 from C:/boost/1.79.0/include/boost-1_79/boost/python/to_python_indirect.hpp:10,
                 from C:/boost/1.79.0/include/boost-1_79/boost/python/converter/arg_to_python.hpp:10,
                 from C:/boost/1.79.0/include/boost-1_79/boost/python/call.hpp:15,
                 from C:/boost/1.79.0/include/boost-1_79/boost/python/object_core.hpp:14,
                 from C:/boost/1.79.0/include/boost-1_79/boost/python/args.hpp:22,
                 from C:/boost/1.79.0/include/boost-1_79/boost/python.hpp:11,
                 from C:/Users/Pichi/Desktop/New folder/hello.cpp:6:
C:/boost/1.79.0/include/boost-1_79/boost/python/detail/is_auto_ptr.hpp:17:40: warning: 'template<class> class std::auto_ptr' is deprecated: use 'std::unique_ptr' instead [-Wdeprecated-declarations]
   17 | BOOST_PYTHON_IS_XXX_DEF(auto_ptr, std::auto_ptr, 1)
      |                                        ^~~~~~~~
C:/boost/1.79.0/include/boost-1_79/boost/detail/is_xxx.hpp:20:4: note: in definition of macro 'BOOST_DETAIL_IS_XXX_DEF'
   20 |    qualified_name< BOOST_PP_ENUM_PARAMS_Z(1, nargs, T) >        \
      |    ^~~~~~~~~~~~~~
C:/boost/1.79.0/include/boost-1_79/boost/python/detail/is_auto_ptr.hpp:17:1: note: in expansion of macro 'BOOST_PYTHON_IS_XXX_DEF'
   17 | BOOST_PYTHON_IS_XXX_DEF(auto_ptr, std::auto_ptr, 1)
      | ^~~~~~~~~~~~~~~~~~~~~~~
In file included from C:/msys64/mingw64/include/c++/11.2.0/memory:76,
                 from C:/boost/1.79.0/include/boost-1_79/boost/function/function_base.hpp:16,
                 from C:/boost/1.79.0/include/boost-1_79/boost/function/detail/prologue.hpp:17,
                 from C:/boost/1.79.0/include/boost-1_79/boost/function/function_template.hpp:13,
                 from C:/boost/1.79.0/include/boost-1_79/boost/function/detail/maybe_include.hpp:15,
                 from C:/boost/1.79.0/include/boost-1_79/boost/function/function0.hpp:11,
                 from C:/boost/1.79.0/include/boost-1_79/boost/python/errors.hpp:13,
                 from C:/boost/1.79.0/include/boost-1_79/boost/python/handle.hpp:11,
                 from C:/boost/1.79.0/include/boost-1_79/boost/python/args_fwd.hpp:10,
                 from C:/boost/1.79.0/include/boost-1_79/boost/python/args.hpp:10,
                 from C:/boost/1.79.0/include/boost-1_79/boost/python.hpp:11,
                 from C:/Users/Pichi/Desktop/New folder/hello.cpp:6:
C:/msys64/mingw64/include/c++/11.2.0/bits/unique_ptr.h:57:28: note: declared here
   57 |   template<typename> class auto_ptr;
      |                            ^~~~~~~~
In file included from C:/boost/1.79.0/include/boost-1_79/boost/python/converter/pyobject_traits.hpp:9,
                 from C:/boost/1.79.0/include/boost-1_79/boost/python/converter/object_manager.hpp:10,
                 from C:/boost/1.79.0/include/boost-1_79/boost/python/to_python_value.hpp:19,
                 from C:/boost/1.79.0/include/boost-1_79/boost/python/detail/invoke.hpp:19,
                 from C:/boost/1.79.0/include/boost-1_79/boost/python/detail/caller.hpp:16,
                 from C:/boost/1.79.0/include/boost-1_79/boost/python/object/function_handle.hpp:8,
                 from C:/boost/1.79.0/include/boost-1_79/boost/python/converter/arg_to_python.hpp:19,
                 from C:/boost/1.79.0/include/boost-1_79/boost/python/call.hpp:15,
                 from C:/boost/1.79.0/include/boost-1_79/boost/python/object_core.hpp:14,
                 from C:/boost/1.79.0/include/boost-1_79/boost/python/args.hpp:22,
                 from C:/boost/1.79.0/include/boost-1_79/boost/python.hpp:11,
                 from C:/Users/Pichi/Desktop/New folder/hello.cpp:6:
C:/boost/1.79.0/include/boost-1_79/boost/python/converter/pyobject_type.hpp:13:11: warning: inline function 'PyObject* boost::python::converter::checked_downcast_impl(PyObject*, PyTypeObject*)' declared as dllimport: attribute ignored [-Wattributes]
   13 | PyObject* checked_downcast_impl(PyObject *obj, PyTypeObject *type)
      |           ^~~~~~~~~~~~~~~~~~~~~
In file included from C:/boost/1.79.0/include/boost-1_79/boost/preprocessor/iteration/detail/iter/limits/forward1_256.hpp:14,
                 from C:/boost/1.79.0/include/boost-1_79/boost/preprocessor/iteration/detail/iter/forward1.hpp:1343,
                 from C:/boost/1.79.0/include/boost-1_79/boost/python/call_method.hpp:32,
                 from C:/boost/1.79.0/include/boost-1_79/boost/python.hpp:17,
                 from C:/Users/Pichi/Desktop/New folder/hello.cpp:6:
C:/boost/1.79.0/include/boost-1_79/boost/python/call_method.hpp: In function 'typename boost::python::detail::returnable<T>::type boost::python::call_method(PyObject*, const char*, boost::type<Target>*)':
C:/boost/1.79.0/include/boost-1_79/boost/python/call_method.hpp:61:26: warning: 'PyObject* PyEval_CallMethod(PyObject*, const char*, const char*, ...)' is deprecated [-Wdeprecated-declarations]
   61 |     PyObject* const result =
      |         ~~~~~~~~~~~~~~~~~^~~
   62 |         PyEval_CallMethod(
      |         ~~~~~~~~~~~~~~~~~~
   63 |             self
      |             ~~~~
   64 |             , const_cast<char*>(name)
      |             ~~~~~~~~~~~~~~~~~~~~~~~~~
   65 |             , const_cast<char*>("(" BOOST_PP_REPEAT_1ST(N, BOOST_PYTHON_FIXED, "O") ")")
      |             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   66 |             BOOST_PP_REPEAT_1ST(N, BOOST_PYTHON_FAST_ARG_TO_PYTHON_GET, nil)
      |             ~
In file included from C:/Program Files/Python39/include/Python.h:154,
                 from C:/boost/1.79.0/include/boost-1_79/boost/python/detail/wrap_python.hpp:178,
                 from C:/boost/1.79.0/include/boost-1_79/boost/python/detail/prefix.hpp:13,
                 from C:/boost/1.79.0/include/boost-1_79/boost/python/args.hpp:8,
                 from C:/boost/1.79.0/include/boost-1_79/boost/python.hpp:11,
                 from C:/Users/Pichi/Desktop/New folder/hello.cpp:6:
C:/Program Files/Python39/include/ceval.h:28:43: note: declared here
   28 | Py_DEPRECATED(3.9) PyAPI_FUNC(PyObject *) PyEval_CallMethod(
      |                                           ^~~~~~~~~~~~~~~~~
[100%] Linking CXX shared module NativeLib.pyd
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles/NativeLib.dir/objects.a(hello.cpp.obj):hello.cpp:(.text+0x32): undefined reference to `__imp__Py_Dealloc'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles/NativeLib.dir/objects.a(hello.cpp.obj):hello.cpp:(.text+0x79): undefined reference to `__imp__ZN5boost6python7objects15function_objectERKNS1_11py_functionE'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles/NativeLib.dir/objects.a(hello.cpp.obj):hello.cpp:(.text+0x9c): undefined reference to `__imp__ZN5boost6python6detail17scope_setattr_docEPKcRKNS0_3api6objectES3_'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles/NativeLib.dir/objects.a(hello.cpp.obj):hello.cpp:(.text+0xba): undefined reference to `__imp__Py_Dealloc'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles/NativeLib.dir/objects.a(hello.cpp.obj):hello.cpp:(.text+0xd6): undefined reference to `__imp__Py_Dealloc'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles/NativeLib.dir/objects.a(hello.cpp.obj):hello.cpp:(.text+0x128): undefined reference to `__imp__ZN5boost6python6detail11init_moduleER11PyModuleDefPFvvE'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles/NativeLib.dir/objects.a(hello.cpp.obj):hello.cpp:(.text$_ZN5boost6python6detail21converter_target_typeINS0_15to_python_valueIRKPKcEEE10get_pytypeEv[_ZN5boost6python6detail21converter_target_typeINS0_15to_python_valueIRKPKcEEE10get_pytypeEv]+0x3): undefined reference to `__imp_PyUnicode_Type'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles/NativeLib.dir/objects.a(hello.cpp.obj):hello.cpp:(.text$_ZNK5boost6python7objects23caller_py_function_implINS0_6detail6callerIPFPKcvENS0_21default_call_policiesENS_3mpl7vector1IS6_EEEEE9signatureEv[_ZNK5boost6python7objects23caller_py_function_implINS0_6detail6callerIPFPKcvENS0_21default_call_policiesENS_3mpl7vector1IS6_EEEEE9signatureEv]+0x7b): undefined reference to `__imp__ZN5boost6python6detail12gcc_demangleEPKc'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles/NativeLib.dir/objects.a(hello.cpp.obj):hello.cpp:(.text$_ZNK5boost6python7objects23caller_py_function_implINS0_6detail6callerIPFPKcvENS0_21default_call_policiesENS_3mpl7vector1IS6_EEEEE9signatureEv[_ZNK5boost6python7objects23caller_py_function_implINS0_6detail6callerIPFPKcvENS0_21default_call_policiesENS_3mpl7vector1IS6_EEEEE9signatureEv]+0xcf): undefined reference to `__imp__ZN5boost6python6detail12gcc_demangleEPKc'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles/NativeLib.dir/objects.a(hello.cpp.obj):hello.cpp:(.text$_ZN5boost6python9converter23expected_pytype_for_argIPKcE10get_pytypeEv[_ZN5boost6python9converter23expected_pytype_for_argIPKcE10get_pytypeEv]+0x1c): undefined reference to `__imp__ZN5boost6python9converter8registry5queryENS0_9type_infoE'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles/NativeLib.dir/objects.a(hello.cpp.obj):hello.cpp:(.text$_ZN5boost6python9converter23expected_pytype_for_argIPKcE10get_pytypeEv[_ZN5boost6python9converter23expected_pytype_for_argIPKcE10get_pytypeEv]+0x2f): undefined reference to `__imp__ZNK5boost6python9converter12registration25expected_from_python_typeEv'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles/NativeLib.dir/objects.a(hello.cpp.obj):hello.cpp:(.text$_ZN5boost6python7objects23caller_py_function_implINS0_6detail6callerIPFPKcvENS0_21default_call_policiesENS_3mpl7vector1IS6_EEEEEclEP7_objectSG_[_ZN5boost6python7objects23caller_py_function_implINS0_6detail6callerIPFPKcvENS0_21default_call_policiesENS_3mpl7vector1IS6_EEEEEclEP7_objectSG_]+0x11): undefined reference to `__imp__ZN5boost6python9converter19do_return_to_pythonEPKc'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles/NativeLib.dir/objects.a(hello.cpp.obj):hello.cpp:(.text$_ZN5boost6python7objects23caller_py_function_implINS0_6detail6callerIPFPKcvENS0_21default_call_policiesENS_3mpl7vector1IS6_EEEEED1Ev[_ZN5boost6python7objects23caller_py_function_implINS0_6detail6callerIPFPKcvENS0_21default_call_policiesENS_3mpl7vector1IS6_EEEEED1Ev]+0xd): undefined reference to `__imp__ZN5boost6python7objects21py_function_impl_baseD2Ev'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles/NativeLib.dir/objects.a(hello.cpp.obj):hello.cpp:(.text$_ZN5boost6python7objects23caller_py_function_implINS0_6detail6callerIPFPKcvENS0_21default_call_policiesENS_3mpl7vector1IS6_EEEEED0Ev[_ZN5boost6python7objects23caller_py_function_implINS0_6detail6callerIPFPKcvENS0_21default_call_policiesENS_3mpl7vector1IS6_EEEEED0Ev]+0x15): undefined reference to `__imp__ZN5boost6python7objects21py_function_impl_baseD2Ev'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles/NativeLib.dir/objects.a(hello.cpp.obj):hello.cpp:(.text.startup+0x3): undefined reference to `__imp__Py_NoneStruct'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles/NativeLib.dir/objects.a(hello.cpp.obj):hello.cpp:(.rdata$_ZTVN5boost6python7objects23caller_py_function_implINS0_6detail6callerIPFPKcvENS0_21default_call_policiesENS_3mpl7vector1IS6_EEEEEE[_ZTVN5boost6python7objects23caller_py_function_implINS0_6detail6callerIPFPKcvENS0_21default_call_policiesENS_3mpl7vector1IS6_EEEEEE]+0x30): undefined reference to `boost::python::objects::py_function_impl_base::max_arity() const'
collect2.exe: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/NativeLib.dir/build.make:101: NativeLib.pyd] Error 1
make[1]: *** [CMakeFiles/Makefile2:83: CMakeFiles/NativeLib.dir/all] Error 2
make: *** [Makefile:91: all] Error 2

EDIT

the suggestion given what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix does not solve my problem, because even though it can be understood that it is about linking at compile time, my c++ script uses boost-python , an important element that they do not refer to, and therefore I do not find the way to adapt the given solutions to my problem.

You never linked the extension to boost-python , or the python libraries. Just looking at the python_add_module function ( https://github.com/Kitware/CMake/blob/master/Modules/FindPythonLibs.cmake ), it doesn't do a whole lot - outside of making sure the library has the correct suffix. It should work if you add

target_link_libraries(NativeLib ${Boost_LIBRARIES} ${PYTHON_LIBRARIES})

to your CMakeLists.txt file. There's also a few issues with some of your naming, eg in MyProject.py you do import hello but you've named the extension as NativeLib . You may find https://iainhaslam.com/scraps/hello-boost-python/ useful.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM