简体   繁体   中英

Adding JSON file to plugin qmake project

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.

Well, you could just add the JSON file to resources: create some *.qrc file, add yours there and then write in the .pro file something like RESOURCES += plugin_data.qrc .- There is also DISTFILES variable, but AFAIK it's Unix-only and does not solve your problem.

Tried myself and it never worked, still the recipe from documentation works: INCLUDEPATH += JSON_FILE_LOCATION_DIR . It's true that qmake caches builds sometimes, but they say adding to include path should do the trick and make a proper build.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM