Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

exporting shared DLL class



  • Hi,
    I want to make a subdir project that contains two projects: DLLproject (a shared DLL that works as a database reader) and TestProject(a widget application). My DLL has two classes as shown in the snippet below. I need the function getEmployees to return a QMap of Employees to the TestProject.
    dllproject.h

    #ifndef DLLPROJECT_H
    #define DLLPROJECT_H
    
    #include "DLLproject_global.h"
    #include "employee.h"
    #include <QMap>
    #include <QObject>
    
    class DLLPROJECT_EXPORT DLLproject
    {
    public:
        DLLproject();
        QMap<QString,Employee> getEmployees();
    
    private:
        QMap<QString,Employee> Employees;
    };
    
    #endif // DLLPROJECT_H
    

    dllproject.cpp

    #include "dllproject.h"
    
    DLLproject::DLLproject()
    {
        for(int i = 1; i<11; i++ ){
            Employee emp;
            emp.setName("John"+QString::number(i));
            emp.setAge(20+i);
            Employees.insert(emp.getName(),emp);
        }
    }
    
    QMap<QString, Employee> DLLproject::getEmployees(){
        return Employees;
    }
    
    

    employee.h

    #ifndef EMPLOYEE_H
    #define EMPLOYEE_H
    
    #include <QObject>
    
    class Employee
    {
    public:
        Employee();
        QString getName();
        void setName(QString name);
        int getAge();
        void setAge(int age);
    
    private:
        QString Name;
        int Age;
    };
    #endif // EMPLOYEE_H
    

    employee.cpp

    #include "employee.h"
    
    Employee::Employee(){
    }
    QString Employee::getName(){
    return  Name;
    }
    void Employee::setName(QString name){
        this->Name = name;
    }
    int Employee::getAge(){
        return Age;
    }
    void Employee::setAge(int age){
        this->Age = age;
    }
    
    mainwindow.h
    
    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    #include "DLLproject_global.h"
    #include "dllproject.h"
    
    QT_BEGIN_NAMESPACE
    namespace Ui { class MainWindow; }
    QT_END_NAMESPACE
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        MainWindow(QWidget *parent = nullptr);
        ~MainWindow();
    
    private slots:
        void on_pushButton_clicked();
    
    private:
        Ui::MainWindow *ui;
    };
    #endif // MAINWINDOW_H
    

    mainwindow.cpp

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    }
    
    MainWindow::~MainWindow(){
        delete ui;
    }
    void MainWindow::on_pushButton_clicked()
    {
        DLLproject dl;
        QMap<QString, Employee> emps = dl.getEmployees();
        QMapIterator<QString,Employee>i(emps);
        while (i.hasNext()) {
            i.next();
            Employee em = i.value();
            ui->listWidget->addItem(em.getName());
        }
    }
    

    I successfully shared the DLL with the testProject but when I try to iterate through the QMap of employees I get this error

    \Link DLL\LinkDLL\TestProject\mainwindow.cpp:25: error: undefined reference to `Employee::getName()'
    

    P.S. when I use ui->listWidget->addItem(i.key())the code works.
    I hope I made the problem clear.
    My C++ is very shallow and this might not be the best way do this, so I appreciate if you point me to the right way.

    Thanks in advance


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    You need to also export the Employee class.



  • @SGaist Thank you so much for the quick reply.
    I finally managed to do it.