2

I have the following error when trying to compile a c++ program while linking the library Libssh on Windows (packages libssh-common and libssh-devel both installed with cygwin). Clion doesn't give me an include error and cmake finds the library during the Cmake reload of Clion, but when compiling/linking it complains that the references are undefined.

Could someone point out my stupid mistake? Thanks in advance!

Scanning dependencies of target main
[ 50%] Building CXX object CMakeFiles/main.dir/main.cpp.o
[100%] Linking CXX executable main.exe
CMakeFiles/main.dir/main.cpp.o:main.cpp:(.text+0x30): undefined reference to `ssh_new'
CMakeFiles/main.dir/main.cpp.o:main.cpp:(.text+0x30): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `ssh_new'
collect2: error: ld returned 1 exit status

my CmakeLisst.txt

add_executable(main main.cpp)
find_package(LIBSSH)
IF (LIBSSH_FOUND)
    message(${LIBSSH_VERSION})
    include_directories(${LIBSSH_INCLUDE_DIR})
    link_directories(${LIBSSH_INCLUDE_DIR})
    target_link_libraries(main ${LIBSSH_LIBRARIE})
endif ()

output Cmake reload

C:\Users\seven\.CLion2018.3\system\cygwin_cmake\bin\cmake.exe -DCMAKE_BUILD_TYPE= -DCMAKE_MAKE_PROGRAM=C:/cygwin64/bin/make.exe -DCMAKE_C_COMPILER=C:/cygwin64/bin/gcc.exe -DCMAKE_CXX_COMPILER=C:/cygwin64/bin/g++.exe -G "CodeBlocks - Unix Makefiles" "/cygdrive/c/Users/seven/Documents/github/Server control"
0.7.5
-- Configuring done
-- Generating done
-- Build files have been written to: /cygdrive/c/Users/seven/Documents/github/Server control/cmake-build-default-cygwin

[Finished]

main.cpp

#include <stdlib.h>
#include <iostream>
#define LIBSSH_STATIC 1
#include <libssh\libssh.h>

int main() {
    std::cout << "Hello world" << std::endl;
    ssh_session my_ssh_session = ssh_new();
    return 0;
}

--update 1-- CmakeOutput.log github gist

--SOLUTION----

Had to change my cmakelist to the following

find_package(LIBSSH)
IF (LIBSSH_FOUND)
    message(${LIBSSH_VERSION})
    include_directories(${LIBSSH_INCLUDE_DIR})
    link_directories(${LIBSSH_LIBRARY_DIR})
    target_link_libraries(main -L${LIBSSH_LIBRARY} -lssh)
endif ()
  • Are you completely positive the LIBSSH_FOUND part in CMakeLists.txt is executed? Try to inspect CMakeOutput.log and CMakeError.log for making sure and possible helpful hints – valiano Jan 5 '19 at 19:31
  • 1
    @valiano Well, it manages to print out the correct version number, so I thought the block executed. I tried to find CMakeError.log but it isn't present in my project files. I don't find any weird entries in CmakeOutput.log but I find nothing about libssh either. I will give a github gist – tibovanheule Jan 5 '19 at 20:02
  • Thanks for the gist Tibo_Vanheule. It's difficult for me to say whether libssh is indeed picked. You can check whether it's actually linked against with: run make VERBOSE=1 from the command line, where your project Makefile is located (possibly change one source file to force the build). Then, look for the ld command line in the build output. – valiano Jan 5 '19 at 20:19
  • Okay, so after running make VERBSOE=1. I looked in the output and didn't found the ld command. make verbose output .Thanks for the answer! – tibovanheule Jan 5 '19 at 21:13
0

Judging from make VEBOSE=1 output, the libssh library is indeed not linked against.

Try fixing these lines in your CMakeLists.txt:

link_directories(${LIBSSH_LIBRARY_DIR})
target_link_libraries(main ${LIBSSH_LIBRARY})
  • The parameter to link_directories should be LIBSSH_LIBRARY_DIR, rather than LIBSSH_INCLUDE_DIR.
  • The parameter to target_link_libraries should be LIBSSH_LIBRARY, rather than LIBSSH_LIBRARIE.

Or, alternatively:

link_directories(${LIBSSH_LIBRARY_DIR})
target_link_libraries(main -L${LIBSSH_LIBRARY_DIR} -lssh")
| improve this answer | |
  • Thank you, it fixes the link, but gave new error, sadly! make[2]: *** No rule to make target '/usr/lib/cygssh.dll', needed by 'main.exe'. Stop. new verbose gist and yes i installed all openssh related packages – tibovanheule Jan 6 '19 at 15:24
  • Hmm... You get cygssh.dll, but I think you're really looking for libssh.a. What if you use target_link_libraries(main -L${LIBSSH_LIBRARY_DIR} -lssh")? Or, even hard code the path to libssh.a, if nothing else works. – valiano Jan 6 '19 at 19:05
  • It now compiles perfectly, Thank you very much for helping me! – tibovanheule Jan 6 '19 at 21:50
  • @tibovanheule gladly! – valiano Jan 7 '19 at 6:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.