无法构建非常简单的tcl应用程序

时间:2016-06-20 15:20:10

标签: tcl

我试图用C ++编写 very 简单的Tcl应用程序:

#include <tcl.h>

#include <iostream>

int main (int argc, char *argv[])
{
    std::cout << "Calling Tcl_FindExecutable." << std::endl;
    Tcl_FindExecutable (argv[0]);

    std::cout << "Calling Tcl_CreateInterp." << std::endl;
    Tcl_Interp *pInterp = Tcl_CreateInterp ();

    if (Tcl_Eval (pInterp, "puts stdout {Hello, World!}") != TCL_OK)
    {
        std::cerr << "Error: " << Tcl_GetStringResult (pInterp) << std::endl;
        return (0);
    }

    if (Tcl_Eval (pInterp, "puts stdout [info nameofexecutable]") != TCL_OK)
    {
        std::cerr << "Error: " << Tcl_GetStringResult (pInterp) << std::endl;
        return (0);
    }

    return (1);
}

我可以通过g++ -c Wall -I/opt/ActiveTcl-8.6/include noddy.cpp -o noddy.o

进行编译

但是当我用g++ -L/opt/ActiveTcl-8.6/lib -ltcl8.6 -o noddy noddy.o

链接它时

我收到错误,说所有Tcl库程序都是未定义的。

我做错了什么,拜托?

修改

实际命令是

$ g++ -c -Wall -I/opt/ActiveTcl-8.6/include noddy.cpp -o noddy.o
$ g++ -L/opt/ActiveTcl-8.6/lib -ltcl8.6 -o noddy noddy.o
noddy.o: In function 'main':
noddy.cpp:(.text+0x37): undefined reference to 'Tcl_FindExecutable'
noddy.cpp:(.text+0x60): undefined reference to 'Tcl_CreateInterp'
noddy.cpp:(.text+0x78): undefined reference to 'Tcl_Eval'
noddy.cpp:(.text+0x8d): undefined reference to 'Tcl_GetStringResult'
noddy.cpp:(.text+0xda): undefined reference to 'Tcl_Eval'
noddy.cpp:(.text+0xef): undefined reference to 'Tcl_GetStringResult'
collect2: ld returned 1 exit status

1 个答案:

答案 0 :(得分:0)

在链接语句中,对象模块和库的顺序非常重要。您应首先包含对象,然后包含库(重写为C以避免安装g ++):

> gcc -Wall -I/opt/tcl/include -c noddy.c
> gcc -o noddy.exe noddy.o -L/opt/tcl/lib -ltcl86
> noddy
Calling Tcl_FindExecutable.
Calling Tcl_CreateInterp.
Hello, World!
C:/Code/noddy.exe

可是:

> gcc -o noddy.exe -L/opt/tcl/lib -ltcl86 noddy.o
noddy.o:noddy.c:(.text+0x23): undefined reference to '_imp__Tcl_FindExecutable'
noddy.o:noddy.c:(.text+0x36): undefined reference to '_imp__Tcl_CreateInterp'
noddy.o:noddy.c:(.text+0x50): undefined reference to '_imp__Tcl_Eval'
noddy.o:noddy.c:(.text+0x62): undefined reference to '_imp__Tcl_GetStringResult'
noddy.o:noddy.c:(.text+0x9b): undefined reference to '_imp__Tcl_Eval'
noddy.o:noddy.c:(.text+0xad): undefined reference to '_imp__Tcl_GetStringResult'
e:/opt/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: noddy.o: bad reloc address 0x20 in section '.eh_frame'
e:/opt/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: final link failed: Invalid operation
collect2.exe: error: ld returned 1 exit status