如何链接一个简单的libssh程序

时间:2017-01-20 12:00:40

标签: c++ libssh

问题:使用libssh库获取一个简单的程序,以便成功编译而不会出现链接器错误。

程序:

Realm

计算机是64位,运行Windows。使用MinGW(通过CodeBlocks)和构建日志(为了便于阅读而略微编辑)将编译器命令显示为:

#include <iostream>

#define LIBSSH_STATIC 1
#include <libssh/libssh.h>
#include <stdlib.h>

void sftpSession()
{
    ssh_session test_session = ssh_new();
    if (!test_session) {
        throw std::runtime_error("Could not create a session?!");
        return;
    }
    ssh_free(test_session);
    std::cout << "Session created & freed successfully." << std::endl;
}

int main(int argc, char **argv)
{
    std::cout << "SFTP test program" << std::endl;
    try {
        sftpSession();
    }
    catch (const std::runtime_error &e) {
        std::cout << "thrown: " << e.what() << std::endl;
    }
    return EXIT_SUCCESS;
}

我尝试/调查过的事情

我理解x86_64-w64-mingw32-g++.exe -Wall -fexceptions -O2 -std=c++0x -g -m64 -Dmine_dev=1 -I"C:\Program Files\zlib\zlib-1.2.8-dll\include" -I"C:\Program Files (x86)\libssh\libssh-0.6.5\include" -c C:\Users\ ... \SFTP_testing\main.cpp -o obj\Release\main.o x86_64-w64-mingw32-g++.exe -L"C:\Program Files (x86)\Ingres\IngresII\ingres\lib" -o bin\Release\SFTP_testing.exe obj\Release\main.o -s "C:\Program Files (x86)\libssh\libssh-0.6.5\lib\libssh.dll.a" obj\Release\main.o: In function `sftpSession()': C:/Users/ ... /SFTP_testing/main.cpp:9: undefined reference to `ssh_new' C:/Users/ ... /SFTP_testing/main.cpp:14: undefined reference to `ssh_free' collect2.exe: error: ld returned 1 exit status Process terminated with status 1 (0 minute(s), 2 second(s)) 3 error(s), 0 warning(s) (0 minute(s), 2 second(s)) 是指链接器能够找到函数的声明,而不是它们的定义。

  • 编译器确实找到了头文件和函数的定义。

  • 头文件确实有必要的undefined reference to代码,包括以下内容:

    extern "C"
  • 链接器成功找到库。 (通过ProcessMonitor程序检查。它肯定是访问#ifdef __cplusplus extern "C" { #endif ... LIBSSH_API ssh_session ssh_new(void); ... LIBSSH_API void ssh_free(ssh_session session); ... #ifdef __cplusplus } #endif 文件。)

  • 使用nm实用程序,我检查了libssh.dll.a中的对象代码确实包含libssh.dll.assh_new的引用

  • 检查32位/ 64位问题之间没有混淆:提供了两个版本的预编译库。一个是ssh_free,另一个是...\libssh-0.6.5\bin\libssh.dll,前者给出了以下错误:...\libssh-0.6.5\lib\libssh.dll.a但后者没有给出错误,所以我假设i386 architecture of input file 'C:\Program Files (x86)\libssh\libssh-0.6.5\bin\libssh.dll' is incompatible with i386:x86-64 output文件是要使用的正确库文件。

  • 检查了this very similar question about libssh2中关于链接库顺序的建议,但是这里对g ++的调用看起来正确,因为libssh.dll.a参数在最后。

    < / LI>
  • 检查more general advice about linker errors,但找不到任何适用的内容。

  • 在重建之前清理,以避免“死木”的影响。

  • 尝试了其他旧版本的库。相同的错误消息。

  • 尝试(未成功)编译较新版本的libssh。

1 个答案:

答案 0 :(得分:0)

libssh未链​​接。正确的方法是复制代码块安装的include目录下的libssh目录(包含头文件)。并将.dll.a文件复制到CodeBlocks安装的lib目录下。 如果要这样做,也要删除#define LIBSSH_STATIC 1行。

相关问题