Tcl运行环境在启动时会通过tclInit
函数按如下顺序加载第一个找到的文件"init.tcl"。
如果没有找到,则提示错误并退出。
根据proc tclInit
源码整理如下:
#------# 1. $tcl_library 2. $env(TCL_LIBRARY) 3. $env(TCL_LIBRARY)/../tcl8.5 ;# require TCL_LIBRARY ~= /tcl(.*)$ 4. $tclDefaultLibrary || ::tcl::pkgconfig get scriptdir,runtime 5. $tcl_dir/lib/tcl8.5 ;# tcldir = [file dir [file dir $argv0] ] 6. $tcl_dir/../lib/tcl8.5 ;# argv0 = tclsh = $tcl_dir/bin/tclsh 7. $tcl_dir/library 8. $tcl_dir/../library 9. $tcl_dir/../tcl8.5.9/library ;# info patchlevel 10. $tcl_dir/../../tcl8.5.9/library 11. {*}$tcl_libPath #------# # Note: tcl8.5 = tcl[info tclversion] tcl8.5.9 = tcl[info patchlevel]
综上来看,可以考虑在两个位置安装"init.tcl"文件:
$TCL_LIBRARY
:设置为"/some/path/tcl" 或者 "/some/path/tcl8.5"$0/../lib/tcl8.5
,比如 "/some/path/to/tcl/lib/tcl8.5"if {[namespace which -command tclInit] eq ""} { proc tclInit {} { global tcl_libPath tcl_library env tclDefaultLibrary rename tclInit {} if {[info exists tcl_library]} { set scripts {{set tcl_library}} } else { set scripts {} if {[info exists env(TCL_LIBRARY)] && ($env(TCL_LIBRARY) ne {})} { lappend scripts {set env(TCL_LIBRARY)} lappend scripts { if {[regexp ^tcl(.*)$ [file tail $env(TCL_LIBRARY)] -> tail] == 0} continue if {$tail eq [info tclversion]} continue file join [file dirname $env(TCL_LIBRARY)] tcl[info tclversion]} } if {[info exists tclDefaultLibrary]} { lappend scripts {set tclDefaultLibrary} } else { lappend scripts {::tcl::pkgconfig get scriptdir,runtime} } lappend scripts { set parentDir [file dirname [file dirname [info nameofexecutable]]] set grandParentDir [file dirname $parentDir] file join $parentDir lib tcl[info tclversion]} \ {file join $grandParentDir lib tcl[info tclversion]} \ {file join $parentDir library} \ {file join $grandParentDir library} \ {file join $grandParentDir tcl[info patchlevel] library} \ { file join [file dirname $grandParentDir] tcl[info patchlevel] library} if {[info exists tcl_libPath] && [catch {llength $tcl_libPath} len] == 0} { for {set i 0} {$i < $len} {incr i} { lappend scripts [list lindex \$tcl_libPath $i] } } } set dirs {} set errors {} foreach script $scripts { lappend dirs [eval $script] set tcl_library [lindex $dirs end] set tclfile [file join $tcl_library init.tcl] if {[file exists $tclfile]} { if {[catch {uplevel #0 [list source $tclfile]} msg opts]} { append errors "$tclfile: $msg " append errors "[dict get $opts -errorinfo] " continue } unset -nocomplain tclDefaultLibrary return } } unset -nocomplain tclDefaultLibrary set msg "Can't find a usable init.tcl in the following directories: " append msg " $dirs " append msg "$errors " append msg "This probably means that Tcl wasn't installed properly. " error $msg } }