It gives undefined symbol error when accessing the extern variable from plugin
I have header file globals.h
#ifndef GLOBALS_H
#define GLOBALS_H
extern int globalNumber;
#endif // GLOBALS_H
which is included in both main application and plugin;
in main application mainwindow.cpp
#include "mainwindow.h"
int globalNumber=12345;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
qDebug()<<"read variable from main app:"<<globalNumber;
loadPlugin();/*loads plugin correctly with QPluginLoader using an interface.h containing virtual void run()=0;
and calls run() after the plugin loading;*/
}
The plugin also have same globals.h
Inside plugin.cpp
#include<plugin.cpp>
void plugin::run()
{
qDebug()<<"read variable from plugin:"<<globalNumber;//if i comment this plugin get loaded without error
}
I get the application output as
read variable from main app:12345
Cannot load load library .../libplugin.so undefined symbol: globalNumber
Is there any other way to achieve this? Can it be done with singleton pattern or qglobalstatic? An example will be really helpfull. :)