title: Using CMake with Qt5 and Qt Plugins
body: <p>I'd like to avoid using <code>qmake</code> and <code>.pro</code> files. The problem is that I cannot get <code>cmake</code> to work nicely with <code>Qt Plugins</code>. I've included the code below showing the interface, plugin, loader that work correctly for the given <code>.pro</code> file but I cannot figure out how to transfer this functionality to <code>cmake</code>. </p>
  
  <h2>Plugin Interface</h2>
  
  <p>Pure virtual interface, known by the loader.</p>
  
  <pre><code>#include &lt;QtCore/qglobal.h&gt;
  
  class HelloPluginInterface
  {
  public:
      virtual void DoSomething() const = 0;
  };
  
  Q_DECLARE_INTERFACE( HelloPluginInterface, "com.harbrick.Qt.HelloPluginInterface")
  </code></pre>
  
  <h2>Plugin</h2>
  
  <p>Plugin that becomes a <code>.so</code> to be loaded by the loader.</p>
  
  <pre><code>#include &lt;QtPlugin&gt;
  #include "helloplugin_global.h"
  
  class HelloPlugin : public QObject, public HelloPluginInterface
  {
      Q_OBJECT
      Q_PLUGIN_METADATA( IID "com.harbrick.Qt.HelloPluginInterface" )
      Q_INTERFACES( HelloPluginInterface )
  
  public:
      void DoSomething() const;
  };
  </code></pre>
  
  <h2>Plugin Loader</h2>
  
  <pre><code>void MainWindow::LoadPlugin( const QString&amp; pathToLibrary )
  {
      QPluginLoader loader( pathToLibrary );
      QObject* instance = loader.instance();
  
      if( instance )
      {
          HelloPluginInterface *plugin = qobject_cast&lt; HelloPluginInterface* &gt;( instance );
          if(plugin)
          {
              //do stuff
              ...
          }
          else
          {
              qDebug() &lt;&lt; "Not a plugin: " &lt;&lt; Filename &lt;&lt; " = " &lt;&lt; loader.errorString();
          }
      }
  }
  </code></pre>
  
  <h2>CMakeLists.txt</h2>
  
  <p><strong>Can't get to work</strong></p>
  
  <pre><code>project( HelloPlugin )
  cmake_minimum_required( VERSION 2.8.11 )
  
  set( CMAKE_INCLUDE_CURRENT_DIR ON )
  
  find_package(Qt5Widgets)
  
  include_directories( 
      ${CMAKE_CURRENT_SOURCE_DIR} 
  )
  
  set( INCLUDE
      cmakeplugin.h
      cmakeplugininterface.h
  )
  
  set( SOURCES
      cmakeplugin.cpp
  )
  
  add_executable(${PROJECT_NAME} ${INCLUDE} ${SOURCES} ${SRC_LIST})
  
  add_library( cmakePlugin SHARED cmakeplugin.cpp )
  </code></pre>
  
  <h2>QMake .pro</h2>
  
  <p><strong>Works</strong></p>
  
  <pre><code>QT       += widgets
  
  TARGET = HelloPlugin
  TEMPLATE = lib
  
  SOURCES += helloplugin.cpp
  
  HEADERS += helloplugin.h \
      helloplugin_global.h
  
  CONFIG += plugin debug
  
  INSTALLS += target
  </code></pre>