How to include a .dll using Qt Creator - qt

I've followed the steps described in https://wiki.qt.io/How_to_link_to_a_dll
but, somehow I still get undefined reference errors.
This is what I did:
add to my .pro file the library folder path via INCLUDEPATH+=
add to my .pro file the .dll via LIBS+=
include the .dll (in my case "okFrontPanelDLL.h") via #include in my code
I don't know if it matters, but my library is taken from: http://intantech.com/files/RhythmStim_API_Release_170328.zip
and the extracted folder contains a single .dll and multiple source and header files (do i have to add all sources and headers from the library via SOURCES+= and HEADERS+=?).
Currently, I can declare a variable based on a class defined in the library
okCFrontPanel *dev;
but accessing functions defined for the class, e.g. calling the constructor like
dev = new okCFrontPanel;
leads to an undefined reference error.
edit: I tried direcly adding the source and header files form the library into my Sources folder instead of linking the library and the code works fine, so there is (probably) at least nothing wrong with how I am trying to use the functionalities of the library.
edit2: further information:
OS: Win 7 64 bit
Qt version: 5.9.0
compiler: MinGW 32bit
file location: /[PROJECT FOLDER]/mylibrary
#include <QCoreApplication>
#include "myLibrary/okFrontPanelDLL.h"
int main(int argc, char *argv[])
{
// QCoreApplication a(argc, argv);
// return a.exec();
printf("hello world\n");
okCFrontPanel *dev;
dev = new okCFrontPanel;
// dev->BoardModel;
// If only one Opal Kelly board is plugged in to the host computer, we can use this.
dev->OpenBySerial();
// Set XEM6010 PLL to default configuration to produce 100 MHz FPGA clock.
dev->LoadDefaultPLLConfiguration();
// Upload RhythmStim bitfile which is compiled from RhythmStim Verilog code.
dev->ConfigureFPGA("mylibrary/main.bit");
printf("omg dis is working\n");
}

Related

moc failing with 'Undefined interface' with Qt 5.10 in a Docker container

A very simple Qt project fails to build for me with Qt 5.10 in a Docker container (with an image derived from opensuse:tumbleweed). The project is as follows:
sh-4.4# cat test.pro
TEMPLATE = app
TARGET = test
INCLUDEPATH += .
INCLUDEPATH += sub
HEADERS = obj.h sub/iface.h
SOURCES = obj.cpp main.cpp
sh-4.4# cat sub/iface.h
#pragma once
#include <QtPlugin>
class Interface
{
public:
virtual ~Interface () {}
};
Q_DECLARE_INTERFACE (Interface, "org.meh.interface/1.0")
sh-4.4# cat obj.h
#pragma once
#include <QObject>
#include <sub/iface.h>
class Obj : public QObject
{
Q_OBJECT
Q_INTERFACES (Interface)
};
sh-4.4# cat obj.cpp
#include "obj.h"
sh-4.4# cat main.cpp
int main() {}
In this case moc complains as follows:
obj.h:9: Error: Undefined interface
Everything is fine in another container with Qt 5.9, and everything is also fine with Qt 5.10 when the project is built in openSUSE Build Service (which uses something else instead of Docker). Some quick googling did not reveal any relevant bugreports for recent Qt versions.
What could be wrong?
Running moc under strace shows Operation not permitted on various statx calls, which sheds some light on why exactly it fails (also, related to this question). This pull request is hopefully going to fix this.
Did you try to run the container with the --privileged (see Which capabilities are needed for statx to stop giving EPERM)?

How to use QWS_MOUSE_PROTO inside code in Qt

I am using QT to make an application for Embedded linux device. When I started my application, mouse & keyboard was not working. From searching about this problem, I came to know that we need to run below command before starting the application:
export QWS_MOUSE_PROTO="USB:/dev/input/event-mouse"
export QWS_KEYBOARD="USB:/dev/input/event-keyboard"
After running above commands, I was able to use mouse and keyboard in my application. But this looks a bit odd because whenever I need to run my application, I have to run those commands. Also I will be setting my application to auto run after the boot so in that case I won't be able to run those commands. So I was wondering if I can include these commands somewhere in my code so that they automatically run and then application starts. Can anyone please guide me here. Please help. Thanks.
Instead of running your application, you could run a script:
#! /usr/bin/env bash
export QWS_MOUSE_PROTO="USB:/dev/input/event-mouse"
export QWS_KEYBOARD="USB:/dev/input/event-keyboard"
my_application
You can also set the environment variables inside of your application, before Qt is started. Use setenv:
#include <cstdlib>
int main(int argc, char ** argv) {
// Set default values if none are set.
setenv("QWS_MOUSE_PROTO", "USB:/dev/input/event-mouse", 0);
setenv("QWS_KEYBOARD", "USB:/dev/input/event-keyboard", 0);
QApplication app(argc, argv);
...
return app.exec();
}
External QWS_MOUSE_PROTO and QWS_KEYBOARD will override the internal defaults since the override parameter is set to zero. This is the desired behavior.

