老师我有一个 源码文件 在 msys2 终端里可以编译 运行 如下截图
但是在 clion 里不能运行
请问老师我应该如何解决呢
工程名字
untitled
代码文件名字 min.c
内容如下
#include <libssh/libssh.h>
#include <stdlib.h>
#include <stdio.h>
void free_channel(ssh_channel channel) {
ssh_channel_send_eof(channel);
ssh_channel_close(channel);
ssh_channel_free(channel);
}
void free_session(ssh_session session) {
ssh_disconnect(session);
ssh_free(session);
}
void error(ssh_session session) {
fprintf(stderr, “Error: %s\n”, ssh_get_error(session));
free_session(session);
exit(-1);
}
int main() {
ssh_session session;
ssh_channel channel;
int rc, port = 22;
char buffer[1024];
unsigned int nbytes;
printf(“Session…\n”);
session = ssh_new();
if (session == NULL) exit(-1);
ssh_options_set(session, SSH_OPTIONS_HOST, “127.0.0.1”);
ssh_options_set(session, SSH_OPTIONS_PORT, &port);
ssh_options_set(session, SSH_OPTIONS_USER, “root”);
printf(“Connecting…\n”);
rc = ssh_connect(session);
if (rc != SSH_OK) error(session);
printf(“Password Autentication…\n”);
rc = ssh_userauth_password(session, NULL, “123456”);
if (rc != SSH_AUTH_SUCCESS) error(session);
printf(“Channel…\n”);
channel = ssh_channel_new(session);
if (channel == NULL) exit(-1);
printf(“Opening…\n”);
rc = ssh_channel_open_session(channel);
if (rc != SSH_OK) error(session);
printf(“Executing remote command…\n”);
rc = ssh_channel_request_exec(channel, “ls -l”);
if (rc != SSH_OK) error(session);
printf(“Received:\n”);
nbytes = ssh_channel_read(channel, buffer, sizeof(buffer), 0);
while (nbytes > 0) {
fwrite(buffer, 1, nbytes, stdout);
nbytes = ssh_channel_read(channel, buffer, sizeof(buffer), 0);
}
free_channel(channel);
free_session(session);
return 0;
}
cmakelist 文件内容如下
cmake_minimum_required(VERSION 3.17)
project(${ProjectId} C)
FIND_PACKAGE(PkgConfig REQUIRED)
PKG_CHECK_MODULES(LIBSSH REQUIRED libssh)
INCLUDE_DIRECTORIES(LIBSSHINCLUDEDIRS)LINKDIRECTORIES({LIBSSH_INCLUDE_DIRS})
LINK_DIRECTORIES(LIBSSHINCLUDEDIRS)LINKDIRECTORIES({LIBSSH_LIBRARY_DIRS})
ADD_DEFINITIONS(${LIBSSH_CFLAGS})
add_executable(untitled main.c)
target_link_libraries(untitled ${LIBSSH_LIBRARIES})
msys2 终端可以编译前的依赖安装
pacman -S gcc
pacman -S libssh-devel
pacman -S libssh
测试编译 可以成功
gcc ssh.c -lssh