Qt vtable error - c++

I'm going through the old Trolltech Qt tutorials as I find them more helpful than the newer ones and an undefined reference to vtable error hits me when I got to a class widget implementing its own signals.
http://doc.trolltech.com/4.0/tutorial-t7.html
Instead of keeping the code separate, I just copied them all in the same .cpp for convenience.
I snooped around and found that a vtable error is caused by an undefined virtual function. However, I didn't omit anything from the tutorial code except the code separation. Omitting the Q_OBJECT macro from lcdrange.h as well as the slots and signals seem to make the project compile. I figure it's void valueChanged(int newValue); at lcdrange.h that is causing the fuss but the tutorial did not provide a definition. I'm sure they won't post code that isn't working so I must be doing something wrong. I'm using C::B(MinGW) if that means anything. I'm also new to C++.

MOC must generate code for lcdrange.h and the generated code must be compiled and linked.
qmake ensures this for all header files listed in the HEADERS variable.
Make sure lcdrange.h is listed in the HEADERS section of your .pro file, and rerun qmake.

Related

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.

Automake issue with linking

I'm working on a library that links another library to use its static functions.
Everything was working perfectly until I switched from including all my code in the header file to sharing it between the .h and .cpp file.
I tried to use ./configure --prefix="<same path as before>" and then make install but ended up with a "/usr/bin/install: cannot stat .libs/<library name>.so.0.0.0': No such file or directory" error. (also, I did change my makefile to accommodate for the inclusion of the .cpp file)
Then, trying to compile my old library (which should've still worked since the linked library didn't make install correctly and was still technically using the older version) I get a pile of errors that seem to have to do with c++ itself. For example:
.../gcc/4.9.1/linux64/include/c++/4.9.1/bits/stl_bvector.h:354:13: error: expected ';' at end of member declaration
There are an absolute ton of errors similar to the one above, all having to do with issues in the path .../gcc/4.9.1/linux64/include/c++/4.9.1/
Has anyone run into this error before? Let me know if I need to provide any more information, it's just that I've never had this happen so I'm at a loss as to where to proceed. It's likely something simple I'm missing but the longer I stare at it the less it makes sense.
Thanks!
Update: I ended up solving the issue I was having. Basically, the .so file was not being accessed, so I used make clean and then the linked library compiled properly. Next, the issues with the base library was just an extra character within a header file that was causing the compiler to blow up with over 1500 lines of errors. After fixing that, it was able to link up properly and is working fine now.

Qt Undefined Reference To

