Sunday, July 7, 2013

Build libssh2 on Visual Studio 2010

//

In this post I'd like to show you few easy steps how to build libssh2 library with Microsoft Visual Studio 2010.

Recently I started a project which require SSH connection to a server. I've tried few libraries which provide SSH support and libssh2 seems to feet my need the best. It's portable, easy to work with (even the documentation is not great) and has a SSH client support.

Unfortunately the libssh2 comes only as a source code, but fortunately it's very easy to build. The setup I'm using is Microsoft Visual Studio 2010 on Windows 7 64bit.

Even I'm on 64 bit machine I'll build 32 bit version of libssh2. It's easier that way.

Download the sources


First we have to download the sources. The current version is 1.4.3 and can be downloaded from libssh2 website here.

Next in Visual Studio, File->Open->Project/Solution and navigate to libssh-1.4.3\win32. Open libssh2.dsw. The Visual Studio will ask to convert it to 2010 workspace file format. Say YES.

After the it is converted there should be 2 projects in the workspace:

  • libssh2 - the library

  • tests - asimple test application which uses the libssh2 library file created by libssh2 project.


Additional libraries needed



  • openssl can be downloaded from here

  • zlib an be downloaded from here


Both have to be 32 bit as we are going to build 32 bit version of the libssh2.

Build


First thing first - build the library



  1. On the libssh2 project properties C/C++->General->Additional Include Directories add '<path to openssl>\include'.

  2. Go to libssh2_priv.h(43) and comment out the line [sourcecode language="plain"]#define LIBSSH2_LIBRARY[/sourcecode]

  3. libssh2 project properties C/C++->Preprocessor->Preprocessor Definitions and add 'LIBSSH2_LIBRARY'.


 Build the 'test' project

  1. test project properties Linker->General->Output File replace with '%(OutputFile)'

  2. test project properties Linker->General->Additional Library Directories add:

    • '<path to openssl>\lib'

    • '<path to zlib>\static32'. Make sure you rename 'zlibstat.lib' to 'zlib.lib' within this folder as we reference to 'zlib'lib.

    • <path to libssh2.lib>. In my case it is '.\Debug_lib'.



  3. test project properties Linker->Input->Additional Dependencies add zlib.lib and libeay32.lib.

  4. test project properties Debugging->Command and change to '$(OutputPath)\$(TargetFileName)'

  5. test project properties Debugging->Working Directory and change to '$(OutputPath)'

  6. Now if you build you'll probably get:


[sourcecode language="plain"]
simple.obj : error LNK2019: unresolved external symbol __imp__libssh2_exit referenced in function _main
simple.obj : error LNK2019: unresolved external symbol __imp__libssh2_session_free referenced in function _main
simple.obj : error LNK2019: unresolved external symbol __imp__libssh2_session_init_ex referenced in function _main
simple.obj : error LNK2019: unresolved external symbol __imp__libssh2_init referenced in function _main
simple.obj : error LNK2019: unresolved external symbol __imp__libssh2_base64_decode referenced in function _test_libssh2_base64_decode
[/sourcecode]

Go to 'test' project properties C/C++->Preprocessor->Preprocessor Definitions and remove 'LIBSSH2_WIN32'

That's it. Build and Enjoy.

Subscribe for our NewsLetter!

13 comments:

  1. Hello,

    >> zlib an be downloaded from >>here<< <-- link to zlib binaries is invalid and referes to libssh sources.

    Thanks for the article!

    ReplyDelete
  2. A few minor corrections I noticed (using VS2013 if that makes a difference):

    1) It should be 'zlibstat.lib' not 'zlib.lib'
    2) Command can be: $(TargetPath)
    3) Directory to run from should be: $(ProjectDir)\Release (if running in release)

    ReplyDelete
  3. Thanks for the information!

    ReplyDelete
  4. i can't open it in vs2010 it says that libssh2 is targeted to .net framework version =V4
    but i installed .net framework version 4.0.3019 can you help me

    ReplyDelete
  5. This comment has been removed by the author.

    ReplyDelete
  6. Thanks for the excellent post!

    I was able to build both projects successfully. However, I get an "error LNK1104: cannot open file 'libssh2d.lib' " if I try to build the 2 projects together as a solution.

    I get this even after adding the Debug_lib library which has the libssh2d.lib file in it.

    Just wanted to know if you see something similar if you build solution instead of individual projects.

    ReplyDelete
    Replies
    1. You may need to add "libssh2d.lib":
      Linker->Input->Additional Dependencies. (and possibly "ws2_32.lib", if you get winsock related link errors).
      Also make sure you have "Debug_lib" added in:
      Linker->General->Additional Library Directories

      Delete
    2. Right, I was missing that. Thanks!

      Delete
  7. Hi Daniel. This is exactly the issue I am facing. Were you able to resolve it on your end? Thanks.

    ReplyDelete
  8. Please can you tell , how to build libssh2 for 64 bit

    ReplyDelete
    Replies
    1. Did you try to build it and link it vs. 64bit version of the libraries?
      BTW. Whay you need 64bit?... 32 should be sufficient in most cases.

      Delete
  9. solved it finally. It worked out for me to set multiple start projects in the workspace. I set the libssh2 as start project with no action and the tests project with the Starting action.

    ReplyDelete
  10. Not sure exactly what is the case, but it looks like you try to build Release version of the executable (using libssh2.lib), but you have build debug version of the library libssh2d.lib. Try to build the executable debug version, or provide release version of the libssh library.

    ReplyDelete