PDA

View Full Version : Linking DLL aganist qt application in windows



Surajkumar
15th August 2015, 14:40
Hi,
I have created a shared library in qt and a sample application to test the library functions. But when i try to link the dll file, only single function is accessible other functions are not.Below is my library and application code snippet. how can i change the code to access the library function ?

Library Header file:

#ifndef TEST_LIB_H
#define TEST_LIB_H
#include <QtCore/qglobal.h>

#if defined(TEST_LIB_LIBRARY)
# define TEST_LIBSHARED_EXPORT Q_DECL_EXPORT
#else
# define TEST_LIBSHARED_EXPORT Q_DECL_IMPORT
#endif

class TEST_LIBSHARED_EXPORT Test_lib {
public:
Test_lib();
};

extern "C" TEST_LIBSHARED_EXPORT int add(int a,int b);
extern "C" TEST_LIBSHARED_EXPORT int sub(int a,int b);

#endif // TEST_LIB_H


Libray source file:

#include "test_lib.h"
#include "stdio.h"

Test_lib::Test_lib()
{
printf("Test Lib Invoked\n");
}
extern "C" TEST_LIBSHARED_EXPORT int add(int a, int b)
{
printf("In Lib\n add");
int c=a+b;
return c;
}

extern "C" TEST_LIBSHARED_EXPORT int sub(int a, int b)
{
printf("In Lib\n sub");
return 2;
// int c=a-b;
//return c;
}


Application header file:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include "test_lib.h"

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();

private:
Ui::MainWindow *ui;

private slots:
void on_pushButton_clicked();
};


#endif // MAINWINDOW_H

Application source file:

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "QLibrary"
#include "QDebug"
#include "QMessageBox"

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
int result=0;
result=add(1,2);
qDebug() << result;
result=sub(2,1);
qDebug() << result;
}

MainWindow::~MainWindow()
{
delete ui;
}


My problem is add function is accessible but sub function is not accessible.
Thanks in advance.

ChrisW67
15th August 2015, 22:05
How have you linked the test library to the main program? What does the main program pro file look like?