开发者

Is there a way to link my GCL Lisp file with a separate C++ program on Windows?

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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