Skip to content

Instantly share code, notes, and snippets.

@peteristhegreat
Last active October 11, 2018 17:38
Show Gist options
  • Save peteristhegreat/632c8ae7d1e468a8c9a88096a3c1fdc0 to your computer and use it in GitHub Desktop.
Save peteristhegreat/632c8ae7d1e468a8c9a88096a3c1fdc0 to your computer and use it in GitHub Desktop.
Qt dll build and link

This demo is a merge of two dll examples.

https://wiki.qt.io/How_to_create_a_library_with_Qt_and_use_it_in_an_application

http://www.mingw.org/wiki/sampledll

https://wiki.qt.io/How_to_link_to_a_dll

To do this in Windows with MinGW w64... get your qt + mingw setup using the following:

msys2 Install

http://www.msys2.org/ get the version with x86_64 in the installer name.

Follow pacman instructions here: https://wiki.qt.io/MinGW-64-bit

pacman -Sy
pacman -S bash pacman pacman-mirrors msys2-runtime
# may have to close the msys2 window here if there is a warning that says to do so
pacman -Su

pacman -S base-devel git mercurial cvs wget p7zip
pacman -S perl ruby python2 mingw-w64-x86_64-toolchain # mingw-w64-i686-toolchain
pacman -S mingw-w64-x86_64-qt-creator
pacman -S mingw-w64-x86_64-qt5-static
#include <stdio.h>
#include "example_dll.h"
__stdcall void hello(const char *s)
{
printf("Hello %s\n", s);
}
int Double(int x)
{
return 2 * x;
}
void CppFunc(void)
{
puts("CppFunc");
}
void MyClass::func(void)
{
puts("MyClass.func()");
}
#ifndef EXAMPLE_DLL_H
#define EXAMPLE_DLL_H
#ifdef __cplusplus
extern "C" {
#endif
#ifdef BUILDING_EXAMPLE_DLL
#define EXAMPLE_DLL __declspec(dllexport)
#else
#define EXAMPLE_DLL __declspec(dllimport)
#endif
void __stdcall EXAMPLE_DLL hello(const char *s);
int EXAMPLE_DLL Double(int x);
#ifdef __cplusplus
}
#endif
// NOTE: this function is not declared extern "C"
void EXAMPLE_DLL CppFunc(void);
// NOTE: this class must not be declared extern "C"
class EXAMPLE_DLL MyClass
{
public:
MyClass() {};
virtual ~MyClass() {};
void func(void);
};
#endif // EXAMPLE_DLL_H
TEMPLATE = lib # This line is very significant
CONFIG += console c++11
CONFIG -= app_bundle
CONFIG -= qt
CONFIG += dll # This line is very significant
#CONFIG += staticlib
SOURCES += \
example_dll.cpp
HEADERS += \
example_dll.h
#QMAKE_LFLAGS += QMAKE_LFLAGS_WINDOWS_DLL
#QMAKE_CXXFLAGS += -fno-builtin
DEFINES += BUILDING_EXAMPLE_DLL
#include <stdio.h>
#include "example_dll.h"
#include <stdio.h>
int main(void)
{
hello("World");
printf("%d\n", Double(333));
CppFunc();
MyClass a;
a.func();
return 0;
}
// Dynamic Linking, does not need to be the same compiler, but does need to be the same bit type
// No LIBS required in .pro file, but the INCLUDEPATHS directory should be there.
#include <stdio.h>
#include "example_dll.h"
#include <stdio.h>
#include "windows.h"
#define DLL_CALL_CONVEN __stdcall
#ifdef __cplusplus
extern "C"
{
#endif
// typedef <return type> (DLL_CALL_CONVEN* <func_name>_ptr)(parameter list);
typedef void (DLL_CALL_CONVEN* hello_ptr) (char *);
typedef int (DLL_CALL_CONVEN* Double_ptr) (int);
#ifdef __cplusplus
}
#endif
int main(void)
{
HINSTANCE handle = LoadLibrary(TEXT("C:\\development\\build-example_dll-Qt_5_11_2_MinGW_8_2-Release\\release\\example_dll.dll"));
if (handle == NULL) return 1;
hello_ptr hello = (hello_ptr) GetProcAddress(handle, "hello");
Double_ptr Double = (Double_ptr) GetProcAddress(handle, "Double");
hello("World");
printf("%d\n", Double(333));
// CppFunc();
// MyClass a;
// a.func();
// double x = sqrt(2.0);
// printf ("The sqrt of 2 is: %f\n", x);
return 0;
}
TEMPLATE = app
CONFIG += console c++11
CONFIG -= app_bundle
CONFIG -= qt
SOURCES += \
main.cpp
#QMAKE_CXXFLAGS += -fno-builtin
#DEFINES -= BUILDING_EXAMPLE_DLL
DEPENDPATH += . "C:/development/example_dll"
INCLUDEPATH += "C:/development/example_dll"
LIBS += "C:/development/build-example_dll-Qt_5_11_2_MinGW_8_2-Release/release/example_dll.dll"
#LIBS += -L"C:/development/build-example_dll-Qt_5_11_2_MinGW_8_2-Release/release" -lexample_dll
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment