Linux, C++, and Libraries like myHTML

Whenever you write c++ code for a project, have you ever wondered where the actual implementations for iostream’s << and >> operators are?  What about the implementations for all of the algorithms that are in the algorithm header? Whenever you’re building a project and linking in things from external libraries, where’s the compiled version of those functions?  The secret is in libraries of code external to your project.

Often times when coding, you may want to use libraries from third party sources other than the Standard lib.  One example is the MyHTML parsing library available on GitHub.  It isn’t just a header file that you include in your project and that gets built when you build your project.  It is meant to be built as an external library and used with static or dynamic linking.

Building MyHTML Library

Here’s an example walk-through of incorporating MyHTML parser into a project in the CSE 2341 Virtual Machine (tested with the Fall 2018 VM only).  All of the steps below are to be done inside the VM.

  1. Go to the MyHTML github repo and either clone the repository in your vm (git clone) or choose the download zip option. DO NOT clone it or download it to your repo for the class. If you download the zip file, unzip it.  For the purposes of the walk-through, I’ll assume you downloaded it to /home/student/Downloads.
  2. open a Terminal and navigate to the root folder of the myhtml library, probably something like cd /home/student/Downloads/MyHTML. The name of the MyHTML folder may be different… it could be something like myhtml-master.
  3. Type make and press enter.  This will build the library.  It built without incident on the standard VM for Fall 2018 CSE 2341.  If you’re using a different platform or have made significant changes to the VM, your mileage may vary. During the build process, you shouldn’t see anything that looks like an error.

Note that there is normally another command you’d use to “install” the library into the system include paths, but we aren’t going to do that right now.  The command is sudo make install.  However, we’re just going to use the lib from the MyHtml folder where you built it.

Incorporating MyHTML into your Project

When you build your project, you’ll need to tell QtCreator to build and link against the MyHTML library.

  1. In Qt Creator, right click on the project name and choose Add Library.
  2. In the Add Library Dialog, choose External Library then click Next.
  3. Next to Library File, click Browse.  Navigate to the lib folder that is inside the myHTML folder.  It is likely at /home/student/Downloads/myhtml/lib. Choose the file named libmyhtml.so.  Click next and then finish to complete the addition.
  4. Wherever needed, you can include the myhtml api.h file #include <myhtml/api.h> and the compiler should be able to find it.

At this point, the example main method on the MyHTML github’s project page should build and execute.  If you create a new project to test myhtml in, don’t forget to add it to this new project as well.  However, you don’t need to do the build steps again.

 

Speak Your Mind

*

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.