生成一个新终端并打开 vim

Spawning a new terminal and opening vim

我想要实现的是从 C/C++ 程序和 运行 vim 打开一个新终端。我通过分叉和执行“xterm -e vim [fname]”来做到这一点。尽我所能,我似乎无法让 xterm 理解我想要它做什么。

相关代码段如下:

    int pid = fork();
    if (pid){
        //parent
        int retstat;
        waitpid (pid, &retstat, 0);
    }else{
        //child
        
        char* ifname_cchararr = (char*)malloc(ifname.length() + 1);
        strcpy (ifname_cchararr, ifname.c_str());
        char* const argv[4] = {"-e", "vim", ifname_cchararr, NULL};
       // std::cout << ifname_cchararr<<std::endl;
        execvp ("xterm", argv);
    }

运行 程序导致 xterm 抱怨:

-e : Explicit shell already was /usr/bin/vim

-e : bad command line option "testfile"

我觉得我搞砸了 argc,但我很困惑,因为 运行在 xterm window:

中设置了以下内容

xterm -e vim testfile

工作得很好。

请赐教!

您忘记添加 xterm 作为 argv 中的第一个参数。您必须将程序名称添加到 argv,这似乎有点奇怪,因为您已经告诉 execvp 您正在调用哪个程序,但事实就是如此。有关 为什么 的更多信息,请参阅最近在 Unix 和 Linux 上提出的问题:Why does argv include the program name