Why do I get a segmentation fault in my simple c++ program using libexpect.so?

China☆狼群 提交于 2019-12-04 04:01:11

The global variable TclStubs *tclStubsPtr defined inside Tcl happens to be NULL when exp_spawnv tries to accessTcl_ErrnoMsg which is defined as a member of that structure (see tcl.h):

#ifndef Tcl_ErrnoMsg
#define Tcl_ErrnoMsg \
    (tclStubsPtr->tcl_ErrnoMsg) /* 128 */
#endif

I'm not familiar with neither expect nor Tcl but the above suggests that you probably should call some initialization subroutine (if there exists one) or set it manually.

I'd be most concerned about the warning when compiling. The interface appearantly requires you to pass a writable string, but you pass a string constant. If it does indeed write to it, it will result in a segmentation fault. So it looks like a good candidate for your problem.

What happens if you instead try to create a writable buffer and pass that:

char name[] = "bash";
FILE* file = exp_popen(name);

Update: I've tested your program (with the above change, and a "return 0;" at the end), and it works fine for me. Perhaps there's something wrong with your system, like a half-installed library? You can check if it also fails when you link with -static. If you do that, you are sure that the compile-time linked library is the same as the run-time used library (because it will be included in the executable at compile-time).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!