Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. exporting shared DLL class

exporting shared DLL class

Scheduled Pinned Locked Moved Solved General and Desktop
qt 5.14subdirsshared library
3 Posts 2 Posters 695 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sDmt
    wrote on 12 Feb 2020, 20:09 last edited by
    #1

    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

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 12 Feb 2020, 20:19 last edited by
      #2

      Hi and welcome to devnet,

      You need to also export the Employee class.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      S 1 Reply Last reply 12 Feb 2020, 20:33
      1
      • S SGaist
        12 Feb 2020, 20:19

        Hi and welcome to devnet,

        You need to also export the Employee class.

        S Offline
        S Offline
        sDmt
        wrote on 12 Feb 2020, 20:33 last edited by sDmt 2 Dec 2020, 20:33
        #3

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

        1 Reply Last reply
        0

        1/3

        12 Feb 2020, 20:09

        • Login

        • Login or register to search.
        1 out of 3
        • First post
          1/3
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved