有没有办法将我的 GCL Lisp 文件与单独的 C++ 链接起来Windows 上的程序?

发布于 2024-11-30 02:54:10 字数 1300 浏览 1 评论 0原文

我已经查找了一些有关此的信息,但没有发现任何非常有帮助的信息。

背景

我安装的是 GNU Common Lisp。我可以创建一个 Lisp 文件并使用以下命令将其编译为 .o 目标文件:

gcl -compile <my lisp filename>

一旦我有了该 .o 文件,我就使用该命令(使用 MinGW):

g++ -o myProgram.exe temp.o temp.cpp

我的 Lisp 文件在 GCL 中工作。我的 temp.cpp 文件是:

//function from (defun fib (x) ...) in lisp file
extern int fib(int);

#include <iostream>

int main()
{
    int val;

    std::cout << "Print Fibonacci numbers" << std::endl;

    std::cout << ">";
    std::cin >> val;
    while (val != -1)
    {
        std::cout << fib(val) << std::endl << std::endl;
        std::cout << ">";
        std::cin >> val;
    }

    return 0;
}

编译时出现的错误是这样的:

temp.cpp:(.text+0x180): undefined reference to `fib(int)'
temp.o:temp.c:(.text+0xb): undefined reference to `vs_base'
temp.o:temp.c:(.text+0x17): undefined reference to `vs_limit'
temp.o:temp.c:(.text+0x1d): undefined reference to `vs_top'
temp.o:temp.c:(.text+0x2d): undefined reference to `small_fixnum_table'
...

错误很长,看起来像 GCL 中定义的所有函数。

我的问题

所以,最后是我的问题。我想做的事情可能吗?如果我打算将程序与 C++ 程序链接,是否需要在程序中包含整个 GCL 库?

I have looked for some info on this and haven't found anything very helpful.

Background

What I have is GNU Common Lisp installed. I can create a Lisp file and compile it to a .o object file using the command:

gcl -compile <my lisp filename>

Once I have that .o file I use the command (using MinGW):

g++ -o myProgram.exe temp.o temp.cpp

My Lisp file works in GCL. My temp.cpp file is:

//function from (defun fib (x) ...) in lisp file
extern int fib(int);

#include <iostream>

int main()
{
    int val;

    std::cout << "Print Fibonacci numbers" << std::endl;

    std::cout << ">";
    std::cin >> val;
    while (val != -1)
    {
        std::cout << fib(val) << std::endl << std::endl;
        std::cout << ">";
        std::cin >> val;
    }

    return 0;
}

The errors I get when compiling are this:

temp.cpp:(.text+0x180): undefined reference to `fib(int)'
temp.o:temp.c:(.text+0xb): undefined reference to `vs_base'
temp.o:temp.c:(.text+0x17): undefined reference to `vs_limit'
temp.o:temp.c:(.text+0x1d): undefined reference to `vs_top'
temp.o:temp.c:(.text+0x2d): undefined reference to `small_fixnum_table'
...

The errors are a lot longer and it looks like all the functions defined in GCL.

My Question

So, finally my question. Is what I am trying to do possible? Do I somehow need to include the entire GCL library with a program if I plan on linking it with a C++ program?

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

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

发布评论

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

评论(1

我喜欢麦丽素 2024-12-07 02:54:10

首先,我不确定是否可以从 C++ 调用 GCL 编译的函数。比较 CL 和 C++ 函数的定义:

(defun fib (x) ...)

第二

int fib(int)

个函数是严格类型,而第一个函数接受并返回任何对象。那么,g++ 应该在 temp.o 文件中搜索什么函数?即使你在CL的函数中声明类型,编译后的函数是否与C++的函数具有相同的格式?即使是 C++ 和 Delphi 等类似的语言,如果没有特殊指令也无法链接在一起,因为向函数堆栈传递参数的顺序不同。深入研究你会发现 C++ 和 CL 具有完全不同的内存管理策略,因此如何一起使用它们是完全模糊的。

克服任何此类差异的一种方法是使用 - 可以从两种语言访问的任何资源,例如套接字、管道等。例如,如果您有模块 my-lisp-module 您可以为其公共函数创建简单的套接字接口,并从您喜欢的任何语言调用它们。

虽然使用网桥非常灵活,但不是很方便。将 Common Lisp 嵌入到 C++ 程序中的另一种方法是...使用 嵌入式 Common Lisp。它是专门为像您这样的目的而设计的。您可以在其手册页面上找到嵌入规则。

最后,您可以在已经支持与 C++ 代码集成的平台上使用 Common Lisp 实现。如果您仅在 Windows 中工作,则必须很容易将您的应用程序集成到 CLR 的 CL 实现。如果您打算迁移到 Linux,还可以使用 JVM 实现

First of all, I'm not sure it is possible to call GCL compiled functions from C++ at all. Compare definitions of your CL's and C++'s functions:

(defun fib (x) ...)

and

int fib(int)

Second function is strictly typed, while first one takes and returns any object. So, what function should g++ search for in your temp.o file? Even if you declare type in CL's function, will compiled function have same format as C++'s functions? Even such similar languages as C++ and Delphi cannot be linked together without special directives because of different order of passing arguments to the function stack. Diving deeper you can see that C++ and CL have completely different memory management strategies, so it's completely obscure how to use them together.

One way to overcome any such differences is to use bridges - any resources that can be accessed from both languages, e.g. sockets, pipes and so on. For example, if you have module my-lisp-module you can create simple socket interface for its public functions and call them from whatever language you like.

Though using bridges is extremely flexible, it is not very convenient. Another way to embed Common Lisp into C++ program is to... use Embedded Common Lisp. It was designed specially for purposes like yours. You can find rules of embedding on their manual page.

Finally, you can use Common Lisp implementation for a platform, that already supports integration with C++ code. If you work only in Windows, it must be easy to integrate your app on one of CL implementations for CLR. If you are about to move to Linux, implementations for JVM are also available.

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