c++ - 如何在tcl中编译可加载的dll

标签 c++ dll compilation tcl

在尝试了很多并搜索了 web 选项来编译和加载 dll 之后,我无法为 tcl 创建 dll。你能解释一下如何做到这一点吗?

最佳答案

好的,这是一个简单的例子。此代码编译并适用于 Tcl8.5 和 VS2008。首先,我创建了一个名为 BasicTclExtn 的 WIN32 dll 项目,用于导出符号。

// BasicTclExtn.h
#ifdef BASICTCLEXTN_EXPORTS
#define BASICTCLEXTN_API __declspec(dllexport)
#else
#define BASICTCLEXTN_API __declspec(dllimport)
#endif

int BasicExtnCmd(ClientData data, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[]) ;
extern "C" {
    BASICTCLEXTN_API int Basictclextn_Init(Tcl_Interp *interp) ;
}

然后是.cpp文件

// BasicTclExtn.cpp : Defines the exported functions for the DLL application.
#include "stdafx.h"
#include "BasicTclExtn.h"

int
BasicExtnCmd(ClientData data,
             Tcl_Interp *interp,
             int objc,
             Tcl_Obj *CONST objv[])
{

    // Check the number of arguments
    if (objc != 3) {
        Tcl_WrongNumArgs(interp, 1, objv, "arg arg");
        return TCL_ERROR;
    }

    long v1, v2, result ;

    if ( Tcl_GetLongFromObj(interp, objv[1], &v1) != TCL_OK)
        return TCL_ERROR ;

    if ( Tcl_GetLongFromObj(interp, objv[2], &v2)  != TCL_OK)
        return TCL_ERROR ;

    result = v1 + v2 ;

    Tcl_SetObjResult(interp, Tcl_NewIntObj(result)) ;
        return TCL_OK ;
}

    // Note the casing on the _Init function name
    BASICTCLEXTN_API int Basictclextn_Init(Tcl_Interp *interp)
    {
        // Link with the stubs library to make the extension as portable as possible
        if (Tcl_InitStubs(interp, "8.1", 0) == NULL) {
            return TCL_ERROR;
        }

        // Declare which package and version is provided by this C code
        if ( Tcl_PkgProvide(interp, "BasicTclExtn", "1.0") != TCL_OK ) {
            return TCL_ERROR ;
        }

        // Create a command
        Tcl_CreateObjCommand(interp, "BasicExtnCmd", BasicExtnCmd, (ClientData)NULL, (Tcl_CmdDeleteProc *)NULL);

        return TCL_OK ;
    }

您需要在 stdafx.h 中#include tcl.h。

此示例使用 Tcl stub 工具,有关更多信息,请参阅有关 Tcl_InitStubs 函数的文档;使用 stub 时,您只需要链接到 tclstub85.lib。要使代码正确链接,您需要执行以下操作:

  • 将安装 tcl.h 的包含目录添加到 Configuration Properties -> C/C++ -> General -> Additional Include Directories
  • 定义USE_TCL_STUBS符号,我通常在 Properties-> C/C++ -> Preprocessor -> Preprocessor Definitions 中执行此操作。您可能还会发现您随后需要定义 <DLLNAME>_EXPORTS (在我的示例中为 BASICTCLEXTN_EXPORTS)在此之后,我不确定为什么会发生这种情况。
  • 在配置属性 -> 链接器 -> 常规 -> 附加库目录中添加 tclstub85.lib 文件所在目录的路径作为附加库目录。
  • 将 tclstub85.lib 添加到配置属性 -> 链接器 -> 输入 -> 附加依赖项
  • 如果编译器发出有关 MSVCRT 的警告,则通过将 MSVCRT 添加到配置属性 -> 链接器 -> 输入 -> 忽略特定库中的忽略库来排除 MSVCRT。

所有这些 .lib、.dll 和 .h 文件应该很容易在您的 Tcl 安装中找到。您还需要确保在运行时可以找到相关的 tclstub85.dll 和 tcl85.dll,确保 Tcl 的 bin 目录在 PATH 上应该解决这个问题。因此,您应该能够从 Tcl 执行以下操作:

C:\Projects\BasicTclExtn\Debug>tclsh
% load BasicTclExtn.dll
% BasicExtnCmd 1 2
3
% BasicExtnCmd 1 2.p
expected integer but got "2.p"
% BasicExtnCmd 1 2
3
% BasicExtnCmd 1
wrong # args: should be "BasicExtnCmd arg arg"
% BasicExtnCmd 1 3
4
% exit

这是最简单的 Tcl 扩展形式,您可以添加对 Tcl_CreateObjCommand() 的额外调用将更多命令添加到此扩展中。 Tcl 提供了一些工具来帮助处理传递给命令的命令行参数。使用的示例代码 Tcl_WrongNumArgs()但你也应该看看 Tcl_GetIndexFromObj()功能。

我还建议您获取 Brent Welch 撰写的《Practical Programming in Tcl and Tk》。您可以在这里阅读一些示例章节 http://www.beedub.com/book/ ,第 3 版中有关 Tcl 的 C 编程的章节将对您有很大帮助。

关于c++ - 如何在tcl中编译可加载的dll,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1392112/

相关文章:

c++ - 编译 C++ : undefined reference

c++ - 全局更改 gcc 包含路径

c++ - 如何在 openssl 中从 SHA 切换到 SHA-1

c++ - 如何在Mfc c++中创建透明矩形?

c++ - 将 dll 转换为 lib 以进行静态链接?

python - 在线编辑Python脚本?

c++ - Qt 中多线程的 Worker 类

c++ - 从不同的 dll 继承时信号未正确导出

c# - 如果 ISP 不使用 BIN 目录,我该如何部署 ASP.NET MVC 应用程序?

在 MinGW 中更改 pkg-config 的 PKG_CONFIG_PATH