How to compile a DLL loaded in tcl

After trying a lot and searching for a web option to compile and download the dll, I could not create the dll for tcl. Can you explain to me how to do this.

+4
source share
1 answer

Ok, here is a simple example. This code compiles and works for Tcl8.5 and VS2008. To start, I created a WIN32 dll project called BasicTclExtn that exported characters.

// 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) ; } 

And then the .cpp file

 // 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 ; } 

You need # to include tcl.h in stdafx.h.

This example uses the Tcl stubs tool; for more information, see the Tcl_InitStubs function documentation; when using stubs, you only need to reference tclstub85.lib. To install the code correctly, you need to do the following:

  • Add include directory where tcl.h is installed in Configuration Properties -> C / C ++ -> General -> Additional Include Directories
  • Define the symbol USE_TCL_STUBS , I usually do this in Properties-> C / C ++ โ†’ Preprocessor โ†’ Preprocessor Definitions. You may also find that then you need to define <DLLNAME>_EXPORTS ( BASICTCLEXTN_EXPORTS in my example) after that, I'm not sure why this is happening.
  • Add the path to the directory where the tclstub85.lib file is an additional library directory under Configuration Properties โ†’ Linker โ†’ General โ†’ Additional Library Directories.
  • Add tclstub85.lib to configuration properties -> Linker -> Input -> Additional Dependancies
  • If the compiler throws a warning about MSVCRT, then exclude MSVCRT by adding it to ignored libraries in Configuration Properties โ†’ Linker โ†’ Input โ†’ Ignore Specific Library.

All of these .lib, .dll, and .h files should be easily found in your Tcl installation. You also need to make sure that the related tclstub85.dll and tcl85.dll can be found at runtime, making sure the bin directory for Tcl is in PATH should sort it. Thus, you should be able to do the following from 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 

This is the simplest form of Tcl exstention, you can add additional calls to Tcl_CreateObjCommand() to add more commnds to this extension. Tcl provides some features that help you process the command line options passed to the command. The sample code uses Tcl_WrongNumArgs() , but you should also look at the functions Tcl_GetIndexFromObj() .

I also suggest you get a copy of Practical Programming in Tcl and Tk from Brent Welch. You can read a few examples here http://www.beedub.com/book/ , the chapter on C programming for Tcl from the 3rd edition will help you a lot.

+8
source

All Articles