Oli*_*One 4 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) ;
}
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.要使代码正确链接,您需要执行以下操作:
USE_TCL_STUBS符号,我通常在Properties-> C/C++ - > Preprocessor - > Preprocessor Definitions中执行此操作.您可能还会发现在此之后您需要定义<DLLNAME>_EXPORTS(BASICTCLEXTN_EXPORTS在我的示例中),我不确定为什么会发生这种情况.所有这些.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编程章节将对您有所帮助.
| 归档时间: |
|
| 查看次数: |
3882 次 |
| 最近记录: |