简体   繁体   English

无法链接 Cmake 中的 libssh

[英]Unable to link libssh in Cmake

I am trying to compile C++ Qt app that needs a connection to a ssh server, but I am unable to do it, as every time I try to compile it, I am getting the following error code:我正在尝试编译需要连接到 ssh 服务器的 C++ Qt 应用程序,但我无法执行此操作,因为每次我尝试编译它时,都会收到以下错误代码:

[1/1] Linking CXX executable SSH-Manager
FAILED: SSH-Manager 
: && /usr/bin/c++ -g  CMakeFiles/SSH-Manager.dir/SSH-Manager_autogen/mocs_compilation.cpp.o CMakeFiles/SSH-Manager.dir/src/main.cpp.o CMakeFiles/SSH-Manager.dir/src/mainwindow.cpp.o CMakeFiles/SSH-Manager.dir/src/ssh_connection.cpp.o -o SSH-Manager  -Wl,-rpath,/home/<username>/Qt/6.4.1/gcc_64/lib:  -L  -lssh  /home/<username>/Qt/6.4.1/gcc_64/lib/libQt6Widgets.so.6.4.1  /home/<>username/Qt/6.4.1/gcc_64/lib/libQt6Gui.so.6.4.1  /home/<username>/Qt/6.4.1/gcc_64/lib/libQt6Core.so.6.4.1  /usr/lib/x86_64-linux-gnu/libGL.so && :
/usr/bin/ld: CMakeFiles/SSH-Manager.dir/src/ssh_connection.cpp.o: in function `SSH_connection::SSH_connection()':
/home/<username>/Programming/Projects/SSH-Manager/src/ssh_connection.cpp:11: undefined reference to `ssh_new'
.
.
.

/usr/bin/ld: /home/<username>/Programming/Projects/SSH-Manager/src/ssh_connection.cpp:17: 
collect2: error: ld returned 1 exit status
ninja: build stopped: subcommand failed.

And the relevant part of CMakeLists.txt:以及 CMakeLists.txt 的相关部分:

find_package(LIBSSH)
if (LIBSSH_FOUND)
    message(${LIBSSH_VERSION})
    include_directories(${LIBSSH_INCLUDE_DIR})
    link_directories(${LIBSSH_LIBRARY_DIR})
    target_link_libraries(SSH-Manager PRIVATE -L${LIBSSH_LIBRARY} -lssh)
else ()
    message(Unable to find libssh!)
endif ()

What have I already tried:我已经尝试过什么:

  1. purged and reinstalled openssh-server and libssh-dev清除并重新安装 openssh-server 和 libssh-dev
  2. found and verified libssh.h file找到并验证了 libssh.h 文件
  3. included LIBSSH_DIR包括 LIBSSH_DIR

What am I doing wrong?我究竟做错了什么?

If you look at what is being passed to the compiler, the error becomes pretty clear:如果您查看传递给编译器的内容,错误将变得非常清楚:

... -Wl,-rpath,/home/<username>/Qt/6.4.1/gcc_64/lib:  -L  -lssh 

As you can see -L is empty.如您所见,-L 是空的。

To fix this let's start with the correct usage of target_link_libraries , this is NOT the correct usage:要解决这个问题,让我们从target_link_libraries的正确用法开始,这不是正确的用法:

target_link_libraries(SSH-Manager PRIVATE -L${LIBSSH_LIBRARY} -lssh)

Do not pass anything like -Lxxx -lxxx to this function, the function doesn't serve as a function that passes compiler options.不要将-Lxxx -lxxx之类的任何内容传递给此 function,function 不会作为传递编译器选项的 function。 If you check the libssh site you will see the variables which are populated, ie find_package(package) populates certain variables that you then can use in your CMakeLists.txt , in your case you are mainly interested in ${LIBSSH_LIBRARIES} , also note that you shouldn't need to use link_directories() .如果您检查libssh 站点,您将看到填充的变量,即find_package(package)填充某些变量,然后您可以在CMakeLists.txt中使用这些变量,在您的情况下,您主要对${LIBSSH_LIBRARIES}感兴趣,还要注意你不应该需要使用link_directories() So instead of所以而不是

target_link_libraries(SSH-Manager PRIVATE -L${LIBSSH_LIBRARY} -lssh)
# it should look like this
target_link_libraries(SSH-Manager PRIVATE ssh) #OR ${LIBSSH_LIBRARIES}

Also bare in mind that the name of the package might be case-sensitive if you are on Linux另外请记住,如果您使用的是Linux ,则 package 的名称可能区分大小写

EDIT: The authors also have an alias, so target_link_libraries(project ssh) is sufficient.编辑:作者也有一个别名,所以target_link_libraries(project ssh)就足够了。 Check the link provided to see the correct usage of it in your project.检查提供的链接以查看它在您的项目中的正确用法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM