Python OpenSSL C扩展:未定义符号:AES\u set\u encrypt\u key
我正在尝试为Python编写OpenSSL C扩展。已生成共享库*.so文件,但在导入模块时遇到未定义的符号错误。它抛出以下错误未定义符号:AES\u set\u encrypt\u key: openssl_python.cPython OpenSSL C扩展:未定义符号:AES\u set\u encrypt\u key,python,c,cpython,Python,C,Cpython,我正在尝试为Python编写OpenSSL C扩展。已生成共享库*.so文件,但在导入模块时遇到未定义的符号错误。它抛出以下错误未定义符号:AES\u set\u encrypt\u key: openssl_python.c 这里的基本问题是,当setup.py链接扩展名时,它会将-lcrypto放在包含代码的对象文件之前的命令行上。Unix链接器在命令行上严格地从左到右处理对象和库:-lcrypto foo.o不会使用libcrypto解析foo.o中的符号。这是出于历史原因,不再有太多意义
这里的基本问题是,当setup.py链接扩展名时,它会将-lcrypto放在包含代码的对象文件之前的命令行上。Unix链接器在命令行上严格地从左到右处理对象和库:-lcrypto foo.o不会使用libcrypto解析foo.o中的符号。这是出于历史原因,不再有太多意义,但我们仍然坚持使用它,因为它会破坏太多的makefile来更改它。此外,出于历史原因(不再有意义),如果不将-Wl、-z、defs放在命令行上,则Python扩展的共享库编译代码在技术上是共享库,其中包含未定义的符号,这不是链接时错误,这就是构建似乎可以工作的原因 您的扩展本质上需要libcrypto。如果我正确阅读了Distutils文档,这意味着您应该在libraries=keyword参数到Extension中指定它。。。而不是把它放在CFLAG中。像这样:
openssl_module = Extension('openssl_python',
sources = ['openssl_python.c'],
libraries = ['crypto'])
zwol的解决方案要好得多。但我只是为了完整起见才把它包括进去 发出以下命令:
x86_64-linux-gnu-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -g -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -fPIC -I/usr/include/python3.5m -c openssl_python.c -lcrypto -o build/temp.linux-x86_64-3.5/openssl_python.o
接
x86_64-linux-gnu-gcc -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions -Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-Bsymbolic-functions -Wl,-z,relro -g -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 build/temp.linux-x86_64-3.5/openssl_python.o -lcrypto -o /home/rohith/python_c_extension/aes/openssl_python.cpython-35m-x86_64-linux-gnu.so
共享库现在已生成。请执行python3 setup.py clean;CFLAGS=-Wl,-z,defs-lcrypto python3 setup.py build_ext-inplace,然后将这些命令的完整且未编辑的输出复制并粘贴到问题中。您将至少收到一条错误消息。@zwol运行了您建议的命令并编辑了问题ha。我现在知道怎么回事了。我意识到-lcrypto的位置不对,只是手动发出了编译命令:x86_64-linux-gnu-gcc-pthread-DNDEBUG-g-fwrapv-O2-Wall-Wstrict原型-g-fstack-protector-strong-Wformat-Werror=format-security-Wdate-time-D_-FORTIFY\u SOURCE=2-fPIC-I/usr/include/python3.5m-c openssl\python.c-lcrypto-o build/temp.linux-x86_64-3.5/openssl_python.o。但您的解决方案要优雅得多。谢谢
running build_ext
building 'openssl_python' extension
creating build
creating build/temp.linux-x86_64-3.5
x86_64-linux-gnu-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -Wl,-z,defs -lcrypto -Wdate-time -D_FORTIFY_SOURCE=2 -fPIC -I/usr/include/python3.5m -c openssl_python.c -o build/temp.linux-x86_64-3.5/openssl_python.o
openssl_python.c: In function ‘openssl_module_aes_encrypt’:
openssl_python.c:49:21: warning: pointer targets in passing argument 1 of ‘AES_cbc_encrypt’ differ in signedness [-Wpointer-sign]
AES_cbc_encrypt(sn,
^
In file included from openssl_python.c:4:0:
/usr/include/openssl/aes.h:107:6: note: expected ‘const unsigned char *’ but argument is of type ‘char *’
void AES_cbc_encrypt(const unsigned char *in, unsigned char *out,
^
openssl_python.c:61:12: warning: return makes pointer from integer without a cast [-Wint-conversion]
return 1;
^
openssl_python.c:20:23: warning: unused variable ‘sqlcFirmwarePadding’ [-Wunused-variable]
static unsigned char sqlcFirmwarePadding[] = {
^
x86_64-linux-gnu-gcc -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions -Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,defs -lcrypto -Wdate-time -D_FORTIFY_SOURCE=2 build/temp.linux-x86_64-3.5/openssl_python.o -o /home/rohith/python_c_extension/aes/openssl_python.cpython-35m-x86_64-linux-gnu.so
build/temp.linux-x86_64-3.5/openssl_python.o: In function `openssl_module_aes_encrypt':
/home/rohith/python_c_extension/aes/openssl_python.c:27: undefined reference to `PyArg_ParseTuple'
/home/rohith/python_c_extension/aes/openssl_python.c:37: undefined reference to `AES_set_encrypt_key'
/home/rohith/python_c_extension/aes/openssl_python.c:49: undefined reference to `AES_cbc_encrypt'
build/temp.linux-x86_64-3.5/openssl_python.o: In function `PyInit_openssl_python':
/home/rohith/python_c_extension/aes/openssl_python.c:84: undefined reference to `Py_Initialize'
/home/rohith/python_c_extension/aes/openssl_python.c:86: undefined reference to `PyModule_Create2'
collect2: error: ld returned 1 exit status
error: command 'x86_64-linux-gnu-gcc' failed with exit status 1
openssl_module = Extension('openssl_python',
sources = ['openssl_python.c'],
libraries = ['crypto'])
x86_64-linux-gnu-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -g -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -fPIC -I/usr/include/python3.5m -c openssl_python.c -lcrypto -o build/temp.linux-x86_64-3.5/openssl_python.o
x86_64-linux-gnu-gcc -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions -Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-Bsymbolic-functions -Wl,-z,relro -g -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 build/temp.linux-x86_64-3.5/openssl_python.o -lcrypto -o /home/rohith/python_c_extension/aes/openssl_python.cpython-35m-x86_64-linux-gnu.so