- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试编译并运行来自 https://docs.python.org/3/extending/embedding.html#very-high-level-embedding 的示例, 但失败了。
我的环境是Ubuntu 20.04.2 LTS,系统自带python3.8(静态构建),安装了libpython3-dev
和libpython3.8-dev
包。
我尝试过的:
主.c:
#define PY_SSIZE_T_CLEAN
#include <Python.h>
int
main(int argc, char *argv[])
{
wchar_t *program = Py_DecodeLocale(argv[0], NULL);
if (program == NULL) {
fprintf(stderr, "Fatal error: cannot decode argv[0]\n");
exit(1);
}
Py_SetProgramName(program); /* optional but recommended */
Py_Initialize();
PyRun_SimpleString("from time import time,ctime\n"
"print('Today is', ctime(time()))\n");
if (Py_FinalizeEx() < 0) {
exit(120);
}
PyMem_RawFree(program);
return 0;
}
来自 https://docs.python.org/3/extending/embedding.html#compiling-and-linking-under-unix-like-systems , 获取 gcc
标志。
tian@tian-B250M-Wind:~$ python3-config --cflags
-I/usr/include/python3.8 -I/usr/include/python3.8 -Wno-unused-result -Wsign-compare -g -fdebug-prefix-map=/build/python3.8-4wuY7n/python3.8-3.8.10=. -specs=/usr/share/dpkg/no-pie-compile.specs -fstack-protector -Wformat -Werror=format-security -DNDEBUG -g -fwrapv -O3 -Wall
tian@tian-B250M-Wind:~$ python3-config --ldflags
-L/usr/lib/python3.8/config-3.8-x86_64-linux-gnu -L/usr/lib -lcrypt -lpthread -ldl -lutil -lm -lm
(我不知道为什么 python3-config
输出有一些重复的值,这不是打字错误)
gcc {copy cflags output} -o main main.c/path_to_libpython/libpython3.8.a {copy ld flags output}
:
gcc -I/usr/include/python3.8 -I/usr/include/python3.8 -Wno-unused-result -Wsign-compare -g -fdebug-prefix-map=/build/python3.8-4wuY7n/python3.8-3.8.10=. -specs=/usr/share/dpkg/no-pie-compile.specs -fstack-protector -Wformat -Werror=format-security -DNDEBUG -g -fwrapv -O3 -Wall -o main main.c /usr/lib/python3.8/config-3.8-x86_64-linux-gnu/libpython3.8.a -L/usr/lib/python3.8/config-3.8-x86_64-linux-gnu -L/usr/lib -lcrypt -lpthread -ldl -lutil -lm -lm
make test1
报错,几乎都与-fPIE
错误有关:
/usr/bin/ld: /tmp/ccdMZ2Yk.o: relocation R_X86_64_32 against `.rodata.str1.8' can not be used when making a PIE object; recompile with -fPIE
/usr/bin/ld: /usr/lib/python3.8/config-3.8-x86_64-linux-gnu/libpython3.8.a(obmalloc.o): relocation R_X86_64_32 against `.text.hot' can not be used when making a PIE object; recompile with -fPIE
/usr/bin/ld: /usr/lib/python3.8/config-3.8-x86_64-linux-gnu/libpython3.8.a(initconfig.o): relocation R_X86_64_32 against symbol `_PyRuntime' can not be used when making a PIE object; recompile with -fPIE
/usr/bin/ld: /usr/lib/python3.8/config-3.8-x86_64-linux-gnu/libpython3.8.a(pathconfig.o): relocation R_X86_64_32 against symbol `_Py_path_config' can not be used when making a PIE object; recompile with -fPIE
/usr/bin/ld: /usr/lib/python3.8/config-3.8-x86_64-linux-gnu/libpython3.8.a(preconfig.o): relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a PIE object; recompile with -fPIE
/usr/bin/ld: /usr/lib/python3.8/config-3.8-x86_64-linux-gnu/libpython3.8.a(pylifecycle.o): relocation R_X86_64_32 against symbol `_PyRuntime' can not be used when making a PIE object; recompile with -fPIE
/usr/bin/ld: /usr/lib/python3.8/config-3.8-x86_64-linux-gnu/libpython3.8.a(pystate.o): relocation R_X86_64_32 against `.rodata.str1.8' can not be used when making a PIE object; recompile with -fPIE
/usr/bin/ld: /usr/lib/python3.8/config-3.8-x86_64-linux-gnu/libpython3.8.a(pythonrun.o): relocation R_X86_64_32 against symbol `_PyParser_Grammar' can not be used when making a PIE object; recompile with -fPIE
/usr/bin/ld: /usr/lib/python3.8/config-3.8-x86_64-linux-gnu/libpython3.8.a(pytime.o): relocation R_X86_64_32S against symbol `PyFloat_Type' can not be used when making a PIE object; recompile with -fPIE
.....
我看到有一个 /usr/lib/python3.8/config-3.8-x86_64-linux-gnu/libpython3.8-pic.a
并尝试链接它但也失败了( log ).
后来我尝试了一个带有 share build python 的 Docker 镜像(python:3.10.5-bullseye
)并成功了。
root@tian-B250M-Wind:/# python3-config --cflags
-I/usr/local/include/python3.10 -I/usr/local/include/python3.10 -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall
root@tian-B250M-Wind:/# python3-config --ldflags
-L/usr/local/lib -lcrypt -lpthread -ldl -lutil -lm -lm
gcc -I/usr/local/include/python3.10 -I/usr/local/include/python3.10 -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -o main main.c -L/usr/local/lib -lcrypt -lpthread -ldl -lutil -lm -lm -lpython3.10
(我在最后加了-lpython3.10
)
它编译并 ./main
:
root@tian-B250M-Wind:/# ./main
Today is Thu Jul 14 10:39:20 2022
我编译的ubuntu系统自带的python3.8有什么问题吗?
无论如何,我只是想验证我可以链接静态 python 库 libpythonx.y.a
。因此,如果有人可以在新安装的机器上或使用非系统附带的 python(例如,自建静态 python)来实现这一点,我想尝试一下。
最佳答案
打印 gcc -v
的输出。您将在“Configured With”中找到标志 --enable-default-pie
。 PIE(位置独立可执行文件)默认启用。
您包含的预编译对象不是在启用 PIE 的情况下构建的(python 3.8),因此如果您选择在系统上使用已安装的 Python,则必须禁用 PIE 才能编译您的代码。
您在 Docker 中没有遇到此问题的原因是您正在构建不同版本的预编译 python 3.10,它确实启用了 PIE。
要么,在 Makefile 中的 LDFlags
中添加标志 -no-pie
;
或者,在编译期间将 -no-pie
传递给 gcc
。
在启用 PIE 标志的系统上编译所需的 Python,然后使用通过 PIE 编译的新 Python 编译代码。
https://docs.python.org/3/extending/embedding.html#very-high-level-embedding https://www.redhat.com/en/blog/position-independent-executables-pie https://linuxtut.com/en/4fc41123ed41cf443a6b/
关于python - 编译官方文档 "Embedding Python in Another Application"示例失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72979290/
今天上午据小米穿戴官方消息,为了保障更好的使用体验和更充足的备货,小米手表尊享版将延期至2020年2月开售。 据了解,早在今年11月初小米正式发布小米手表,共有标准版和尊享版两个版本,价格分别为1
我的意思是,如果我可以运行例如官方 docker 镜像 DEBIAN 并在此之上 运行具有相同支持架构的官方 docker 镜像 NGINX,例如Linux x86-64? 它会像我以非 docker
在 .NET 3.5 中,大量扩展方法被添加到核心代码库中。我注意到在 MSDN 中,IEnumerable<> 等添加了有关扩展方法的部分。 是否有已添加的所有扩展方法的列表以供引用? 编辑感谢您的
在vertx指南中,链接异步调用的顺序组合如下所示: FileSystem fs = vertx.fileSystem(); Future startFuture = Future.future();
neo4j 文档有 this page about creating unit tests for neo4j. 本质上它建议我们创建一个模拟数据库使用 graphDb = new TestGraph
我是 C++ 的新手,我想了解链接器是如何正式工作的。我正在使用 Linux Mint 16。是否存在 ld 链接器官方规范? 最佳答案 您可能会对链接器和共享库有更多了解 here还有here是 l
已关闭。此问题旨在寻求有关书籍、工具、软件库等的建议。不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以
我想创建一个自定义 Android 汽车应用程序,而不会分散驾驶员的注意力。我浏览了很多博客和网站,发现自 3.0 版本以来曾经有一些自定义应用程序被屏蔽。这些自定义应用程序的开发人员使用的是非官方的
我刚找到 this Project就像。嘿。为什么不尝试使用它呢? 所以。刚刚下载它(我没有做任何更改!),打开 Android Studio,运行它,然后遇到以下问题。 error: cannot
Docker 的官方 Postgres 镜像使用两个环境变量:POSTGRES_USER 和 POSTGRES_PASSWORD 参数化数据库创建: https://registry.hub.dock
我刚刚从 Python download page 下载了 Python 2.7.3 64 位安装程序我想验证签名。所以我需要导入 key ,我的首选方法是从受信任的 key 服务器获取它们。 Pyt
当我安装 Andoird 5.0 SDK 并导入 Camera2Basic 示例源代码时。CameraDevice.StateListener 和 CameraCaptureSession.State
在Design Philosophy节official Fragment guide ,它说: You should design each fragment as a modular and reu
我正在使用 Roslyn 编写一个应用程序来从语法和语义上分析 C# 源代码。对于正在分析的源代码中定义的每种类型,我想存储它是引用类型(类)、值类型(结构)还是接口(interface)。 类型的类
有没有官方的 XSLT 验证器,比如 http://validator.w3.org/用于 HTML 和 XHTML? 我知道http://validator.w3.org/也可用于验证 XML 和
我主要使用 C++ 进行科学计算,最近我一直将自己限制在一个非常类似于 C 的 C++ 特性子集;即,除了复杂和 STL 之外没有类/继承,模板仅用于查找/替换各种替换,以及其他一些我无法用语言表达的
我了解到Google会根据访问它的浏览器/设备来自动提供 TTF , EOT , WOFF 或 SVG 字体文件。 现在,我计划从服务器本身托管和提供字体文件,为此,我首先必须下载Web字体的所有文件
他们有什么不同? 例如,与官方 SDK 相比,选择 libfreenect 或 OpenNI+SensorKinect 有什么优势,反之亦然? 有什么缺点? 最佳答案 请注意,以下答案是按日期计算的,
我想使用官方的 Hashicorp 镜像来运行 Terraform 而不是手动安装。我已经关注了 dockerhub 上的文档,但没有关于如何在何处安装卷的任何详细信息 - 例如在当前目录中使用“ma
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 5 年前。
我是一名优秀的程序员,十分优秀!