如何使用libssh运行makefile

How to run makefile with libssh?

本文关键字:makefile 运行 libssh 何使用      更新时间:2023-10-16

我的libssh(libssh.org(有问题。我需要在远程服务器上运行一个makefile。我使用命令"channel_request_exec":

int SSHExecCmd (void(* MessSender)(char* CurMessage, bool IsError, CWnd* MainWnd),ssh_session session,  CString & ShellEcho, char * cmd, CWnd* MainWnd)
{
    ssh_channel channel;
    int rc;
    channel = ssh_channel_new(session);
    if (channel == NULL) return SSH_ERROR;
    rc = ssh_channel_open_session(channel);
    if (rc != SSH_OK)
    {
        ssh_channel_free(channel);
        return rc;
    }
    rc = ssh_channel_request_exec(channel, cmd);
    if (rc != SSH_OK)
    {
        ssh_channel_close(channel);
        ssh_channel_free(channel);
        return rc;
    }
    char buffer[256];
    unsigned int nbytes;
    nbytes = ssh_channel_read(channel, buffer, sizeof(buffer), 0);
    while (nbytes > 0)
    {
        if (fwrite(buffer, 1, nbytes, stdout) != nbytes)
        {
            ssh_channel_close(channel);
            ssh_channel_free(channel);
            return SSH_ERROR;
        }
        nbytes = ssh_channel_read(channel, buffer, sizeof(buffer), 0);
    }
    if (nbytes < 0)
    {
        ssh_channel_close(channel);
        ssh_channel_free(channel);
        return SSH_ERROR;
    }
    return SSH_OK;
}

Makefile位于根目录:

all: mpi_cuda.o pattern2d.o
        mpicc mpi_cuda.o pattern2d.o -o mpi_cuda -lrt -lpthread -L/opt/cuda/lib64 -lcudart -lm
mpi_cuda.o: mpi_cuda.c
        mpicc -g -std=c99 -D_GNU_SOURCE -I/opt/cuda/include -c $< -o $@
pattern2d.o: pattern2d.cu
        nvcc -g -c $< -o $@

我发送命令"make"并接收回波:

mpicc -g -std=c99 -D_GNU_SOURCE -I/opt/cuda/include -c mpi_cuda.c -o mpi_cuda.oda

但是什么也没发生(不执行编译(。

如果我真的用油灰做:一切都可以。回声:

make
mpicc -g -std=c99 -D_GNU_SOURCE -I/opt/cuda/include -c mpi_cuda.c -o mpi_cuda.o
mpi_cuda.c: В функции ‘main’:
mpi_cuda.c:148: предупреждение: недостаточно аргументов для указанного формата
nvcc -g -c pattern2d.cu -o pattern2d.o
mpicc mpi_cuda.o pattern2d.o -o mpi_cuda -lrt -lpthread -L/opt/cuda/lib64 -lcudart -lm

我该如何解决此问题?

不熟悉libssh,但错误可能是,因为环境设置不同,所以显式运行make-throughshell可能会有所帮助。

尝试将命令(make?(更改为

bash -c make

如果不起作用,请尝试

bash -c "export > env.txt ; make > make_out.txt 2> make_err.txt"

然后检查这些文件是否出现,以及它们包含的内容,这应该会给出很好的提示。

如果您有一个工作案例和一个非工作案例,那么从这两个案例中获取这些文件,并将它们进行比较(例如,与diff -u(。

如果您不使用bash,请将bash更改为您使用的任何shell(在这种情况下,请检查-c是否是给出命令字符串的正确开关,以及export是否是显示环境的正确命令(。


基于以下注释:env.txt中的差异可能是,因为一些环境变量只为交互式shell设置。例如,在我的Ubuntu框中,.bashrc的开头有这样的行:

# If not running interactively, don't do anything
[ -z "$PS1" ] && return

现在,如果在该行之后的.bashrc中设置了任何所需的环境变量,并且您的ssh连接是非交互式的(没有伪tty(,则不会设置这些变量。

如果是这种情况,请在进行上述测试之前,将这些env变量集移动到~/.profile~/.bashrc中。还要执行man bash,并阅读有关初始化文件的内容(如~/.bashrc(。

另一个解决方案是使ssh会话具有交互性,我相信这一点在本页中有关于libssh的文档:http://api.libssh.org/master/libssh_tutor_shell.html。

我建议打开一个非交互式shell并在那里执行。参见

http://api.libssh.org/master/libssh_tutor_shell.html