In a header file of your C/C++ application or extension that includes the Tcl library, add these lines:
#include <tcl.h> ... #if ((TCL_MAJOR_VERSION < 8) || ((TCL_MAJOR_VERSION == 8) \ && (TCL_MINOR_VERSION == 0))) extern char * Tcl_PkgPresent _ANSI_ARGS_((Tcl_Interp *, char *, char *, int)); #endif
Somewhere in your program/library code, add these lines:
#if ((TCL_MAJOR_VERSION < 8) || ((TCL_MAJOR_VERSION == 8) \ && (TCL_MINOR_VERSION == 0))) char * Tcl_PkgPresent(Tcl_Interp* interp, char* name, char* version, int exact) { /* Must use writable buffer to support Tcl 7. The Tcl_DStringAppend interface became CONST in Tcl 8 */ char *cmd = "package provide"; Tcl_DString buf; Tcl_DString loadedVersion; /* Use [package provide] to query if the named package is already in the interpreter. Tcl_PkgProvide() doesn't return the version, so a Tcl_Eval() is necessary. */ Tcl_DStringInit(&buf); Tcl_DStringAppend(&buf, cmd, -1); Tcl_Eval(interp, Tcl_DStringAppendElement(&buf, name)); Tcl_DStringFree(&buf); Tcl_DStringInit(&loadedVersion); Tcl_DStringGetResult(interp, &loadedVersion); if ( !(Tcl_DStringLength(&loadedVersion)) ) { Tcl_DStringFree(&loadedVersion); if (version != NULL) { Tcl_AppendResult(interp, "package ", name, " ", version, " is not present", (char *) NULL); } else { Tcl_AppendResult(interp, "package ", name, " is not present", (char *) NULL); } return NULL; } Tcl_DStringFree(&loadedVersion); /* At this point we know some version is loaded, so Tcl_PkgRequire will have no side effect. Call it to do the rest of the work. */ return Tcl_PkgRequire(interp, name, version, exact); } #endif
There! Now you can call Tcl_PkgPresent() even if you happen to link against a pre-Tcl 8.1 library.