boost compute (opencl wrapper), initial setup problems (qt, g++)

Been trying to compile this sample code: https://github.com/boostorg/compute/blob/master/README.md
I installed QT Creator 5.7 using mingw530
I compiled the boost libraries using
bootstrap.bat gcc
b2 install --prefix="C:\Boostbuild" --toolset=gcc
bjam --build-dir=c:/Dev/Boost/Boost_lib toolset=gcc stage
I installed AMD SDK 3.0, 2.9.1, and 2.9
I even downloaded opencl 1.1, 1.2, and 2.1 cl.hpp and tried to include that.
The compile starts, but I get a slew of errors
C:\Dev\Boost\compute-master\include\boost\compute\device.hpp:80: error: undefined reference to `clRetainDevice#4'
C:\Users\User\Documents\Projects\build-console-test-Desktop_Qt_5_7_0_MinGW_32bit-Debug\debug\main.o:-1: In function `ZN5boost7compute6deviceaSERKS1_':
I tried a simple qt console app, using the code supplied by boost compute
Note: this isn't specific to qt, I've also tried compiling this using
g++ -I/path/to/compute/include sort.cpp -lOpenCL
doing an -I to each of the include's in the main.cpp (see below)
Ideally, I'd like to know how to compile the example given on their page, with includes and all (and relevant amd sdk and/or opencl versions) along with the necessary included libraries.
My qt project file libraries
INCLUDEPATH += C:\Dev\Boost\compute-master\include
INCLUDEPATH += C:/Users/User/Downloads/dev/boost_1_61_0
INCLUDEPATH += "C:\Program Files (x86)\AMD APP SDK\2.9-1\include"
My main.cpp
#include <iostream>
#include <vector>
#include <algorithm>
#include <boost/compute.hpp>
//#define CL_USE_DEPRECATED_OPENCL_1_1_APIS
//#undef CL_VERSION_1_2
//#include <C:\Dev\OpenCL\2.1\cl.hpp>
namespace compute = boost::compute;
int main()
{
// get the default compute device
compute::device gpu = compute::system::default_device();
// create a compute context and command queue
compute::context ctx(gpu);
compute::command_queue queue(ctx, gpu);
// generate random numbers on the host
std::vector<float> host_vector(1000000);
std::generate(host_vector.begin(), host_vector.end(), rand);
// create vector on the device
compute::vector<float> device_vector(1000000, ctx);
// copy data to the device
compute::copy(
host_vector.begin(), host_vector.end(), device_vector.begin(), queue
);
// sort data on the device
compute::sort(
device_vector.begin(), device_vector.end(), queue
);
// copy data back to the host
compute::copy(
device_vector.begin(), device_vector.end(), host_vector.begin(), queue
);
return 0;
}
if I uncomment out the include cl.hpp, I get further
C:/Dev/Boost/compute-master/include/boost/compute/allocator/buffer_allocator.hpp:91: undefined reference to `clReleaseMemObject#4'
The "slew of errors" are link errors because the location of the AMP APP SDK libraries (libOpenCL.a in this case) is missing.
E.g. to link to the 32 bit version for MinGw, -lOpenCL becomes:
-L"C:\Program Files (x86)\AMD APP SDK\2.9-1\lib\x86" -lOpenCL
Or you could add the following to your qt .pro file:
# Ensure that the AMDAPPSDKROOT environment variable has been set
OPENCL_ROOT = $$(AMDAPPSDKROOT)
isEmpty(OPENCL_ROOT) {
error("Please set AMDAPPSDKROOT to the location of the AMD APP SDK")
} else {
message(Using Boost from: $$OPENCL_ROOT)
}
INCLUDEPATH += $$OPENCL_ROOT/include
LIBS += -L$${OPENCL_ROOT}/lib/x86
LIBS += -lOpenCL
Note: the AMDAPPSDKROOT environment variable is normally created when you install the AMD APP SDK. In your case it should be set to:
C:\Program Files (x86)\AMD APP SDK\2.9-1\

Qt and using an existing win32 dll

I want to do some development work using Qt. I have built several small apps and followed
some tutorials. All's fine and seems straight forward.
The development to be done involves using existing code that is contained in win32 dlls. I want to reuse this code with minimum fuss and link them into my Qt app. I have the headers, libs and the dlls so I'll be linking at compile time not dynamically at runtime.
I have tried to do this but Qt always complains always complains with link errors similar to:
main.obj:-1: error: LNK2019: unresolved external symbol _imp_Add referenced in function _main
No matter how I tweak the .pro file it always complains.
I've spent many hours googling and found snippets of info. I could not find one answer that told the whole story. What I'm after is a set of steps, a tutorial like sequence that needs to be followed. There may even be an example in the Qt installation examples but I've been unable to find it.
Here is the simple 'knock-up' I've been trying to get to work in order to move on to the main development. It's based on the MS tutorial dll MathsFunc.
The win32 dll:
// Visual Studio 2005
//Funcs.h
#ifdef MATHFUNCS_EXPORTS
#define MATHFUNCSDLL_API __declspec(dllexport)
#else
#define MATHFUNCSDLL_API __declspec(dllimport)
#endif
#ifdef __cplusplus
extern "C" { /* Assume C declarations for C++ */
#endif
// Returns a + b
MATHFUNCSDLL_API double Add(double a, double b);
#ifdef __cplusplus
} /* Assume C declarations for C++ */
#endif
//Funcs.cpp
#include "Funcs.h"
double Add(double a, double b)
{
return a + b;
}
The Qt app that imports the dll.
//main.cpp
#include <QCoreApplication>
#include "../../mathfuncs/funcs.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
double dResult = Add(1.0,2.0);
printf("1 + 2 = %f\n",dResult);
return a.exec();
}
//The Qt project file .por
#-------------------------------------------------
#
# Project created by QtCreator 2013-03-04T09:16:18
#
#-------------------------------------------------
QT += core
QT -= gui
TARGET = Useit
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp
INCLUDEPATH += c:/tmp/mathfuncs
HEADERS += c:/tmp/mathfuncs
LIBS += c:/tmp/mathfuncs/MathFuncs.lib
Thanks in advance for any input.
D.

Force library linking with Qt and google test

I'm trying to write a test suit for my Qt(c++) application using google test, the main problem is that my application consists of one main program and various shared libs. Everything was working just fine, until I tried to do some code coverage using gcov/lcov (which don't operate on dynamic libs), so I modified all my .pro file to have a static lib compilation using :
CONFIG += staticlib create_prl
QMAKE_LFLAGS += -static
LIBS += ../../Libs/lib*.a
The problem is that google test test application is something like this:
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
so it detects the linkage at runtime, which means that when I compile the test application here is no warning or whatever that indicate a problem, and when running the test application it execute 0 test because it doesn't found any.
my solution (quit annoying one) is to define in each class which will be used for test a phony public static member and do an operation for the main test application on it:
class SETTINGS_TESTS_SHARED_EXPORT SettingsTests: public testing::Test {
public:
SettingsTests();
virtual ~SettingsTests();
static bool dummy;
protected:
virtual void SetUp();
virtual void TearDown();
private:
Settings* _setting0;
Settings* _setting1;
Settings* _setting2;
};
using namespace MBI::SETTINGS;
using namespace MBI::TESTS;
int main(int argc, char **argv) {
SettingsTests::dummy = true;
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
so I can force the linkage of that lib on my application.
I have also found the option -u to do something like that in gcc but doesn't seem to work. have anyone have a way to force static library linking if no symbol can be detected in compile time ?
I don't completely understand the question, it's not explained what ::testing::InitGoogleTest and RUN_ALL_TESTS actually do, and how they're related to dynamic linking.
But: you can make the GNU linker ignore unresolved symbols at link time by using the option --unresolved-symbols=ignore-all , or if you need to pass the option to gcc, -Wl,--unresolved-symbols=ignore-all.
In the google test doc there is a mention of linkage problem with visual c++ dll:
Important note for Visual C++ users
If you put your tests into a library and your main() function is in a different library or in your .exe file, those tests will not run. The reason is a bug in Visual C++. When you define your tests, Google Test creates certain static objects to register them. These objects are not referenced from elsewhere but their constructors are still supposed to run. When Visual C++ linker sees that nothing in the library is referenced from other places it throws the library out. You have to reference your library with tests from your main program to keep the linker from discarding it. Here is how to do it. Somewhere in your library code declare a function:
__declspec(dllimport) int PullInMyLibrary() { return 0; }
If you put your tests in a static library (not DLL) then __declspec(dllexport) is not required. Now, in your main program, write a code that invokes that function:
int PullInMyLibrary();
static int dummy = PullInMyLibrary();
This will keep your tests referenced and will make them register themselves at startup.
In addition, if you define your tests in a static library, add /OPT:NOREF to your main program linker options. If you use MSVC++ IDE, go to your .exe project properties/Configuration Properties/Linker/Optimization and set References setting to Keep Unreferenced Data (/OPT:NOREF). This will keep Visual C++ linker from discarding individual symbols generated by your tests from the final executable.
There is one more pitfall, though. If you use Google Test as a static library (that's how it is defined in gtest.vcproj) your tests must also reside in a static library. If you have to have them in a DLL, you must change Google Test to build into a DLL as well. Otherwise your tests will not run correctly or will not run at all. The general conclusion here is: make your life easier - do not write your tests in libraries!
And the solution adopted is pretty much what I did with g++. I'm suspecting the fact that qmake produce intermediary file moc_* and that my code in linked against that.

Resources