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
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
- On the libssh2 project properties C/C++->General->Additional Include Directories add '<path to openssl>\include'.
- Go to libssh2_priv.h(43) and comment out the line [sourcecode language="plain"]#define LIBSSH2_LIBRARY[/sourcecode]
- libssh2 project properties C/C++->Preprocessor->Preprocessor Definitions and add 'LIBSSH2_LIBRARY'.
Build the 'test' project
- test project properties Linker->General->Output File replace with '%(OutputFile)'
- 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'.
- test project properties Linker->Input->Additional Dependencies add zlib.lib and libeay32.lib.
- test project properties Debugging->Command and change to '$(OutputPath)\$(TargetFileName)'
- test project properties Debugging->Working Directory and change to '$(OutputPath)'
- 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!