I had some messy code so I finally seperated it into headers and sources, but now I get 136 warnings saying
inline function 'void suchandsuch()' used but never defined
and errors
undefined reference to 'Widget::Widget(QWidget*)'
I've tried QMake, all the sources are in .pro file, rebuilding, cleaning and deleting all the moc files.
The first problem is in the inline functions. If you're familiar with template functions, inline functions have the same requirements. They need to have their implementation details included in the header file so that the compiler can generate the code inline whenever its included somewhere else. You can't implement then in the regular .cpp file.
As for the second problem, if Widget::Widget() is referring to the QWidget class and wasn't copy pasted from the error log, I'm guessing that it means your code isn't linking properly against the QtGui library. Make sure the .pro file doesn't have a line removing it as its otherwise included by default (aka, you don't want a line saying QT -= gui).
If that's not the problem and the Widget class it can't find a Widget(QWidget*) constructor for is your own class, then the problem might just be that there isn't an implementation in the widget's .cpp file for a Widget::Widget(QWidget*) function.

Symbol Not Found, expected in Flat Namespace ObjC++

I've got probably what is a simple problem, but there's no informative errors or warnings during compile to alert me to what is going wrong.
I've got a Objective-C++ app that includes a C++ main and ObjC header files.
It builds fine, but when run, it gives this error message:
Dyld Error Message:
Symbol not found: _OBJC_CLASS_$_AppController
Referenced from: /Users/slate/Documents/osirixplugins/eqOsirix/build/Development/rcOsirix.app/Contents/MacOS/rcOsirix
Expected in: flat namespace
in /Users/slate/Documents/osirixplugins/eqOsirix/build/Development/rcOsirix.app/Contents/MacOS/rcOsirix
No amount of googling has resulted in a solution, and I'm sure I've just missed a compilation or build option somewhere.
"AppController.h" is included in the target (checked), and #import'd in the ObjC Class File.
Any help is greatly appreciated.
ObjC++ constantly gives me a headache.
Thanks,
-S!
Clearly the AppController class is missing. Is the AppController class defined in a framework of dynamic library? If so, when you run the app, does it know where to find the libraries/frameworks?
This is a linker issue, by the way. The header files are irrelevant. It's the .m or .mm files you need to look at.
Not sure if this is your issue, but I was having a similar problem with a C++ dll, which took me all day to debug. I haven't programmed in C++ for around 15 years, and while attempting to write a pure virtual function, I used the familiar syntax "virtual void f();" -- oops. In C++ it should be "virtual void f() == 0;" The latest version of gcc on mac OSX 10.9.2 happily compiles the code.
I think it's a perfectly legal forward declaration... however, not sure if C++ allows classes to span multiple files, so it seems like this should be flagged (as no implementation is ever supplied in the CXX file.) In any event, the symbol makes it into the object code, and the linker never complains, so you end up with the missing reference. Hope this helps someone.

VC++ Library Clashing Problem

I am working on a C++ project that uses Qt (gui lib), VTK (graphics lib) and another library which is so obscure I won't mention its name and will instead call it LIB_X. The project uses Qt for the gui components and VTK (more precisely the QVTKWidget extension provided by VTK that supports Qt) for rendering geometry.. and it uses LIB_X to gather and manipulate geometry.
The problem is that it turns out that LIB_X actually uses VTK (where and how, I don't know, it's closed source). At first there was no problem, compiling with both libs linked was going fine, but at some point I called a certain (and highly needed) LIB_X function and compiling led to a bunch of 'blah blah something about a VTK lib/obj already defined in LIB_X dll' errors.
e.g. (and note this is with /FORCE:MULTIPLE so it's a warning here, let me know if you want the error without /FORCE:MULTIPLE and I'll post it):
1>LIB_X.lib(LIB_X.dll) : warning LNK4006: "public: __thiscall std::vector<double,class std::allocator<double> >::~vector<double,class std::allocator<double> >(void)" (??1?$vector#NV?$allocator#N#std###std##QAE#XZ) already defined in vtkCommon.lib(vtkInformationDoubleVectorKey.obj);
I tried using /FORCE:MULTIPLE and it seemed to be a miracle at first, but I am getting random errors in code that would mostly give heap errors. I decided to remove all references to LIB_X from the main project and created a static lib that would handle all LIB_X stuff. I'm not a C++ expert, so I'm not certain how it handles lib clashing when you're using a pre-compiled lib, but I still received lib clashing errors when linking my static lib into my main project, so I still have to use /FORCE:MULTIPLE.
Once I had the static lib it seemed like the random errors had gone away, I was able to do a lot with LIB_X methods in the main project via the static lib, BUT out of nowhere, I added a new data member to my main project's class (a std::vector of doubles) and suddenly I was getting a heap error in one of my static library's methods. If I commented out the new data member, the static library's method would run fine. I hate to give the current error, because honestly I'm not sure if examining it will be worthwhile, but here it is anyway in case it can help:
note: it crashes to xutility on about line 151, pops up assertion:
"file: dbgheap.c line: 1279 expression: _CrtIsValidHeapPointer(pUserData)"
The error comes after adding a vector vector double to a vector vector vector double, crashing on the push_back line:
std::vector<std::vector<double>> tmpVec;
for(srvl_iter = srvl.begin(); srvl_iter != srvl.end(); ++srvl_iter)
{
tmpVec.push_back((*srvl_iter).getControlPoints());
}
this->_splines.push_back(tmpVec); //CRASH
It only started crashing here when I added a new data member to my main project (separate from the static lib!) Commenting out the new data member takes the error away.
std::vector<std::vector<std::vector<double>>> _geometry;
So, /FORCE:MULTIPLE seems bad, I get random errors that just don't make sense to me. Are there other solutions? Am I screwed? Is there something I can do with LIB_X's linking of VTK?
I encountered a bunch of LNK4006 errors when linking my app to a library (call it library LIB_Y) that made heavy use of std::vector<std::string>, which I also did in my app. After a bit of experimenting I found one solution that worked -- wrap LIB_Y in a separate DLL that calls LIB_Y (LIB_Y_WRAPPER, say), and then link the main app against LIB_Y_WRAPPER.
To try out my suggestion you will need to:
Change your "static lib that handles all LIB_X stuff" from a static LIB project into a DLL project (which I will call LIB_X_WRAPPER).
Make sure the header files of LIB_X_WRAPPER don't include any of the LIB_X header files. This is really important because the wrapper needs to completely isolate your app from the data types declared in the LIB_X header files (such as std::vector<double>). Only refer to LIB_X's header files from within the source files of LIB_X_WRAPPER.
Change the declaration of all classes and functions in your static lib to ensure they are exported from the DLL (see this answer if you need details about exporting from a DLL).
This solution worked for me because it kept the instantiation (compiler generated functions) of the std::vector<std::string> class used by LIBY completely separate from the instantiation of std::vector<std::string> in my app.
As an aside, I suspect the cause of the crash you are seeing (you comment it is in the destructor of std::vector<double>) is because the instantiation of std::vector<double> in your app is different to that in LIB_X.
The commenting out is probably just random luck - if you are corrupting the heap you don't always see it right away but an stl vector will allocate and deallocate things left and right so it's no wonder it's finding the error.
Some libs require you include things in a certain order. I am not sure why exactly because to me it seems like giving up and saying you can't design a library properly, but it's the sad fact. So long as you don't include this lib_x anywhere that you include vtk it should be fine, though.
However, they might be fighting over some resource or using something improperly that makes it impossible for them to work together. If you are able to get the compile to work ok by segregating them and it still fails then yes you are just out of luck because it's just a failing in the way that this lib_x was designed and since it's so obscure it's not likely to have been thoroughly debugged against all uses. When something's not widely used it usually ends up being something that works on the developer's machine and project but not necessarily anyone else's.