skip navigation
URC Consortium Logo

You are here: MyURC.org > Tools & Prototypes > UCHe > tutorial > Creating a Dynamic Library

How to Create a Dynamic Library for UCHe

UCHe is responsible for loading and using the external shared libraries created by the developers. UCHe, which is written in C++, uses dlopen, dlsym, dlclose for loading external shared library. The fact that the dlopen API was written with C, loading a library with C++ is a bit complicated. The developer should follow the following steps for creating a UCHe compatible shared library.

  1. Main class of the shared library should be derived class of UCHe Interface class.

    For example, for creating TA library, the main class of shared library should be derived class of ITA.

    #include “ITA.h”
    
    class exampleTA : public ITA{
    };
  2. As mentioned above, UCHe is using dlopen for loading the shared library. UCHe is using create_control function as interface function for loading shared library object and symbol. In order to use “C language” dlopen, the keyword “extern C” should be used for this function. It is also important to note that keyword “extern C” cannot be used for member function of shared library class as this give problem called name mangling. As result, the function create_function should be outside the main class.

    For above example,

    #include “ITA.h”
    class exampleTA : public ITA{
    };
    
    extern "C" ITA* create_control () {
        return new exampleTA;
    }
  3. Interface class is required as bridge between shared library and UCHe. The Interface class is using namespace "org::myurc::uch". Hence namespace "org::myurc::uch" should be used in shared library file.

    For above example,

    #include “ITA.h”
    using namespace org::myurc::uch;
    
    class exampleTA : public ITA{
    };
    
    extern "C" ITA* create_control () {
        return new exampleTA;
    }
  4. For deleting the shared library object, UCHe calls the destroy function. Complete shared library header file should include the following:

    #include “ITA.h”
    using namespace org::myurc::uch;
        
    Class exampleTA : public ITA{
    };
        
    extern "C" ITA* create_control () {
        return new exampleTA;
    }
        
    extern "C" destroy (ITA*ta) {
        delete ta;
    }
  5. While compiling the external shared library, it is required to share the symbols with UCHe. The following things should be done while compiling shared library for proper sharing of symbols.

    I. CPPFLAGS should be

    –Wl-export-dynamic
    for sharing the symbols with UCHe.

    II. Reference for UCHe header files should be given. The following header folders reference should be given for compilation:

    • uche/include/org/myurc/uch
    • uche/include/edu/wisc/trace/uch
    • uche/include/edu/wisc/trace/uch/resource
    • uche/include/edu/wisc/trace/uch/util
    • uche/include/edu/wisc/trace/uch/socket
    • Library “dl” should be included.

    Please see sample Makefile.am and configure.in for further reference.

  6. Sample Makefile.am and configure.in file for compiling shared library.
        #Makefile.am
        #This is using libtool for creating the shared library used by UCHe.
        
        lib_LTLIBRARIES =libExample.la
        libExample_la_SOURCES = \
        ./exampleTA.cc #complete path must be included
        INCLUDES = \
        ./uche/include/org/myurc/uch \
        ./uche/include/edu/wisc/trace/uch \
        ./uche/include/edu/wisc/trace/uch/resource \
        ./uche/include/edu/wisc/trace/uch/util \
        ./uche/include/edu/wisc/trace/uch/socket \
        ./include/exampleTA.h #complete path must be written
    
    
        libExample_CPPFLAGS = -Wl-export-dynamic
        LDADD = -ldl #-L must be use to specify the folder for dl library
    
        #configure.in
        AC_INIT
        AM_INIT_AUTOMAKE(Example,1.0) #1.0 must be replaced by your version no.
        AC_PROG_CC
        AC_PROG_LIBTOOL
        AC_OUTPUT(Makefile)
        
        #Various other checks can be included in configure.in.
        

Last update: Parikshit Thakur &Team, 2008-12-03

This site is maintained by the University of Wisconsin Trace Center, a member of the Universal Remote Console Consortium.