我查找了一些相关信息,但没有发现任何有用的信息。
背景
我安装的是 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 库?
最佳答案
首先,我不确定是否可以从 C++ 调用 GCL 编译的函数。比较 CL 和 C++ 函数的定义:
(defun fib (x) ...)
和
int fib(int)
第二个函数是严格类型,而第一个函数接受并返回任何对象。那么,g++ 应该在 temp.o
文件中搜索什么函数?即使你在CL的函数中声明类型,编译后的函数是否与C++的函数具有相同的格式?即使是 C++ 和 Delphi 等类似的语言,如果没有特殊指令也无法链接在一起,因为向函数堆栈传递参数的顺序不同。深入研究你会发现 C++ 和 CL 具有完全不同的内存管理策略,因此如何一起使用它们是完全模糊的。
克服任何此类差异的一种方法是使用桥梁 - 可以从两种语言访问的任何资源,例如 socket 、管道等。例如,如果您有模块 my-lisp-module
您可以为其公共(public)函数创建简单的套接字接口(interface),并从您喜欢的任何语言调用它们。
虽然使用网桥非常灵活,但不是很方便。将 Common Lisp 嵌入到 C++ 程序中的另一种方法是...使用 Embedded Common Lisp 。它是专门为像您这样的目的而设计的。您可以在其manual上找到嵌入规则。页。
最后,您可以在已经支持与 C++ 代码集成的平台上使用 Common Lisp 实现。如果您仅在 Windows 中工作,那么将您的应用程序集成到 CL implementations for CLR 之一一定很容易。如果您即将迁移到 Linux,implementations for JVM也可用。
关于c++ - 有没有办法将我的 GCL Lisp 文件与 Windows 上的单独 C++ 程序链接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7114797/