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

Oli*_*One 4 c++ dll compilation tcl

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

Jac*_*son 8

好的,这是一个简单的例子.此代码编译并适用于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) ;
}
Run Code Online (Sandbox Code Playgroud)

然后是.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 ;
    }
Run Code Online (Sandbox Code Playgroud)

你需要在stdafx.h中#include tcl.h.

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

  • 将安装了tcl.h的include目录添加到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
Run Code Online (Sandbox Code Playgroud)

这是Tcl exextention最简单的形式,您可以添加其他调用以Tcl_CreateObjCommand()向此扩展中添加更多提交.Tcl提供了一些帮助处理传递给命令的命令行参数的方法.使用的示例代码,Tcl_WrongNumArgs()但您还应该查看Tcl_GetIndexFromObj()函数.

我还建议你在Brent Welch的Tcl和Tk中获得一份实用编程.您可以在这里阅读一些示例章节http://www.beedub.com/book/,第3版的Tcl C编程章节将对您有所帮助.