undefined reference to 'Py_Initialize', c++调用python, ndk编译问题

时间:2022-09-29 11:34:24
c++中调用python, c++编译调用都没问题.
ndk下用g++编译出现编译错误

是在win32平台编译, 用eclipse + cygwin + ndk 编译.
我怀疑是android.mk写得不对, 请指教...谢谢

12 个解决方案

#1


没找到还是没加python库,最好贴上编译参数命令和错误信息,不要光说有问题...

#2


恩,信息太少。

#3


undefined reference to 'Py_Initialize', c++调用python, ndk编译问题undefined reference to 'Py_Initialize', c++调用python, ndk编译问题路过

#4


不好意思
我的android.mk文件

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE := cocos2dcpp_shared

LOCAL_MODULE_FILENAME := libcocos2dcpp

LOCAL_SRC_FILES := hellocpp/main.cpp \

MY_CPP_LIST := $(wildcard $(LOCAL_PATH)/../../Classes/*.cpp)
LOCAL_SRC_FILES += $(MY_CPP_LIST:$(LOCAL_PATH)/%=%)

LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../Classes
LOCAL_C_INCLUDES += $(LOCAL_PATH)/../../Classes/include/vc
LOCAL_C_INCLUDES += /usr/include/python2.7

LOCAL_LDLIBS := -L$(SYSROOT)/usr/bin -lpython2.7

LOCAL_WHOLE_STATIC_LIBRARIES += cocos2dx_static
LOCAL_WHOLE_STATIC_LIBRARIES += cocosdenshion_static
LOCAL_WHOLE_STATIC_LIBRARIES += box2d_static
LOCAL_WHOLE_STATIC_LIBRARIES += chipmunk_static
LOCAL_WHOLE_STATIC_LIBRARIES += cocos_extension_static

include $(BUILD_SHARED_LIBRARY)

$(call import-module,cocos2dx)
$(call import-module,cocos2dx/platform/third_party/android/prebuilt/libcurl)
$(call import-module,CocosDenshion/android)
$(call import-module,extensions)
$(call import-module,external/Box2D)
$(call import-module,external/chipmunk)



eclipse编译信息


Using prebuilt externals
make: Entering directory '/cygdrive/c/cocos2d-x-3.0alpha0-pre/cocos2d-x-3.0alpha0-pre/projects/CityPlusPlus/proj.android'
Android NDK: WARNING:jni/Android.mk:cocos2dcpp_shared: non-system libraries in linker flags: -lpython2.7    
Android NDK:     This is likely to result in incorrect builds. Try using LOCAL_STATIC_LIBRARIES    
Android NDK:     or LOCAL_SHARED_LIBRARIES instead to list the library dependencies of the    
Android NDK:     current module    
Android NDK: WARNING:/cygdrive/c/cocos2d-x-3.0alpha0-pre/cocos2d-x-3.0alpha0-pre/projects/CityPlusPlus/proj.android/../../../cocos2dx/Android.mk:cocos2dx_static: LOCAL_LDLIBS is always ignored for static libraries    
Compile++ thumb  : cocos2dcpp_shared <= main.cpp
Compile++ thumb  : cocos2dcpp_shared <= CFrameWorld.cpp
Compile++ thumb  : cocos2dcpp_shared <= CServerAssist.cpp
In file included from jni/../../Classes/include/vc/sys/config.h:5:0,
                 from jni/../../Classes/include/vc/_ansi.h:16,
                 from jni/../../Classes/include/vc/ieeefp.h:4,
                 from C:/cygwin/usr/include/python2.7/pyport.h:309,
                 from C:/cygwin/usr/include/python2.7/Python.h:58,
                 from jni/../../Classes/CServerAssist.cpp:4:
jni/../../Classes/include/vc/sys/features.h:38:0: warning: "__GNUC_PREREQ__" redefined [enabled by default]
 #define __GNUC_PREREQ__(ma, mi) __GNUC_PREREQ(ma, mi)
 ^
In file included from C:/android-ndk-r9/platforms/android-9/arch-arm/usr/include/limits.h:38:0,
                 from C:/android-ndk-r9/platforms/android-9/arch-arm/usr/include/wchar.h:31,
                 from C:/android-ndk-r9/sources/cxx-stl/gnu-libstdc++/4.8/include/cwchar:44,
                 from C:/android-ndk-r9/sources/cxx-stl/gnu-libstdc++/4.8/include/bits/postypes.h:40,
                 from C:/android-ndk-r9/sources/cxx-stl/gnu-libstdc++/4.8/include/bits/char_traits.h:40,
                 from C:/android-ndk-r9/sources/cxx-stl/gnu-libstdc++/4.8/include/string:40,
                 from jni/../../Classes/CServerAssist.h:15,
                 from jni/../../Classes/CServerAssist.cpp:1:
C:/android-ndk-r9/platforms/android-9/arch-arm/usr/include/sys/cdefs.h:52:0: note: this is the location of the previous definition
 #define __GNUC_PREREQ__(x, y)      \
 ^
Compile++ thumb  : cocos2dcpp_shared <= CAccount.cpp
Compile++ thumb  : cocos2dcpp_shared <= CJniAssist.cpp
Compile++ thumb  : cocos2dcpp_shared <= CValidateAssist.cpp
Compile++ thumb  : cocos2dcpp_shared <= CLoading.cpp
Compile++ thumb  : cocos2dcpp_shared <= CFrameOthers.cpp
Compile++ thumb  : cocos2dcpp_shared <= CSizeAssist.cpp
Compile++ thumb  : cocos2dcpp_shared <= CLogin.cpp
Compile++ thumb  : cocos2dcpp_shared <= CToastAssist.cpp
Compile++ thumb  : cocos2dcpp_shared <= CControlButtonEx.cpp
Compile++ thumb  : cocos2dcpp_shared <= CImgAssist.cpp
Compile++ thumb  : cocos2dcpp_shared <= CFrameConstruction.cpp
Compile++ thumb  : cocos2dcpp_shared <= AppDelegate.cpp
Compile++ thumb  : cocos2dcpp_shared <= CEditBoxEx.cpp
Compile++ thumb  : cocos2dcpp_shared <= VisibleRect.cpp
Compile++ thumb  : cocos2dcpp_shared <= CComLogo.cpp
Compile++ thumb  : cocos2dcpp_shared <= CFontAssist.cpp
Compile++ thumb  : cocos2dcpp_shared <= CLayerForLoginAndRegister.cpp
Compile++ thumb  : cocos2dcpp_shared <= CFrameCurPos.cpp
Compile++ thumb  : cocos2dcpp_shared <= CStartAnimation.cpp
Compile++ thumb  : cocos2dcpp_shared <= CPosSettingAssist.cpp
Compile++ thumb  : cocos2dcpp_shared <= CToastAndroid.cpp
Compile++ thumb  : cocos2dcpp_shared <= CTextAssist.cpp
Compile++ thumb  : cocos2dcpp_shared <= CRegister.cpp
Compile++ thumb  : cocos2dcpp_shared <= CMainFrame.cpp
Compile++ thumb  : cocos2dcpp_shared <= CBackgroundLayer.cpp
Compile++ thumb  : cocos2dcpp_shared <= CToast.cpp
SharedLibrary  : libcocos2dcpp.so
C:/android-ndk-r9/toolchains/arm-linux-androideabi-4.8/prebuilt/windows/bin/../lib/gcc/arm-linux-androideabi/4.8/../../../../arm-linux-androideabi/bin/ld.exe: error: cannot find -lpython2.7
jni/../../Classes/CServerAssist.cpp:25: error: undefined reference to 'Py_Initialize'
jni/../../Classes/CServerAssist.cpp:28: error: undefined reference to 'PyRun_SimpleStringFlags'
jni/../../Classes/CServerAssist.cpp:29: error: undefined reference to 'PyRun_SimpleStringFlags'
jni/../../Classes/CServerAssist.cpp:34: error: undefined reference to 'PyImport_ImportModule'
jni/../../Classes/CServerAssist.cpp:35: error: undefined reference to 'PyObject_GetAttrString'
jni/../../Classes/CServerAssist.cpp:37: error: undefined reference to 'PyTuple_New'
jni/../../Classes/CServerAssist.cpp:38: error: undefined reference to 'Py_BuildValue'
jni/../../Classes/CServerAssist.cpp:38: error: undefined reference to 'PyTuple_SetItem'
jni/../../Classes/CServerAssist.cpp:39: error: undefined reference to 'Py_BuildValue'
jni/../../Classes/CServerAssist.cpp:39: error: undefined reference to 'PyTuple_SetItem'
jni/../../Classes/CServerAssist.cpp:40: error: undefined reference to 'Py_BuildValue'
jni/../../Classes/CServerAssist.cpp:40: error: undefined reference to 'PyTuple_SetItem'
jni/../../Classes/CServerAssist.cpp:43: error: undefined reference to 'PyEval_CallObjectWithKeywords'
jni/../../Classes/CServerAssist.cpp:45: error: undefined reference to 'Py_Finalize'
collect2.exe: error: ld returned 1 exit status
/cygdrive/c/android-ndk-r9/build/core/build-binary.mk:533: recipe for target 'obj/local/armeabi/libcocos2dcpp.so' failed
make: Leaving directory '/cygdrive/c/cocos2d-x-3.0alpha0-pre/cocos2d-x-3.0alpha0-pre/projects/CityPlusPlus/proj.android'
make: *** [obj/local/armeabi/libcocos2dcpp.so] Error 1

#5


编译错误信息里有一个warning和一个错误提示

Android NDK: WARNING:jni/Android.mk:cocos2dcpp_shared: non-system libraries in linker flags: -lpython2.7  

C:/android-ndk-r9/toolchains/arm-linux-androideabi-4.8/prebuilt/windows/bin/../lib/gcc/arm-linux-androideabi/4.8/../../../../arm-linux-androideabi/bin/ld.exe: error: cannot find -lpython2.7

#6


我在cygwin/bin目录找到一个 “libpython2.7.dll”文件,cygwin/lib下有个"libpython2.7.dll.a"文件,但是没有so文件..

#7


不能链接不同平台的库,得去找android平台的...

#8


引用 7 楼 angel_su 的回复:
不能链接不同平台的库,得去找android平台的...

去ndk/platforms/去找?

#9


引用 7 楼 angel_su 的回复:
不能链接不同平台的库,得去找android平台的...

是去网上找libpython2.7.so文件吗?
还是cygwin里面有呢? 
对这个ndk编译不太懂, 大神请见谅..

#10


so只是unix这类系统对库的泛称,不同平台下的so也不能看到同名就随便拿来用,网上搜搜没有的话用ndk编译python源码...

#11


我试了一下把 cygwin\lib\python2.7\config\libpython2.7.dll.a 修改成 "libpython2.7.so" 
eclipse编译一样的提示..
error: cannot find -lpython2.7

#12



LOCAL_LDLIBS := -L$(SYSROOT)/lib/python2.7/config -lpython2.7

#1


没找到还是没加python库,最好贴上编译参数命令和错误信息,不要光说有问题...

#2


恩,信息太少。

#3


undefined reference to 'Py_Initialize', c++调用python, ndk编译问题undefined reference to 'Py_Initialize', c++调用python, ndk编译问题路过

#4


不好意思
我的android.mk文件

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE := cocos2dcpp_shared

LOCAL_MODULE_FILENAME := libcocos2dcpp

LOCAL_SRC_FILES := hellocpp/main.cpp \

MY_CPP_LIST := $(wildcard $(LOCAL_PATH)/../../Classes/*.cpp)
LOCAL_SRC_FILES += $(MY_CPP_LIST:$(LOCAL_PATH)/%=%)

LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../Classes
LOCAL_C_INCLUDES += $(LOCAL_PATH)/../../Classes/include/vc
LOCAL_C_INCLUDES += /usr/include/python2.7

LOCAL_LDLIBS := -L$(SYSROOT)/usr/bin -lpython2.7

LOCAL_WHOLE_STATIC_LIBRARIES += cocos2dx_static
LOCAL_WHOLE_STATIC_LIBRARIES += cocosdenshion_static
LOCAL_WHOLE_STATIC_LIBRARIES += box2d_static
LOCAL_WHOLE_STATIC_LIBRARIES += chipmunk_static
LOCAL_WHOLE_STATIC_LIBRARIES += cocos_extension_static

include $(BUILD_SHARED_LIBRARY)

$(call import-module,cocos2dx)
$(call import-module,cocos2dx/platform/third_party/android/prebuilt/libcurl)
$(call import-module,CocosDenshion/android)
$(call import-module,extensions)
$(call import-module,external/Box2D)
$(call import-module,external/chipmunk)



eclipse编译信息


Using prebuilt externals
make: Entering directory '/cygdrive/c/cocos2d-x-3.0alpha0-pre/cocos2d-x-3.0alpha0-pre/projects/CityPlusPlus/proj.android'
Android NDK: WARNING:jni/Android.mk:cocos2dcpp_shared: non-system libraries in linker flags: -lpython2.7    
Android NDK:     This is likely to result in incorrect builds. Try using LOCAL_STATIC_LIBRARIES    
Android NDK:     or LOCAL_SHARED_LIBRARIES instead to list the library dependencies of the    
Android NDK:     current module    
Android NDK: WARNING:/cygdrive/c/cocos2d-x-3.0alpha0-pre/cocos2d-x-3.0alpha0-pre/projects/CityPlusPlus/proj.android/../../../cocos2dx/Android.mk:cocos2dx_static: LOCAL_LDLIBS is always ignored for static libraries    
Compile++ thumb  : cocos2dcpp_shared <= main.cpp
Compile++ thumb  : cocos2dcpp_shared <= CFrameWorld.cpp
Compile++ thumb  : cocos2dcpp_shared <= CServerAssist.cpp
In file included from jni/../../Classes/include/vc/sys/config.h:5:0,
                 from jni/../../Classes/include/vc/_ansi.h:16,
                 from jni/../../Classes/include/vc/ieeefp.h:4,
                 from C:/cygwin/usr/include/python2.7/pyport.h:309,
                 from C:/cygwin/usr/include/python2.7/Python.h:58,
                 from jni/../../Classes/CServerAssist.cpp:4:
jni/../../Classes/include/vc/sys/features.h:38:0: warning: "__GNUC_PREREQ__" redefined [enabled by default]
 #define __GNUC_PREREQ__(ma, mi) __GNUC_PREREQ(ma, mi)
 ^
In file included from C:/android-ndk-r9/platforms/android-9/arch-arm/usr/include/limits.h:38:0,
                 from C:/android-ndk-r9/platforms/android-9/arch-arm/usr/include/wchar.h:31,
                 from C:/android-ndk-r9/sources/cxx-stl/gnu-libstdc++/4.8/include/cwchar:44,
                 from C:/android-ndk-r9/sources/cxx-stl/gnu-libstdc++/4.8/include/bits/postypes.h:40,
                 from C:/android-ndk-r9/sources/cxx-stl/gnu-libstdc++/4.8/include/bits/char_traits.h:40,
                 from C:/android-ndk-r9/sources/cxx-stl/gnu-libstdc++/4.8/include/string:40,
                 from jni/../../Classes/CServerAssist.h:15,
                 from jni/../../Classes/CServerAssist.cpp:1:
C:/android-ndk-r9/platforms/android-9/arch-arm/usr/include/sys/cdefs.h:52:0: note: this is the location of the previous definition
 #define __GNUC_PREREQ__(x, y)      \
 ^
Compile++ thumb  : cocos2dcpp_shared <= CAccount.cpp
Compile++ thumb  : cocos2dcpp_shared <= CJniAssist.cpp
Compile++ thumb  : cocos2dcpp_shared <= CValidateAssist.cpp
Compile++ thumb  : cocos2dcpp_shared <= CLoading.cpp
Compile++ thumb  : cocos2dcpp_shared <= CFrameOthers.cpp
Compile++ thumb  : cocos2dcpp_shared <= CSizeAssist.cpp
Compile++ thumb  : cocos2dcpp_shared <= CLogin.cpp
Compile++ thumb  : cocos2dcpp_shared <= CToastAssist.cpp
Compile++ thumb  : cocos2dcpp_shared <= CControlButtonEx.cpp
Compile++ thumb  : cocos2dcpp_shared <= CImgAssist.cpp
Compile++ thumb  : cocos2dcpp_shared <= CFrameConstruction.cpp
Compile++ thumb  : cocos2dcpp_shared <= AppDelegate.cpp
Compile++ thumb  : cocos2dcpp_shared <= CEditBoxEx.cpp
Compile++ thumb  : cocos2dcpp_shared <= VisibleRect.cpp
Compile++ thumb  : cocos2dcpp_shared <= CComLogo.cpp
Compile++ thumb  : cocos2dcpp_shared <= CFontAssist.cpp
Compile++ thumb  : cocos2dcpp_shared <= CLayerForLoginAndRegister.cpp
Compile++ thumb  : cocos2dcpp_shared <= CFrameCurPos.cpp
Compile++ thumb  : cocos2dcpp_shared <= CStartAnimation.cpp
Compile++ thumb  : cocos2dcpp_shared <= CPosSettingAssist.cpp
Compile++ thumb  : cocos2dcpp_shared <= CToastAndroid.cpp
Compile++ thumb  : cocos2dcpp_shared <= CTextAssist.cpp
Compile++ thumb  : cocos2dcpp_shared <= CRegister.cpp
Compile++ thumb  : cocos2dcpp_shared <= CMainFrame.cpp
Compile++ thumb  : cocos2dcpp_shared <= CBackgroundLayer.cpp
Compile++ thumb  : cocos2dcpp_shared <= CToast.cpp
SharedLibrary  : libcocos2dcpp.so
C:/android-ndk-r9/toolchains/arm-linux-androideabi-4.8/prebuilt/windows/bin/../lib/gcc/arm-linux-androideabi/4.8/../../../../arm-linux-androideabi/bin/ld.exe: error: cannot find -lpython2.7
jni/../../Classes/CServerAssist.cpp:25: error: undefined reference to 'Py_Initialize'
jni/../../Classes/CServerAssist.cpp:28: error: undefined reference to 'PyRun_SimpleStringFlags'
jni/../../Classes/CServerAssist.cpp:29: error: undefined reference to 'PyRun_SimpleStringFlags'
jni/../../Classes/CServerAssist.cpp:34: error: undefined reference to 'PyImport_ImportModule'
jni/../../Classes/CServerAssist.cpp:35: error: undefined reference to 'PyObject_GetAttrString'
jni/../../Classes/CServerAssist.cpp:37: error: undefined reference to 'PyTuple_New'
jni/../../Classes/CServerAssist.cpp:38: error: undefined reference to 'Py_BuildValue'
jni/../../Classes/CServerAssist.cpp:38: error: undefined reference to 'PyTuple_SetItem'
jni/../../Classes/CServerAssist.cpp:39: error: undefined reference to 'Py_BuildValue'
jni/../../Classes/CServerAssist.cpp:39: error: undefined reference to 'PyTuple_SetItem'
jni/../../Classes/CServerAssist.cpp:40: error: undefined reference to 'Py_BuildValue'
jni/../../Classes/CServerAssist.cpp:40: error: undefined reference to 'PyTuple_SetItem'
jni/../../Classes/CServerAssist.cpp:43: error: undefined reference to 'PyEval_CallObjectWithKeywords'
jni/../../Classes/CServerAssist.cpp:45: error: undefined reference to 'Py_Finalize'
collect2.exe: error: ld returned 1 exit status
/cygdrive/c/android-ndk-r9/build/core/build-binary.mk:533: recipe for target 'obj/local/armeabi/libcocos2dcpp.so' failed
make: Leaving directory '/cygdrive/c/cocos2d-x-3.0alpha0-pre/cocos2d-x-3.0alpha0-pre/projects/CityPlusPlus/proj.android'
make: *** [obj/local/armeabi/libcocos2dcpp.so] Error 1

#5


编译错误信息里有一个warning和一个错误提示

Android NDK: WARNING:jni/Android.mk:cocos2dcpp_shared: non-system libraries in linker flags: -lpython2.7  

C:/android-ndk-r9/toolchains/arm-linux-androideabi-4.8/prebuilt/windows/bin/../lib/gcc/arm-linux-androideabi/4.8/../../../../arm-linux-androideabi/bin/ld.exe: error: cannot find -lpython2.7

#6


我在cygwin/bin目录找到一个 “libpython2.7.dll”文件,cygwin/lib下有个"libpython2.7.dll.a"文件,但是没有so文件..

#7


不能链接不同平台的库,得去找android平台的...

#8


引用 7 楼 angel_su 的回复:
不能链接不同平台的库,得去找android平台的...

去ndk/platforms/去找?

#9


引用 7 楼 angel_su 的回复:
不能链接不同平台的库,得去找android平台的...

是去网上找libpython2.7.so文件吗?
还是cygwin里面有呢? 
对这个ndk编译不太懂, 大神请见谅..

#10


so只是unix这类系统对库的泛称,不同平台下的so也不能看到同名就随便拿来用,网上搜搜没有的话用ndk编译python源码...

#11


我试了一下把 cygwin\lib\python2.7\config\libpython2.7.dll.a 修改成 "libpython2.7.so" 
eclipse编译一样的提示..
error: cannot find -lpython2.7

#12



LOCAL_LDLIBS := -L$(SYSROOT)/lib/python2.7/config -lpython2.7