【问题标题】:gcc libssh linking issue on windowsWindows上的gcc libssh链接问题
【发布时间】:2018-08-02 11:52:28
【问题描述】:

我遇到了一个问题,我无法编译使用 libsshssh 客户端。 这摘自 Wil Allsopp 书。

    #include <libssh/libssh.h>
    #include <stdlib.h>
    #include <stdio.h> 
    #include <windows.h>

    int main()
    {
        ssh_session my_ssh_session;
        int rc;
        char *password;
        my_ssh_session = ssh_new();

        if (my_ssh_session == NULL)
            exit(-1):

        ssh_options_set(my_ssh session, SSH_OTIONS_HOST, "c2host");
        ssh_options_set(my_ssh_session, SSH_OTIONS_PORT, 443);
        ssh_options_set(my_ssh_session, SSH_OTIONS_PORT, "c2user");
        rc = ssh_connect(my_ssh_session);

        if (verify_knownhost(my_ssh_session) < 0)
        {
            ssh_disconnect(my_ssh_session);
            ssh_free(my_ssh_session);
            exit(-1)
        }
        password = ("Password");
        rc = ssh_userauth_password(my_ssh_session, NULL, password);
        ssh_disconnect(my_ssh_session);
        ssh_free(my_ssh_session);
    }

我试过这个命令,但没有帮助:

gcc -I C:\MinGW\lib\gcc\mingw32\6.3.0\include\libssh\ -L C:\MinGW\lib\gcc\mingw32\6.3.0\include\libssh -lssh ssh.c -o out.exe

我收到此错误:

c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../../mingw32/bin/ld.exe: 找不到 -lssh collect2.exe:错误:ld 返回 1 个退出状态

有什么想法吗?谢谢!


我猜这个库是通过以下命令找到的,但是我得到了这个错误:

gcc -I C:\MinGW\lib\gcc\mingw32\6.3.0\include\libssh\  -L 

C:\MinGW\lib\gcc\mingw32\6.3.0\lib\ -lssh ssh.c -o out.exe

ssh.c:在函数'main'中: ssh.c:35:52:警告:传递 'ssh_options_set' 的参数 3 使指针从整数而不进行强制转换 [-Wint-conversion] ssh_options_set(my_ssh_session, SSH_OPTIONS_PORT, 443); ^~~ 在 ssh.c:19:0 包含的文件中: c:\mingw\lib\gcc\mingw32\6.3.0\include\libssh\libssh.h:495:16: 注意:预期为 'const void *' 但参数类型为 'int' LIBSSH_API int ssh_options_set(ssh_session session, enum ssh_options_e type, ^~~~~~~~~~~~~~~ C:\Users\matteo\AppData\Local\Temp\ccI0eQk0.o:ssh.c:(.text+0xf): 未定义引用_imp__ssh_new' C:\Users\AppData\Local\Temp\ccI0eQk0.o:ssh.c:(.text+0x44): undefined reference to_imp__ssh_options_set' C:\Users\AppData\Local\Temp\ccI0eQk0.o:ssh.c:(.text+0x62): 未定义引用_imp__ssh_options_set' C:\Users\AppData\Local\Temp\ccI0eQk0.o:ssh.c:(.text+0x80): undefined reference to_imp__ssh_options_set' C:\Users\AppData\Local\Temp\ccI0eQk0.o:ssh.c:(.text+0x8e): 未定义引用_imp__ssh_connect' C:\Users\AppData\Local\Temp\ccI0eQk0.o:ssh.c:(.text+0xb8): undefined reference to_imp__ssh_userauth_password' C:\Users\AppData\Local\Temp\ccI0eQk0.o:ssh.c:(.text+0xca): 未定义引用_imp__ssh_disconnect' C:\Users\AppData\Local\Temp\ccI0eQk0.o:ssh.c:(.text+0xd8): undefined reference to_imp__ssh_free' collect2.exe:错误:ld 返回 1 个退出状态

【问题讨论】:

  • -LC:\MinGW\lib\gcc\mingw32\6.3.0\include\libssh 应该是:-LC:\MinGW\lib\gcc\mingw32\6.3.0\lib\libssh
  • 你找到答案了吗?

标签: c windows gcc mingw


【解决方案1】:

你应该保持清醒的头脑。编译完 libssh 后,这里有三个目录。 lib、include 和 bin。将 gcc 与选项一起使用:-Iinclude -Llib -lssh。这告诉编译器你的程序的包含目录和 lib 目录。最后一个选项是告诉编译器您使用哪个 .a 文件。这应该编译成功。然而,你永远不知道这条路你应该走多远。当我执行我编译的文件时,它没有输出任何东西。所以,我使用 gdb 来调试发生了什么。这是我发现的。

[New Thread 1932.0xec0]
[New Thread 1932.0x918]
[New Thread 1932.0x13dc]
[New Thread 1932.0x4f8]
[Thread 1932.0x918 exited with code 3221225781]
[Thread 1932.0x4f8 exited with code 3221225781]
During startup program exited with code 0xc0000135.

经过搜索,我发现原因是缺少dll文件。所以,我将 libssh.dll 文件复制到我的程序所在的目录中。然后,同样的问题。最后,我使用dependency walker 工具找出我需要的所有dll 文件。结果是,libzlib.dll、libcrypto-1_1-X64.dll 文件。我只是将 libzlib.dll 文件添加到我的程序目录中。它运行良好。我猜我的程序没有使用 libcrypto 中的函数。也许你也应该添加它们。

【讨论】:

  • 我搜索了很多,查看了很多你的帖子。最后,我已经完成了,经常看你的问题感到很无聊。然后我注册了这个网站来回答你……也许这是一种习惯。(×_×)
猜你喜欢
  • 1970-01-01
  • 2010-09-17
  • 2012-06-29
  • 2012-01-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多