SSH with Cocoa (Xcode and libssh2)

I fought with this about a year ago, and for some strange reason never managed to get things to compile or link. I chalk this down now to my lack of understanding with Objective-C/linking concepts. However it turns out that it is relatively simple (ensure you have Xcode 4 installed before trying).

  1. Point browser to http://www.libssh2.org/ and download the latest snapshot to a temporary location.
  2. Open a terminal window and navigate to the directory containing the the source files and run the following:
    dan$ ./configure
  3. This will output numerous content to the terminal window, present a summary of the configuration options and create a header file needed for compilation. (Running make / make install is NOT required).
  4. Open Xcode and create a new Xcode project, which should be a (Mac OS X -> Framework & Library -> C/C++ Library) and give it a Product Name (e.g. libssh2) and ensure that the type is Static then click create.
  5. Xcode will open with an empty project displaying the Build Settings. At this point we can start adding the files that are part of the libssh2 source tree.
  6. Use Finder and navigate to the directory containing the libssh2 files, inside the ./src directory and sort the files by type and select all of the files “C source“, back in Xcode in the project plan navigate to Build Phases and expand the Compile Sources area. Drag all of the C Source files into the Compile Sources area (should be 22 items), the Compile Sources area can be closed now and the Copy Headers area should be opened.
  7. From the ./src directory drag the all of the “C header source” into the headers area (Private) in Xcode, back in Finder navigate out of the ./src directory and into the ./include directory and drag all of the headers into the Xcode headers area (public).
  8. Finally Close the Copy headers area (should be 17 items), and open the Link Binary with Libraries area and press the+ button at the bottom. Search for for libz.dylib and libcrypto.dylib, these HAVE to be added otherwise linking won’t work.
  9. Pressing ⌘B will build the library, now there will be about 448 errors, however 50% are #define warning regarding deprecated features and the rest are conversations warnings from STD C variables to obj-c variables.
  10. I’d recommend tidying up all of the added files into groups as otherwise managing the files will be a mess e.g: 
  11. Next step will be to add the libssh2 library to another project and use ssh functionality (to be continued) 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *