如何链接一个简单的libssh程序 [英] How to link a simple libssh program

查看:220
本文介绍了如何链接一个简单的libssh程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

程序:

#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;
}

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

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"代码,包括以下内容:

    #ifdef __cplusplus
    extern "C" {
    #endif
    ...
    LIBSSH_API ssh_session ssh_new(void);
    ...
    LIBSSH_API void ssh_free(ssh_session session);
    ...
    #ifdef __cplusplus
    }
    #endif
    

  • 链接器成功找到该库. (通过ProcessMonitor程序检查.它肯定是在访问libssh.dll.a文件.)

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

  • 的引用
  • 检查32位/64位问题之间没有混淆:提供了两个版本的预编译库.一个是...\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,但是后者不给我这样的错误,因此我认为libssh.dll.a文件是要使用的正确库文件./p>

  • 检查了有关libssh2的非常相似的问题中给出的建议,有关库的顺序链接,但是在这里对g ++的调用看起来很正确,因为libssh.dll.a参数恰好位于结尾处.

  • 检查了

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

  • 试图(未成功)编译libssh的较新版本.

解决方案

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

Problem: getting a simple program using the libssh library to compile successfully without getting a linker error.

Program:

#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;
}

Machine is 64-bit, running Windows. Using MinGW (via CodeBlocks) and the build log (edited slightly for readability) shows the compiler command as:

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))

Things I have tried/investigated

I understand that the undefined reference to refers to the linker being able to find the declaration of the functions, but not their definitions.

解决方案

The libssh is not linked. The correct way to do this is to copy the libssh directory (which contains the headers) under the include directory of you Code Blocks installation. and copy the .dll.a file under the lib directory of your CodeBlocks installation. Also delete the #define LIBSSH_STATIC 1 line if you are going to do it this way.

这篇关于如何链接一个简单的libssh程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