【问题标题】:Problems compiling an external library on linux在 linux 上编译外部库的问题
【发布时间】:2014-03-15 06:28:47
【问题描述】:

所以我试图在 linux 上编译 libssh2 库,但是当我尝试编译 例如,它出现了很多错误,即使我包含了它要求的头文件,它仍然要求它。 以下是错误消息和结果消息:

~/ gcc -include /home/Roosevelt/libssh2-1.2.5/src/libssh2_config.h -o lolbaise /home/Roosevelt/libssh2-1.2.5/example/scp.c
/home/Roosevelt/libssh2-1.2.5/example/scp.c:7:28: error: libssh2_config.h: No such file or directory
/home/Roosevelt/libssh2-1.2.5/example/scp.c: In function 'main':
/home/Roosevelt/libssh2-1.2.5/example/scp.c:39: error: storage size of 'sin' isn't known
/home/Roosevelt/libssh2-1.2.5/example/scp.c:81: error: 'AF_INET' undeclared (first use in this function)
/home/Roosevelt/libssh2-1.2.5/example/scp.c:81: error: (Each undeclared identifier is reported only once
/home/Roosevelt/libssh2-1.2.5/example/scp.c:81: error: for each function it appears in.)
/home/Roosevelt/libssh2-1.2.5/example/scp.c:81: error: 'SOCK_STREAM' undeclared (first use in this function)
/home/Roosevelt/libssh2-1.2.5/example/scp.c:87: error: invalid application of 'sizeof' to incomplete type 'struct sockaddr_in'

这是新的错误:

scp.c:(.text+0x106): undefined reference to `libssh2_init'
scp.c:(.text+0x1fe): undefined reference to `libssh2_session_init_ex'
scp.c:(.text+0x234): undefined reference to `libssh2_session_startup'
scp.c:(.text+0x288): undefined reference to `libssh2_hostkey_hash'
scp.c:(.text+0x36f): undefined reference to `libssh2_userauth_password_ex'
scp.c:(.text+0x3e7): undefined reference to `libssh2_userauth_publickey_fromfile_ex'
scp.c:(.text+0x437): undefined reference to `libssh2_scp_recv'
scp.c:(.text+0x531): undefined reference to `libssh2_channel_read_ex'
scp.c:(.text+0x5f8): undefined reference to `libssh2_channel_free'
scp.c:(.text+0x628): undefined reference to `libssh2_session_disconnect_ex'
scp.c:(.text+0x636): undefined reference to `libssh2_session_free'
scp.c:(.text+0x66e): undefined reference to `libssh2_exit'
collect2: ld returned 1 exit status

【问题讨论】:

  • 阅读消息。第一个说它找不到您包含的 libssh2_config.h 文件。如果你解决了这个问题,它很可能会解决所有其他问题。您可以通过阅读错误消息来学习很多东西,而不仅仅是举手询问其他人。

标签: c++ linux libssh


【解决方案1】:

不包含头文件:libssh2_config.h

源代码中有一个包含指令,因此您必须使用-I 选项指示标头的路径:gcc -I/home/Roosevelt/libssh2-1.2.5/src

-include 选项应用于包含一个头文件,该头文件未通过#include 指令明确包含在源代码中。

【讨论】:

  • @Kris,是的,您可以在命令行上有多个 -I 选项。它们都将被添加到被搜索的目录列表中。
  • @Kris:现在是链接版本问题。导致此新错误消息的命令是什么?您可能需要按照“-L -lssh2”的行添加一些内容。
  • 我使用了这个命令:~$ gcc -include /home/Roosevelt/libssh2-1.2.5/include/libssh2.h -o lolbaise /home/Roosevelt/libssh2-1.2.5/example/ scp.c
  • @Kris:您的可执行文件需要与 libssh2 库链接。您需要指出 ( -lssh2) 以及库文件 (libssh2.a 或 libssh2.so) 所在的路径 ( -L )。尝试: gcc -include /home/Roosevelt/libssh2-1.2.5/include/libssh2.h -o lolbaise /home/Roosevelt/libssh2-1.2.5/example/scp.c -L/home/Roosevelt/libssh2- 1.2.5/lib -lssh2
  • 现在我只得到 2 个错误: scp.c:(.text+0xfb): undefined reference to libssh2_init' scp.c:(.text+0x5bd): undefined reference to libssh2_exit' 显然只需要 -lssh2 就不会出现大量错误, -L 命令有点多余。
【解决方案2】:

我刚刚克隆了当前的 git build 并进入了 1.2.5 版本,但我无法重现您的问题。

./buildconf
./configure
make

工作正常。你到底想做什么?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-02-09
    • 1970-01-01
    • 1970-01-01
    • 2018-10-05
    • 1970-01-01
    • 2019-06-09
    • 2011-10-31
    • 1970-01-01
    相关资源
    最近更新 更多