TCL:与Windows中的静态库链接

问题描述

我已经从here下载了TCL源代码,可以使用以下命令在Windows7中编译它以生成静态

nmake -f makefile.vc OPTS=static INSTALLDIR=path_to_your_install_dir
nmake -f makefile.vc install OPTS=static INSTALLDIR=path_to_your_install_dir

输出中,我可以得到tcl87s.lib - tcldds14s.lib - tclreg13s.lib

现在,我正在尝试构建我的C代码(已嵌入TCL):

static const char *user_script = "puts \"Hello World ........\"\n";

int my_cmd(Tcl_Interp *interp,const char *arg_str,...){
  char *cmd_str = Tcl_Alloc(256);
  va_list ap;
  int result;
  va_start(ap,arg_str);
  vsprintf(cmd_str,arg_str,ap);
  result = Tcl_Eval(interp,cmd_str);
  Tcl_Free(cmd_str);
  return result;
}

int main (int argc,char *argv[])
{
    Tcl_FindExecutable(argv[0]);
    Tcl_Interp *myinterp;
    printf ("C: Starting ... \n");
    myinterp = Tcl_CreateInterp();

    if (Tcl_Init(myinterp) != TCL_OK) {
        printf("Error: %s\n",Tcl_GetStringResult(myinterp));
        return TCL_ERROR;
    }

    if (my_cmd(myinterp,user_script) != TCL_OK) {
        printf("Error: %s\n",Tcl_GetStringResult(myinterp));
        return TCL_ERROR;
    }
    
    Tcl_DeleteInterp(myinterp);
    Tcl_Finalize();
    return TCL_OK;
}

因为我需要在Windows中构建它,所以我在Visual Studio命令提示符中编译此代码

cl -I"D:\path\to\tcl\include" myCode.c D:\path\to\tcl87s.lib

但是不幸的是,我总是遇到这些错误

error LNK2019: unresolved external symbol __imp__Tcl_Alloc referenced in function ...
error ....................................__imp__Tcl_Free ..................
error ....................................__imp__Tcl_CreateInterp ..................

.........以及其他TCL功能的更多错误.......

如果我将TCL构建为动态库,则不会出错,并且可以编译C代码。有谁知道可能是什么问题

解决方法

原来,我在编译C代码时必须使用标志-DSTATIC_BUILD。