CopperSpice Overview
Shared Libraries

There are two main ways of linking applications: Static Linking and Shared Libraries.

Static Linking

Static linking results in a stand alone application with the executable and all libraries linked into one large file. An advantage to this approach is having fewer files to deploy. However, there are more disadvantages. Executables are larger and there is less flexibility when deploying updates to the application.

Shared Libraries

Shared libraries provide smaller executables and this is the approach used with CopperSpice. Developers can release upgrade the CopperSpice libraries used by the application, independently of updating the application. This works when libraries are binary compatible.

Another reason to use the shared library approach is if a developer wants to use the same libraries for several applications. T

If you are creating and deploying your own plugins, the shared library approach is required.

Exporting Symbols

Symbols (functions, variables or classes) contained in shared libraries intended to be used by other applications or other libraries, must be marked as exported. The unexported symbols should not be visible from outside the shared library. On most platforms, compilers will hide them by default. On some platforms a special compiler option may be required to hide these symbols.

Depending on your target platform, CopperSpice provides special macros which contain the necessary definitions:

  • Q_DECL_EXPORT must be added to the declarations of symbols used when compiling a shared library
  • Q_DECL_IMPORT must be added to the declarations of symbols used when compiling a client that uses the shared library

The second step involves creating a special header. This header must be created for usage of a shared library from another application or another library. The purpose of this special header is to ensure the correct macro is invoked.

As an example, given a shared library called mySharedLib. A special header for this library, mysharedlib_global.h, might look like the following:

#include <QtGlobal>
#if defined(BUILD_MYSHAREDLIB)
# define MYSHAREDLIB_EXPORT Q_DECL_EXPORT
#else
# define MYSHAREDLIB_EXPORT Q_DECL_IMPORT
#endif

In the Makefile.am used to build the shared library add the following to your CXXFLAGS.

-DBUILD_MYSHAREDLIB


In each header of the shared library specify the following:

#include "mysharedlib_global.h"
MYSHAREDLIB_EXPORT void someFunc();
class MYSHAREDLIB_EXPORT MyClass
{
};