链接Python和C++在窗户上
我正在尝试创建 python
库,用 c ++
编写,使用 boost-python
和 mingw
编译。
C ++
代码:
char const* greet()
{
return "hello, world";
}
#include <boost/python.hpp>
BOOST_PYTHON_MODULE(hello)
{
using namespace boost::python;
def("greet", greet);
}
到目前为止,这就是我所做的:
-
python3.9
已经安装了 - 安装
mingw/g ++
- 下载
boost
zip - 运行以下两个命令:
g ++ -o中间-M64 -D MS_WIN64 -C -I PATH \ to \ BOOST_1_79_0 -I PATH \ to \ PYTHON39 \ include.cpp
输出思考):
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中间
output(以错误结束):
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
我找不到能找到 方案
解决
为了解决我的问题,我尝试了 cmake
而不是 g ++
。
我做了以下操作:
- 安装
cmake
。 - 安装
make
。 - 在我的项目中创建“构建”文件夹。
- 创建
myProject.py
脚本。 - 创建
cmakecache.txt
文件。 - 运行两个命令。
myProject.py
脚本:
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 .)
内部“构建” dir)
命令1: cmake -g“ unix makefiles” -dcmake_build_type =发行..
输出:
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
命令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
edg> eding> eding
给定的建议< a href =“ https://stackoverflow.com/questions/12573816/what-is-an-undefined-referend-referend-referend-norsolved-external-symbol-error-error-error-error-and-how-how-do-i-i-fix”> what-is-and -nundefined-reference-nosolved-external-symbol-error-eRror-and-her-houd-do-do-i-fix 不能解决我的问题,因为即使可以理解它是关于
link
在编译时,我的 c ++
脚本使用 boost-python
,这是他们不参考的重要元素,因此我找不到适应给定解决方案的方法我的问题。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您永远不会将扩展名链接到
boost-python
或python库。只需查看python_add_module
函数(),除了确保库具有正确的后缀之外,它没有做很多事情。 它应该有效如果您添加到
cmakelists.txt
文件, 。您的一些命名也有一些问题,例如myproject.py
您要导入hello hello
,但是您将扩展名为nativelib
。您可能会发现 https://iainhaslam.com/scraps/scraps/hello-boost-python/ 有用。You never linked the extension to
boost-python
, or the python libraries. Just looking at thepython_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 addto your
CMakeLists.txt
file. There's also a few issues with some of your naming, e.g inMyProject.py
you doimport hello
but you've named the extension asNativeLib
. You may find https://iainhaslam.com/scraps/hello-boost-python/ useful.