请教老师 问题 msys2 终端可以编译 clion 不可以编译
来源:5-1 文件包含【领略编译器宏之以小搏大】

完善777
2021-03-10
老师我有一个 源码文件 在 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
5回答
-
完善777
提问者
2021-03-11
还是不行
012021-03-11 -
bennyhuo
2021-03-11
mingw 和 msys 是两个不同的东西,你用 msys 下面的 gcc 和 mingw 是没有关系的。msys 其实是从cygwin简化过来的一个类 Unix 环境,mingw 则是基于 windows 环境提供的 Posix 兼容层。
这个问题表明你的 mingw 当中没有安装 libssh,需要安装的话,在 msys 的命令行(就是你截图里面编译用的命令行)当中,运行:pacman -S mingw-w64-x86_64-libssh
安装好 libssh 以后,再在 CLion 当中运行就可以了。
00 -
完善777
提问者
2021-03-11
还是不行
00 -
bennyhuo
2021-03-11
照着我这个改一下你的 CMakeList 文件:
FIND_PACKAGE(PkgConfig REQUIRED)
PKG_CHECK_MODULES(LIBSSH REQUIRED libssh)
INCLUDE_DIRECTORIES(${LIBSSH_INCLUDE_DIRS})
LINK_DIRECTORIES(${LIBSSH_LIBRARY_DIRS})
ADD_DEFINITIONS(${LIBSSH_CFLAGS})
add_executable(HelloWorldC master.c)
target_link_libraries(HelloWorldC ${LIBSSH_LIBRARIES})00 -
bennyhuo
2021-03-11
你没用 mingw 编译器编译啊,你截图里面用的是 msvc 吧。。。
00
相似问题