使用 Swig 将 C 和 TCL 结合起来

发布于 2024-12-05 20:01:11 字数 966 浏览 2 评论 0原文

我一直在遵循使用 Swig 将 C 与 TCL 结合起来的教程。该教程似乎工作正常,但最后我遇到了一个无法解决的错误。情况如下:

我遵循的教程是: http://www.swig.org/tutorial.html

我有一个名为 test.c: 的文件

char *HelloWorld()
{
    return "hello world";
}

和另一个名为 test.i: 的

%module test
%{
/* Put header files here or function declarations like below */
extern char *HelloWorld();
%}

extern char *HelloWorld();

文件,然后我使用以下命令行参数来准备正确的文件:

gcc -c test.c -o test.o
swig -tcl test.i 
gcc -c test_wrap.c -o test_wrap.o
gcc -dynamiclib -framework Tcl  test.o test_wrap.o -o test.so

最后我尝试使用以下方式加载它:

tclsh
% load test.so test

这是我收到以下错误的地方:

dlsym(0x100600090, Test_Unload): symbol not founddlsym(0x100600090, Test_SafeUnload): symbol not found

据我所知,我并没有偏离教程。谁能告诉我我是如何得到这个错误的,更重要的是如何摆脱它?

提前致谢!

I have been following a tutorial to combine C with TCL using Swig. The tutorial seemed to be properly working but at the end I ran into an error that I cannot solve. The situation is as follows:

The tutorial I was following is:
http://www.swig.org/tutorial.html.

I have a file named test.c:

char *HelloWorld()
{
    return "hello world";
}

and another named test.i:

%module test
%{
/* Put header files here or function declarations like below */
extern char *HelloWorld();
%}

extern char *HelloWorld();

I then used the following command line arguments to ready the correct files:

gcc -c test.c -o test.o
swig -tcl test.i 
gcc -c test_wrap.c -o test_wrap.o
gcc -dynamiclib -framework Tcl  test.o test_wrap.o -o test.so

And finally I tried to load it using:

tclsh
% load test.so test

This is the point where I received the following error:

dlsym(0x100600090, Test_Unload): symbol not founddlsym(0x100600090, Test_SafeUnload): symbol not found

As far as I know I did not stray from the tutorial. Can anyone tell me how it is that I got this error and more importantly how to get rid of it?

Thanks in advance!

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

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

发布评论

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

评论(2

瞄了个咪的 2024-12-12 20:01:11

这些错误消息是否会阻止加载工作?他们不应该;他们报告说不存在支持卸载扩展的低级 API,但这没关系(很多扩展无法卸载;编写支持它的代码很棘手)。

您没有具体提及您正在使用哪个版本的 Tcl — 但它必须至少为 8.5 才能首先搜索这些符号 — 因此很难猜测确切的根本问题是什么。 (该消息不应被报告。)我建议提交 错误报告对此; 确保您在报告中包含所有准确的版本。

Are those error messages stopping the load from working? They shouldn't; they're reporting that the low-level API for supporting unloading of the extension isn't present, but that's OK (lots of extensions can't be unloaded; it's tricky to write code that supports it).

You don't mention exactly which version of Tcl you are using — but it must be at least 8.5 for those symbols to be even searched for in the first place — so it is a little hard to guess what the exact underlying issue is. (The message should simply not be reported.) I advise filing a bug report on this; make sure you include all exact versions in your report.

青朷 2024-12-12 20:01:11

我已经很长时间没有使用 SWIG 了,所以我不确定它是否能让您充分控制它生成的代码,以便您能够应用此修复程序。掩盖这个细节,我可以通过以下内容重现(并修复)该问题:

在“ext.c”中:

#include <tcl.h>

int DLLEXPORT Ext_Init(Tcl_Interp *interp) {


    if (Tcl_InitStubs(interp, TCL_VERSION, 0) == NULL) {
        return TCL_ERROR;
    }
    if (Tcl_PkgProvide(interp, "Ext", "0.0") == TCL_ERROR) {
        return TCL_ERROR;
    }

    return TCL_OK;
}

构建,运行 tclsh,加载扩展:

$ gcc -dynamiclib -framework Tcl ext.c -o ext.so
$ tclsh8.5
% load ./ext.so
dlsym(0x400000, Ext_SafeInit): symbol not found
dlsym(0x400000, Ext_Unload): symbol not found
dlsym(0x400000, Ext_SafeUnload): symbol not found

库加载代码内部的某些内容将该错误消息放入解释器结果中。要停止显示消息,请设置或重置结果,以便 _Init() 函数以以下一项或多项结束:

//    Set the result to a message of your choosing
    Tcl_SetObjResult(interp, Tcl_NewStringObj("ok", -1));

//    Or clear out the result altogether
    Tcl_ResetResult(interp);
    return TCL_OK;
}

init block swig 的功能可能在正确的位置插入代码来实现相同的效果:

%init %{
Tcl_ResetResult(interp);
%}

It's a long time since I used SWIG, so I'm not sure whether it gives you sufficient control over the code it generates for you to be able to apply this fix. Glossing over that detail, I can reproduce (and fix) the issue with the following:

In 'ext.c':

#include <tcl.h>

int DLLEXPORT Ext_Init(Tcl_Interp *interp) {


    if (Tcl_InitStubs(interp, TCL_VERSION, 0) == NULL) {
        return TCL_ERROR;
    }
    if (Tcl_PkgProvide(interp, "Ext", "0.0") == TCL_ERROR) {
        return TCL_ERROR;
    }

    return TCL_OK;
}

Build, run tclsh, load extension:

$ gcc -dynamiclib -framework Tcl ext.c -o ext.so
$ tclsh8.5
% load ./ext.so
dlsym(0x400000, Ext_SafeInit): symbol not found
dlsym(0x400000, Ext_Unload): symbol not found
dlsym(0x400000, Ext_SafeUnload): symbol not found

Something internal to the library loading code is putting that error message into the interpreters result. To stop the message ever surfacing, set or reset the result so that the _Init() function ends with one or other of:

//    Set the result to a message of your choosing
    Tcl_SetObjResult(interp, Tcl_NewStringObj("ok", -1));

//    Or clear out the result altogether
    Tcl_ResetResult(interp);
    return TCL_OK;
}

The init block feature of swig might insert code in the right place to achieve the same thing:

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