使用Python在C中

2024-04-24 16:31:19 发布

您现在位置:Python中文网/ 问答频道 /正文

对于用C编写的程序,我需要使用仅在Python中可用的功能。我找到了一些源代码来帮助我,但是one of them给了我编译代码的命令:

gcc -I/usr/include/python2.7 prog.c -lpython2.7 -o prog -Wall  && ./prog 

我得把它改成

gcc -I/usr/include/python3.4 prog.c -lpython3.4 -o prog -Wall  && ./prog

但是编译器返回:

/usr/bin/ld: cannot find -lpython3.4 collect2: error: ld returned 1 exit status 

这是我第一次使用gcc,所以我真的不知道怎么做,即使我看例子。你知道吗


Tags: of代码命令程序功能编译器源代码include
2条回答

以root用户身份运行ldconfig,以重建缓存并更新符号链接:

sudo ldconfig

ldconfig creates the necessary links and cache to the most recent shared libraries found in the directories specified on the command line, in the file /etc/ld.so.conf, and in the trusted directories (/lib and /usr/lib). The cache is used by the run-time linker, ld.so or ld-linux.so. ldconfig checks the header and filenames of the libraries it encounters when determining which versions should have their links updated.

[...]

资料来源:^{}

如果不起作用,请使用-p选项并查找Python库的名称:

ldconfig -p | grep python

除非你有特别的原因,否则使用文件名最小的。你知道吗

-p

Print the lists of directories and candidate libraries stored in the current cache.

你试过用Cython连接C和Python吗?你知道吗

手册可以在这里找到:https://python.g-node.org/python-summerschool-2011/_media/materials/cython/cython-slides.pdf

在第4.10节中,它简要介绍了python和C的接口,我的想法是您可以从Cython调用您的C代码,这样您就可以完全直接地访问您想要使用的python特性。你知道吗

下面是有关与外部C代码接口的更多信息:

http://cython-docs2.readthedocs.io/en/latest/src/userguide/external_C_code.html

相关问题 更多 >