PTypes: deploying

Top: Deploying the shared (dynamic) library

Static vs. dynamic linking

PTypes builds two separate versions of the library: static and shared (DLL on Windows), giving you a choice, and at the same time putting into a dilemma.

Both static and dynamic linking have their advantages and disadvantages. While small applications consisting of a single executable would prefer to link the library directly, more complex projects with multiple dynamic components and executables would greatly benefit from going 'totally dynamic', including generic low-level libraries, such like CRTL and PTypes. Before making a decision, consider the following:

Advantages of static linking in general:

- Faster run-time loading (in return for slower compilation).
- The installation process is simpler: the end-user doesn't have to deal with package dependencies, DLL/so versioning conflicts with other applications, etc. A simple application may run even without any special installation procedures.
- Windows-specific: turning a static library into a DLL on Windows is not as simple as on UNIX systems. You should at least put special compiler directives against every public symbol in the library and thus make your header files less readable.
- UNIX-specific: for security reasons, UNIX has strict policies with regard to finding shared libraries and managing allowed source paths. In most cases you will have to provide an automated installation procedure and require root privileges in order to place all shared libraries in the right place. Remember, UNIX is a true multiuser system, and often the user that downloaded/purchased your program does not have root access on his computer, and he doesn't want to bother the system administrator either. You may, of course, override LD_LIBRARY_PATH, but then shared libraries won't be truly shared if they are installed somewhere under user's home directory.

Advantages of dynamic linking in general:

- Faster compilation and better modularization in big projects with distributed development (in return for slower run-time loading).
- Bug fixes and improvements in library's implementation do not affect your application and do not require you to recompile everything. What's more, adding new features without changing the existing interfaces in the dynamic library, again, won't affect the other modules.
- Smaller executables; dynamic libraries may be shared not only between the components of your project, but even between applications from different vendors. (The installation package should offer all dependent libraries anyway.) Freeware libraries perfectly suit for sharing between vendors, since they usually don't put any limitation on their usage, at least when linked dynamically.
- Dynamic libraries can be loaded and bound to the main application at run-time allowing you to maintain functional 'drivers' (e.g. an application that extracts plain text from various text formats can support a generic text extraction interface; drivers for each format can be developed and shipped independently).

In summary, dynamic linking is good (1) for big projects, (2) if the library is widely used by many software vendors in its dynamic (shared) form or (3) to take advantage of run-time binding.

Using and deploying UNIX shared object

PTypes builds a shared object named libptypes.so.20 (libptypes.20.dylib on MacOS X), creates a 'default' symbolic link to this library and finally places them both in so/.

If you decided to link your program against PTypes shared object instead of the static library, then all you'd have to do is to change the library path in your makefile from -L../ptypes/lib to -L../ptypes/so. When running the program, it will require the shared library to be either in one of the default locations (usually /usr/lib and /usr/local/lib), or you will have to override the LD_LIBRARY_PATH environment variable and point to the directory where the shared object resides, e.g. ~/ptypes/so.

The shared object libptypes.so.20 should be deployed and installed along with your application. The version number '20' will change when PTypes adds a number of new features and becomes bigger and/or if incompatible changes take place which make it impossible for older programs to use the new shared object.

Using and deploying Windows DLL

The PTypes MSVC project is configured so that the 'release' version of ptypes20.dll along with its import library is copied into so\ as the final step of the build process. This module does not contain any debugging information and is ready to be deployed. Note that the library itself is linked against the multithreaded DLL version of CRTL.

PTypes places a VERSION resource in the DLL, allowing the system or the setup program to automatically compare the existing ptypes20.dll the user may have in the system with the one that comes with your program. This is usually done from within the installation script.

The DLL version of the library built with Dev-C++/MinGW is called ptypes20g.dll. The reason this name is different is that each C++ compiler uses its own `name mangling' scheme for exported symbols, so that applications built by one compiler can't use dynamic libraries built by another one.

Version checking sample code

If, for some reason, you wish to check the version of the shared library you linked with dynamically, you may check the global variable __ptypes_version, which is declared in <pport.h>. This variable holds the version number encoded as a single integer, e.g. 0x010902 designates version 1.9.2. In this form the version numbers (required and actual) can be easily compared as integers.

Note, the name of the library itself reflects major and minor version numbers, so that only the third component of the version number can vary in the file.

If you need to check the version of PTypes before loading the library (for example, during the installation on UNIX), you may write a program that loads the shared object and reads the global symbol __ptypes_version at run-time, using the system dynamic loading interface. Note that this program itself is not using PTypes. Some UNIX systems require to link the program with -ldl.

>     #ifdef WIN32
>     #  include <windows.h>
>     #else
>     #  include <dlfcn.h>
>     #endif

>     #include <stdio.h>

>     const unsigned long required = 0x020001;

>     int main()
>     {
>         void* handle;
>         unsigned long* pversion;
>         int exitcode = 0;

>     #ifdef WIN32
>         const char* libname = "ptypes20.dll";
>         handle = LoadLibrary(libname);
>         if (handle == 0)
>         {
>             printf("ptypes20.DLL not found\n");
>             return 3;
>         }
>         pversion = (unsigned long*)GetProcAddress(HMODULE(handle), "__ptypes_version");
>     #else
>         const char* libname = "libptypes.so.20";
>         handle = dlopen(libname, RTLD_LAZY);
>         if (handle == 0)
>         {
>             printf("%s\n", dlerror());
>             return 3;
>         }
>         pversion = (unsigned long*)dlsym(handle, "__ptypes_version");
>     #endif

>         if (pversion == NULL)
>         {
>             printf("Couldn't determine the version number of %s\n", libname);
>             exitcode = 1;
>         }
>         else
>         {
>             printf("Found %s, version: %ld.%ld.%ld\n", libname,
>                 (*pversion) >> 16, ((*pversion) >> 8) & 0xff, (*pversion) & 0xff);
>             if (*pversion < required)
>             {
>                 printf("Need version %ld.%ld.%ld or later\n",
>                     required >> 16, (required >> 8) & 0xff, required & 0xff);
>                 exitcode = 2;
>             }
>         }

>     #ifdef WIN32
>         FreeLibrary(HMODULE(handle));
>     #else
>         dlclose(handle);
>     #endif

>         return exitcode;
>     }

See also: Compiling and Porting

PTypes home