我使用来自helloworld.py文件的cython生成了c代码:
print("Hello World")
使用:
cython --embed helloworld.py
然后我尝试使用GCC 7.3.0在Ubuntu 18.04上使用Python 3.6编译它,并使用以下命令安装python-dev,python3-dev软件包:
gcc `/usr/bin/python3-config --cflags --ldflags` helloworld.c -o helloworld
/usr/bin/python3-config --cflags --ldflags
返回如下:
-I/usr/include/python3.6m -I/usr/include/python3.6m -Wno-unused-result -Wsign-compare -g -fdebug-prefix-map=/build/python3.6-EKG1lX/python3.6-3.6.5=. -specs=/usr/share/dpkg/no-pie-compile.specs -fstack-protector-strong -Wformat -Werror=format-security -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -L/usr/lib/python3.6/config-3.6m-x86_64-linux-gnu -L/usr/lib -lpython3.6m -lpthread -ldl -lutil -lm -Xlinker -export-dynamic -Wl,-O1 -Wl,-Bsymbolic-functions
并且整个命令以此结束:
/usr/bin/x86_64-linux-gnu-ld: /tmp/ccuXRvp5.o: relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a PIE object; recompile with -fPIC
/usr/bin/x86_64-linux-gnu-ld: final link failed: Nonrepresentable section on output
collect2: error: ld returned 1 exit status
有没有人知道如何解决这个问题?
PS:我试图跑:gcc `/usr/bin/python3-config --cflags --ldflags` -fPIC helloworld.c -o helloworld
但最终结果如下:
/tmp/ccLRGnah.o: In function `__Pyx_check_binary_version':
/home/kuba/Desktop/python-learn/helloworld.c:2263: undefined reference to `PyOS_snprintf'
/home/kuba/Desktop/python-learn/helloworld.c:2264: undefined reference to `Py_GetVersion'
/home/kuba/Desktop/python-learn/helloworld.c:2264: undefined reference to `PyOS_snprintf'
/home/kuba/Desktop/python-learn/helloworld.c:2267: undefined reference to `PyOS_snprintf'
/home/kuba/Desktop/python-learn/helloworld.c:2271: undefined reference to `PyErr_WarnEx'
/tmp/ccLRGnah.o: In function `__Pyx_AddTraceback':
/home/kuba/Desktop/python-learn/helloworld.c:1393: undefined reference to `_PyThreadState_UncheckedGet'
/home/kuba/Desktop/python-learn/helloworld.c:1404: undefined reference to `PyFrame_New'
/home/kuba/Desktop/python-learn/helloworld.c:1412: undefined reference to `PyTraceBack_Here'
/tmp/ccLRGnah.o: In function `PyInit_helloworld':
/home/kuba/Desktop/python-learn/helloworld.c:1061: undefined reference to `PyTuple_New'
/home/kuba/Desktop/python-learn/helloworld.c:1062: undefined reference to `PyBytes_FromStringAndSize'
/home/kuba/Desktop/python-learn/helloworld.c:1063: undefined reference to `PyUnicode_FromStringAndSize'
/home/kuba/Desktop/python-learn/helloworld.c:1097: undefined reference to `PyModule_Create2'
/home/kuba/Desktop/python-learn/helloworld.c:1101: undefined reference to `PyModule_GetDict'
/home/kuba/Desktop/python-learn/helloworld.c:1103: undefined reference to `PyImport_AddModule'
/home/kuba/Desktop/python-learn/helloworld.c:1104: undefined reference to `PyImport_AddModule'
/home/kuba/Desktop/python-learn/helloworld.c:1108: undefined reference to `PyObject_SetAttrString'
/tmp/ccLRGnah.o: In function `__Pyx_InitStrings':
/home/kuba/Desktop/python-learn/helloworld.c:2292: undefined reference to `PyUnicode_Decode'
/home/kuba/Desktop/python-learn/helloworld.c:2302: undefined reference to `PyObject_Hash'
/tmp/ccLRGnah.o: In function `PyInit_helloworld':
/home/kuba/Desktop/python-learn/helloworld.c:1115: undefined reference to `PyObject_SetAttrString'
/home/kuba/Desktop/python-learn/helloworld.c:1119: undefined reference to `PyImport_GetModuleDict'
/home/kuba/Desktop/python-learn/helloworld.c:1120: undefined reference to `PyDict_GetItemString'
/tmp/ccLRGnah.o: In function `__Pyx_PrintOne':
/home/kuba/Desktop/python-learn/helloworld.c:1748: undefined reference to `PyTuple_Pack'
/tmp/ccLRGnah.o: In function `__Pyx_Print':
/home/kuba/Desktop/python-learn/helloworld.c:1706: undefined reference to `PyObject_Call'
/tmp/ccLRGnah.o: In function `PyInit_helloworld':
/home/kuba/Desktop/python-learn/helloworld.c:1145: undefined reference to `PyDict_New'
/home/kuba/Desktop/python-learn/helloworld.c:1147: undefined reference to `PyDict_SetItem'
/tmp/ccLRGnah.o: In function `__Pyx_CreateCodeObjectForTraceback':
/home/kuba/Desktop/python-learn/helloworld.c:1346: undefined reference to `PyUnicode_FromString'
/home/kuba/Desktop/python-learn/helloworld.c:1360: undefined reference to `PyUnicode_FromString'
/home/kuba/Desktop/python-learn/helloworld.c:1364: undefined reference to `PyCode_New'
/tmp/ccLRGnah.o: In function `__Pyx_InitStrings':
/home/kuba/Desktop/python-learn/helloworld.c:2297: undefined reference to `PyBytes_FromStringAndSize'
/tmp/ccLRGnah.o: In function `PyInit_helloworld':
/home/kuba/Desktop/python-learn/helloworld.c:1160: undefined reference to `PyErr_Occurred'
/tmp/ccLRGnah.o: In function `__Pyx_InitStrings':
/home/kuba/Desktop/python-learn/helloworld.c:2290: undefined reference to `PyUnicode_InternFromString'
/home/kuba/Desktop/python-learn/helloworld.c:2303: undefined reference to `PyErr_Clear'
/home/kuba/Desktop/python-learn/helloworld.c:2294: undefined reference to `PyUnicode_FromStringAndSize'
/tmp/ccLRGnah.o: In function `PyInit_helloworld':
/home/kuba/Desktop/python-learn/helloworld.c:1161: undefined reference to `PyExc_ImportError'
/home/kuba/Desktop/python-learn/helloworld.c:1161: undefined reference to `PyErr_SetString'
/tmp/ccLRGnah.o: In function `__pyx_insert_code_object':
/home/kuba/Desktop/python-learn/helloworld.c:1316: undefined reference to `PyMem_Realloc'
/tmp/ccLRGnah.o: In function `PyInit_helloworld':
/home/kuba/Desktop/python-learn/helloworld.c:1121: undefined reference to `PyDict_SetItemString'
/tmp/ccLRGnah.o: In function `__pyx_insert_code_object':
/home/kuba/Desktop/python-learn/helloworld.c:1296: undefined reference to `PyMem_Malloc'
/tmp/ccLRGnah.o: In function `__Pyx_Print':
/home/kuba/Desktop/python-learn/helloworld.c:1670: undefined reference to `PyObject_GetAttr'
/tmp/ccLRGnah.o: In function `__Pyx_main':
/home/kuba/Desktop/python-learn/helloworld.c:1441: undefined reference to `Py_Initialize'
/home/kuba/Desktop/python-learn/helloworld.c:1464: undefined reference to `PyErr_Occurred'
/home/kuba/Desktop/python-learn/helloworld.c:1473: undefined reference to `Py_Finalize'
/home/kuba/Desktop/python-learn/helloworld.c:1440: undefined reference to `Py_SetProgramName'
/home/kuba/Desktop/python-learn/helloworld.c:1441: undefined reference to `Py_Initialize'
/home/kuba/Desktop/python-learn/helloworld.c:1443: undefined reference to `PySys_SetArgv'
/home/kuba/Desktop/python-learn/helloworld.c:1465: undefined reference to `PyErr_Print'
collect2: error: ld returned 1 exit status