When writing plugin libraries with Qt one can attach a JSON file containing some meta data to it using the Q_PLUGIN_METADATA
macro. This JSON file is then linked into the library, for later usage with QPluginLoader::metaData()
.
Unfortunately when building the plugin library the associated JSON file is not by default seen as a dependency for the library binary by qmake
. When the JSON file is modified the plugin library project has to be rebuild (especially re-linked) manually to force the modified JSON file into the library binary.
What would be the proper way to mention the JSON file in the .pro
file so that it is automatically linked in when it is modified?
I typically use the following to make the json file a dependency of the generated moc file that contains the corresponding code. Assuming the class where you specify Q_PLUGIN_METADATA
is located in a header file called myclass.h
, the qmake code is as follows:
DISTFILES += myclass.json
json_target.target = moc_myclass.o
json_target.depends += $$PWD/myclass.json
QMAKE_EXTRA_TARGETS += json_target
Note: You might have to use json_target.target = $$OBJECTS_DIR/moc_myclass.o
instead, if OBJECTS_DIR has previously been defined. Check the generated Makefile
to see if the path of the dependency matches the one of the corresponding target.