1. 程式人生 > >使用CMake建立一個最簡CTK外掛

使用CMake建立一個最簡CTK外掛

1.建立外掛Activator

ctkTestAppActivator_p.h

#include <QScopedPointer>

#include <ctkPluginActivator.h>

class MyAppContainer;

class ctkTestAppActivator : public QObject,
    public ctkPluginActivator
{
  Q_OBJECT
  Q_INTERFACES(ctkPluginActivator)
  Q_PLUGIN_METADATA(IID "org.plugin.test")
public:

  ctkTestAppActivator();
  ~ctkTestAppActivator();

  void start(ctkPluginContext* context);
  void stop(ctkPluginContext* context);
};

ctkTestAppActivator.cpp

#include "ctkTestAppActivator_p.h"

#include <ctkPluginContext.h>

#include <QtPlugin>
#include <QDebug>

//----------------------------------------------------------------------------
ctkTestAppActivator::ctkTestAppActivator()
{
}

//----------------------------------------------------------------------------
ctkTestAppActivator::~ctkTestAppActivator()
{
}

//----------------------------------------------------------------------------
void ctkTestAppActivator::start(ctkPluginContext* context)
{
  qDebug() << "[org.plugin.test] ctkTestAppActivator::start()";
}

//----------------------------------------------------------------------------
void ctkTestAppActivator::stop(ctkPluginContext* context)
{
  qDebug() << "[org.plugin.test] ctkTestAppActivator::stop()";
}

2.編寫CMakeLists.txt檔案

CMakeLists.txt

project(org_plugin_test)

set(PLUGIN_export_directive "org_plugin_test_EXPORT")

set(PLUGIN_SRCS
  ctkTestAppActivator.cpp
)

set(PLUGIN_MOC_SRCS
  ctkTestAppActivator_p.h
)

set(PLUGIN_resources

)

ctkFunctionGetTargetLibraries(PLUGIN_target_libraries)

ctkMacroBuildPlugin(
  NAME ${PROJECT_NAME}
  EXPORT_DIRECTIVE ${PLUGIN_export_directive}
  SRCS ${PLUGIN_SRCS}
  MOC_SRCS ${PLUGIN_MOC_SRCS}
  RESOURCES ${PLUGIN_resources}
  TARGET_LIBRARIES ${PLUGIN_target_libraries}
  TEST_PLUGIN
)

這裡project裡面的是專案名稱,這個名稱至少要帶有一個"_",否則會編譯失敗,剩下的除了原始檔和標頭檔案的分類,其他部分不變,set(PLUGIN_export_directive "org_plugin_test_EXPORT")中的值自己指定

3.編寫manifest_headers.cmake和target_libraries.cmake

這兩個檔案是為了編譯時候生成MANIFEST.MF和*.pro檔案

manifest_headers.cmake

set(Plugin-ActivationPolicy "eager")
set(Plugin-Name "org_plugin_test")
set(Plugin-Version "0.1")
set(Plugin-Description "Test plugin for framework, org_plugin_test")
set(Plugin-Vendor "CommonTK")
set(Plugin-ContactAddress "http://www.commontk.org")
set(Plugin-Category "test")

target_libraries.cmake

set(target_libraries
  CTKPluginFramework
  )

其中set(Plugin-ActivationPolicy "eager")引數不要修改,其他引數參照CTK 中的 MANIFEST.MF 檔案

4.將上述檔案放在一個資料夾下,將該資料夾複製到CTK-master\Libs\PluginFramework\Testing\FrameworkTestPlugins\目錄下,然後在CTK-master\Libs\PluginFramework\Testing\CMakeLists.txt下新增該資料夾配置資訊

5.使用CMake編譯CTK-master,得到ctk的工程檔案

6.使用vs編譯ctk,得到外掛的工程檔案

7.使用vs編譯外掛

8.將外掛在CTK-master-build\CTK-build\bin\test_plugins\下生成的相應版本的dll庫直接新增到要用的地方