unable to call c++ functions in qt - c++

I have a shared library of the C++ application and I am able to call it using the test app. I want to write a Qt UI for this. I am not able to call the C++ functions directly. Only if I give name mangled function name it works.
Also if I create an object of the C++ class and call a function of the class I am getting "undefined reference to " the function.
How can I call the C++ functions and create objects of C++ class and call functions on them?

You must include the path to the headers files. To do this in Qt Creator, modify your .pro file to include the following line:
INCLUDEPATH += path/to/header/files
you will notice that you MUST use the slash above... If you try using '\', it will not work.

It sounds like you're not correctly including the header files for the code you want to use. The "extra" features of QT don't stop all of the normal C++ features from working.

Related

Receiving the error "undefined symbol" when loading C++ dynamic library from C executable

I am trying to write a plugin for a popular program whose code and compilation process I do not have control over. The program is written in C. However, I have written parts of my plugin in C++, since I use the QT5 library for graphics capabilities. The functions that the C program calls are written in C.
When the C program tries to load the plugin (shared library), it produces this error:
dlopen('build/libfoo.so') failed: build/libfoo.so: undefined symbol: _ZTV13JoystickPanel
JoystickPanel is a class in the C++ part of the program.
I've tried rewriting parts of the program in C, but the error was unaffected. I know that I could rewrite the entire program in C, but I'd rather not have to switch to another, more C-friendly GUI framework. I've also opened up libfoo.so in a text editor and search for JoystickPanel, but it appears to be mangled as _ZN13JoystickPanel.
Are there any compiler options or solutions that I'm missing?
I have no idea what _ZN13JoystickPanel means, since it's not apparently a valid mangled C++ name. It should perhaps be _ZN13JoystickPanelE, which would translate to JoystickPanel. That'd be symbol name for sure, but without much meaning anyway. You must have truncated something: I tried quite a bit and just can't generate an object file that would include _ZN13JoystickPanel as the complete symbol. It's just a prefix, there should be a "second half" attached to it - was there?
But _ZTV13JoystickPanel is the vtable for the JoystickPanel class. It's missing because you didn't provide implementations for all the virtual methods of the JoystickPanel class. Most likely, you didn't invoke moc properly, or forgot to compile and link its output.
You need to show a complete build script for your plugin at the very least (the .pro file, or CMakeLists.txt). You'll also need to provide a github link to your project (I presume it's open source).
The symbols you want to find in the compiled output are at least _ZTV13JoystickPanelD#Ev - virtual destructors, where # is a digit, _ZTV13JoystickPanel - the virtual method table,
Those symbols may be absent when compiled with optimization and/or LTCG, but also absent will be references to them.
You may wish to delete the build folder and rebuild your project, just to be sure. qmake is bad at dependency generation for the makefiles it produces, so if you use it, I suggest switching to cmake + ninja.
Apparently, I'd forgetten to put the #include "moc_controller.cpp" line at the bottom of a file that needed it.
For anyone else chasing this issue while using Qt on CMake, consider making sure that the proper lines are added.

How to make use of the private Qt C++ API?

I'm trying to use the QBalloonTip class to generate a stilized tooltip for a button with QT 5.8.0.
What I've tried until now is modify the header file qsystemtrayicon_p.h the method QBalloonTip::showBalloon(...) by giving default arguments. However even when I included the header qsystemtrayicon_p.h in my code, I'm having problems when linking the compiled sourced code. Next error appears:
LNK2019: unresolved external symbol: "public static void QBalloonTip::showBalloon(...)"
Hope anyone has faced this before. Thank you in advance.
To use Qt's private API, you have to explicitly include it in your .pro file:
QT += widgets-private
However, it is not recommended to do that, as the private APIs are not documented and tie you to the specific Qt version you built with. If your Code is GPL compliant, you should propably rather copy the sources of the balloontip class into your application.
Furthermore, this will not solve your problem, as you can't simply modify a header to an already compiled library. This is simply not possible. Either try to inherit from that class, or copy the sources into your project and modify them there.

Making C++ library without name mangling with MinGW

I have several C++ classes which I decided to compile into separate library usable by other applications. Therefore I made a simple C-compatible envelope which wraps C++ classes. The function definitions in the header file are put in extern "C" block.
When I compile this using MinGW g++, I get shared library with decorated function names. Each function name has suffix #ars_size. To get rid of this names I passed -Wl,--kill-at argument to g++. Then I finally got undecorated names.
Then I made a simple C++/Qt application which serves as a tester of the new library. Unfortunately when I include the library's header file into my C++ application, I get linking errors:
release/mainwindow.o:mainwindow.cpp:(.text+0xd6f): undefined reference to `_imp__TdbGetLastErr#12'
release/mainwindow.o:mainwindow.cpp:(.text+0x1163): undefined reference to `_imp__TdbGetAsyncErr#12'
release/mainwindow.o:mainwindow.cpp:(.text+0x166f): undefined reference to `_imp__TdbStop#4'
release/mainwindow.o:mainwindow.cpp:(.text+0x1698): undefined reference to `_imp__TdbForceTrigger#0'
...
To get rid of these errors I have to exclude -Wl,--kill-at during compiling of the library. But then the library is not compatible with C applications. I believe the key is to make MinGW g++ to link undecorated functions. Please does anybody know, how to do this?
EDIT: Adding more details requested in comments.
When compiling the library, I include its header (with extern "C") from the
source code, so the compiler should be aware of extern "C".
The library is not so simple wrapper. Actually it creates several C++ objects
which are operated using handles from C applications. Also it catches
exceptions from C++ classes etc.
Klasyc

Using C++ in swift using a Objective-C Wrapper

I want to build a little project that utilizes the QR Encoder. I simply used Cocoapods to install it within my project and included the QREncoder.h file within my bridging-header, which works just fine for a couple of other Objective-C resources. My problem occurs because I am including the QREncoder.h file which the includes the QR_Encode.h file. This file uses a pretty standard C++ class definition class CQR_Encode{...};, but when I'm trying to compile my code now, I get the following error /PATH_GOES_HERE/Pods/QR-Code-Encoder-for-Objective-C/QRCodeEncoderObjectiveCAtGithub/QR_Encode.h:81:1: Unknown type name 'class'; did you mean 'Class'?. Looking for that error on the internet got me the information, that I need a Objective-C wrapper class for that file, but I thing QREncoder{.h,.mm} should do the job. I also changed file type within the attributes inspector to either 'objective-c++ source' or 'c++ header' but that did not change the error. I also tried setting the 'compile sources as'-setting in build settings, but of course this didn't work since it sets the compile type for all files and since there are Objective-C classes, it won't work. Does anyone have an answer to help me out?

How enable deprecated functions in Qt5

I want to port a Qt4 program to Qt5 and some functions are not defined (such as QHeaderView::setMoveable), but I see in the qheaderview.h file that with some magic defines (QT_DEPRECATED_SINCE) it should be possible to reenable them.
What do I have to do in order to let QHeaderView::setMovable reappear? I do not want to rewrite my code if there is a way like that.
You can add to your .pro file the following line:
DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0