如何将静态 C 目标文件链接到 Perl?

发布于 2024-09-25 01:43:16 字数 118 浏览 2 评论 0原文

我有一个用 C 编写的函数(在 HelloWorld.c 文件中)。 我想编译它并需要创建一个静态目标文件 HelloWorld.a

最后我需要从 Perl 程序 (HelloWorld.pl) 调用它。

I have a function written in C (Say in HelloWorld.c file).
I want to compile this and need to create a staic object file HelloWorld.a

Finally I need to call this from a Perl program (HelloWorld.pl).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

芯好空 2024-10-02 01:43:16

要从 Perl 调用 C,通常会从他的 C 代码编译一个共享库,而不是静态库,然后使用 XSLoaderDynaLoader 模块将其加载到 Perl 解释器中。

为了能够从 Perl 空间调用 C 代码,有很多方法。最常见的一种是编写称为 XSUB 的东西,它具有 perl 端接口,将 perl 调用约定映射到 C 调用约定,并调用 C 函数。这些 XSUB 通常也链接到将加载到 Perl 中的共享库,并用称为 XS 的语言编写,该语言在 perlxsperlxstut

还有其他方法可以构建该包装层,例如各种 XS 代码生成器以及 SWIG。但您也可以使用 NCI 直接调用 C 函数。 Perl 也有很多这样的东西。 P5NCI 是其中一个示例,今年 Google Summer of Code 计划中开发的 ctypes 模块是另一个示例。

这里可能应该提到的另一个相关技术是 Inline::C 以及 Inline 系列的其他模块。它们允许您直接在 Perl 中编写其他语言的代码并调用它。在底层,Inline::C 只是构建 XS 代码并将其结果加载到解释器中。

To call from perl to C one usually compiles a shared, not a static, library from his c code, and then loads that into the perl interpreter using the XSLoader or DynaLoader module.

To then be able to call the C code from perl space there's many ways. The most common one is writing something called XSUBs, which have a perl-side interface, map the perl calling-conventions to C calling-conventions, and call the C functions. Those XSUBs are usually also linked into the shared library that'll be loaded into perl, and written in a language called XS, which is extensively documented in perlxs and perlxstut.

There's also other ways to build that wrapper layer, like various XS code generators, as well as SWIG. But you could also call to the C functions directly using an NCI. Perl also has many of those. The P5NCI is one example of those, the ctypes module developed in this year's Google Summer of Code program is another.

Another related technique that should probably be mentioned here is Inline::C, and the other modules of the Inline family. They allow you to write code in other languages directly in perl, and call to it. Under the hood Inline::C just builds XS code and loads the result of that into the interpreter.

动听の歌 2024-10-02 01:43:16

正如@rafl 所说,您应该使用共享库。

如果您必须使用静态库,那么您必须使用内置静态库重建 Perl。您还需要一些 XS 胶水。然而,这太混乱了,你真的真的不想这样做。

As @rafl says, you should use a shared library.

If you must use a static library, then you have to rebuild Perl with the static library built in. You'll need some XS glue too. However, this is messy enough that you really, really don't want to do it.

南风几经秋 2024-10-02 01:43:16

根据perlxstut

人们普遍认为,如果系统不具备动态加载库的能力,则无法构建 XSUB。这是不正确的。您可以构建它们,但必须将 XSUB 子例程与 Perl 的其余部分链接起来,创建一个新的可执行文件。这种情况与Perl 4类似。

本教程仍然可以在这样的系统上使用。 XSUB 构建机制将检查系统并构建一个可动态加载的库(如果可能),或者构建一个静态库,然后可选地构建一个链接了该静态库的新的静态链接可执行文件。

如果您希望在可以动态加载库的系统上构建静态链接的可执行文件,您可以在以下所有示例中执行不带参数的命令“make”,改为运行命令“make perl”。

如果您选择生成了这样一个静态链接的可执行文件,那么您应该说“make test_static”,而不是“make test”。在根本无法构建动态加载库的系统上,只需说“make test”就足够了。

According to perlxstut:

It is commonly thought that if a system does not have the capability to dynamically load a library, you cannot build XSUBs. This is incorrect. You can build them, but you must link the XSUBs subroutines with the rest of Perl, creating a new executable. This situation is similar to Perl 4.

This tutorial can still be used on such a system. The XSUB build mechanism will check the system and build a dynamically-loadable library if possible, or else a static library and then, optionally, a new statically-linked executable with that static library linked in.

Should you wish to build a statically-linked executable on a system which can dynamically load libraries, you may, in all the following examples, where the command "make" with no arguments is executed, run the command "make perl" instead.

If you have generated such a statically-linked executable by choice, then instead of saying "make test", you should say "make test_static". On systems that cannot build dynamically-loadable libraries at all, simply saying "make test" is sufficient.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文