如何告诉TCL和TK应该在本地搜索默认的`.tcl`语言文件?

时间:2011-07-01 21:07:34

标签: c++ c boost tcl tk

当您编译TCL / Tk manually from sourcesinstall it from ActiveState时,您在TCL / Tk安装文件夹中获得以下结构:

  +- bin
       +- tcl85.dll
       +- tk85.dll
  //...
  +- lib/
       +- tcl8.5/
            //All TCL files (.tcl)
       +- tk8.5/
            //All TK files (.tcl)
  //...

所以当你编译你的某个应用程序并将其链接到TCL和TK DLL时,DLL会搜索相对于tham(到DLL的)目录的所有TCL / TK文件../lib/tk8.5和../lib /tcl8.5。这使得分发您的应用程序变得非常困难,而不必让最终用户安装TCL和TK。

我想发布我的C ++应用程序。

我使用CPPTK作为默认的GUI布局。

我希望能够让终端用户无需安装TCL和TK。我想为他们提供包含TK和TCL .TCL源文件的文件夹,这些文件位于相对于我的应用程序的某个目录中,例如extras/TCLextras/TK。如何告诉TK和TCL DLL源文件夹的位置?这样做的TK和TCL API函数名称是什么?那有什么特殊的cpptk函数吗?

更新 所以我尝试了Donal Fellows answer下一个文件夹结构。

app/
  +- app.exe
  +- tcl85.dll
  +- tk85.dll
  +- extras/
       +- tcl/
            //All TCL files you can find in TCL install folder/ lib/tcl8.5
       +- tk/
            //All TK files you can find in TCL install folder/ lib/tk8.5

我的代码看起来像:

#include <stdio.h>
#include "cpptk/cpptk.h"
using namespace Tk;
int main(int, char *argv[])
{
static char* str = "set extrasDir [file dirname [info nameofexecutable]]/extras\n"
"# Now use it to load some code...\n"
"source $extrasDir/tcl/init.tcl\n"
"# Another way to load code, using all *.tk files from a directory:\n"
"foreach tkFile [glob -nocomplain -directory $extrasDir/tk *.tk] {\n"
"    source $tkFile\n"
"}\n";
// This next part is in a function or method...
//std::string script("the script to evaluate goes here");
std::string result = Tk::details::Expr(str,true); // I think this is correct
std::cout<< result << std::endl;
std::cin.get();
Tk::init(argv[0]);
button(".b") -text("Say Hello");
pack(".b") -padx(20) -pady(6);
Tk::runEventLoop();
std::cin.get();
}

它在cpptkbase.cc的第36行编译但是错误。

BTW:我使用了this html\js app to get char string.

1 个答案:

答案 0 :(得分:1)

如果你有一个包含二进制文件的目录,并且想要找到相对于它的那些Tcl文件,如下所示:

yourapp1.0/
  +- yourapp.exe
  +- extras/
       +- tcl/
            +- foo.tcl
            +- bar.tcl
       +- tk/
            +- grill.tk

然后您可以编写Tcl代码来查找这些脚本。那段代码就像这样:

set extrasDir [file dirname [info nameofexecutable]]/extras
# Now use it to load some code...
source $extrasDir/tcl/foo.tcl
source $extrasDir/tcl/bar.tcl
# Another way to load code, using all *.tk files from a directory:
foreach tkFile [glob -nocomplain -directory $extrasDir/tk *.tk] {
    source $tkFile
}

如果您使用脚本作为主程序,但在上述结构中进行了设置,则使用$argv0(一个特殊的全局变量)而不是[info nameofexecutable]。或者可能[info script](尽管有一些警告)。


[编辑]:要使代码适用于C ++ / Tk,您需要更加棘手。特别是,您需要访问一些额外的内容:

#include "cpptk.h" // might need "base/cpptkbase.h" instead
#include <string>

// This next part is in a function or method...
std::string script("the script to evaluate goes here");
std::string result = Tk::details::Expr(script,true); // I think this is correct

我应该警告我不经常写C ++,所以很有可能这不会起作用;它只是基于读取C ++ / Tk源并进行猜测。 警告经纪人