Search code examples
c++ctclexpectinclude-path

Including expect/tcl library for C/C++


Recently I found an example of how to use the expect library in C++. I tried to compile it, but the compiler (g++) said, that tcl8.5/expect.h doesn't exists. So I tried to include tcl8.6/expect.h - still the same error. I checked the /usr/include/ directory and I wasn't surprised when I've noticed, that there is no tcl8.x directory. I've searched for files with "expect" in their name. Here's what I found:

/usr/include/expect_tcl.h
/usr/include/expect_comm.h
/usr/include/expect.h

Unfortunately when I tried to include any of these I got the following list of errors during compilation:

> g++ test.cpp -Wall -std=c++0x -ltcl8.6 -lglog -o test
  /tmp/cce8k1BA.o: In function `task(std::string const&, std::string const&, std::string const&)':
  test.cpp:(.text+0x16): undefined reference to `exp_is_debugging'
  test.cpp:(.text+0x20): undefined reference to `exp_timeout'
  test.cpp:(.text+0x38): undefined reference to `exp_popen'
  etc...

How can I solve this problem?

[EDIT] When I tried to link it with the expect lib (-lexpect) I got the following error:

/usr/bin/ld: cannot find -lexpect
collect2: error: ld returned 1 exit status

I'm sure that both - tcl8.6 and expect 5.45-4 are installed.


Solution

  • The usual way of distributing Expect these days puts the shared library in a non-standard location and loads it dynamically by full pathname. This works well and is minimal fuss for most people, but does make it rather hard to use Expect's C interface in your own code.

    The easiest way is going to be to build your own copy from source, especially as that will give you control over how exactly it was built. This can particularly include keeping the majority of symbols in the library instead of stripping them on install, which will help a lot with debugging. You probably ought to use the current supported version. (Yes, it's a release from several years ago. It doesn't need a lot of support effort most of the time.)