Home:ALL Converter>Common ways to use a Qt shared library with an application and cmake

Common ways to use a Qt shared library with an application and cmake

Ask Time:2013-04-29T22:06:25         Author:swaechter

Json Formatter

At the moment I have an application and a (static) library, both written in Qt. As build system I use cmake. I link the library with target_link_libraries to the executable and everything works fine (and I have one file/executable).

But now I want to use a shared library. Creating a shared library with add_library and the keyword SHARED works, but I am a bit confused about the way, how to load a shared library. I saw two "ways":

  1. Write a library and use Q_DECL_EXPORT / Q_DECL_IMPORT. They load the library with QLibrary and use a typecast to resolve all (C) functions
  2. Write a library and include the header file in the application. The shared library is "passed" to the executable with the cmake command target_link_libraries. They don't use QLibrary to load the library.

Now I have a few questions:

  1. What is the common ("normal") way to load a shared library ? Export all C++ methods to C functions with __cdecl (Way 1) or include the header file (Way 2)? Other solutions?
  2. Related to way 2 (Include header) - how is the library loaded ? By the operating system ? If I move the library, the application cannot find the library -> Library loaded at runtime ?
  3. Is there a way to load a library without including a header or use it at compile time (Load library at runtime)? I read there is a way, but it isn't that easy

I hope that my questions are clear and not "noobish", otherwise please write a comment

Author:swaechter,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/16280729/common-ways-to-use-a-qt-shared-library-with-an-application-and-cmake
yy