QT Mingw not able to compile template class - qt

I am compiling a project using MSVC2010 compiler i am compiling it against following library (Qt\4.8.1\msvc2010) In that case compilation is successful with no error.
Now I am trying to compilng a QT app using minGW using following library (Qt\4.7.4\mingw).Then i am getting following error.
devicethreadobject.h include following file event.h.
Event.h file contains few template class.
Does MinGW have problem while working with template ?
Why compilation is successfull with MSVC compiler & fails with MINGW compiler ?
Event.h file :----
#ifndef EVENT_H
#define EVENT_H
#include <QtGui>
#include <QEvent>
//String event derived class
template <typename T> class StringEvent : public QEvent
{
QString m_str;
public:
explicit StringEvent(const QString val) : QEvent(staticType()), m_str(val)
{
}
void setvalue(QString val)
{
m_str = val;
}
QString value() const
{
return m_str;
}
static QEvent::Type staticType()
{
static int type = QEvent::registerEventType();
return static_cast<QEvent::Type>(type);
/*
static int type;
if(type == 0)
{
type = QEvent::registerEventType();
}
return static_cast<QEvent::Type>(type);*/
}
static bool is(const QEvent * ev)
{
return ev->type() == staticType();
}
};
class UpdateEvent : public StringEvent<UpdateEvent>
{
public:
explicit UpdateEvent(QString val): StringEvent(val)
{
//qDebug() << "hello";
}
};
class ClearEvent : public StringEvent<ClearEvent>
{
public:
explicit ClearEvent(QString val): StringEvent(val)
{
}
};
#endif // EVENT_H
Error :---
18:23:08: Running build steps for project CanSewLyzer_vs...
18:23:08: Starting: "c:\qtsdk\desktop\qt\4.7.4\mingw\bin\qmake.exe" D:\Stryker\Stryker\Qt\AutoS\CanSewLyzer_err_mingw\CanSewLyzer_vs\CanSewLyzer_vs.pro -r -spec win32-g++ "CONFIG+=release"
18:23:09: The process "c:\qtsdk\desktop\qt\4.7.4\mingw\bin\qmake.exe" exited normally.
18:23:09: Starting: "C:\QtSDK\mingw\bin\mingw32-make.exe"
C:/QtSDK/mingw/bin/mingw32-make.exe -f Makefile.Release
mingw32-make.exe[1]: Entering directory `D:/Stryker/Stryker/Qt/AutoS/CanSewLyzer_err_mingw/CanSewLyzer_vs-build-desktop-Qt_4_7_4_for_Desktop_-_MinGW_4_4__Qt_SDK__Release'
c:/QtSDK/Desktop/Qt/4.7.4/mingw/bin/uic.exe ../CanSewLyzer_vs/mainwindow.ui -o ui_mainwindow.h
g++ -c -O2 -frtti -fexceptions -mthreads -Wall -DUNICODE -DQT_LARGEFILE_SUPPORT -DQT_DLL -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_HAVE_MMX -DQT_HAVE_3DNOW -DQT_HAVE_SSE -DQT_HAVE_MMXEXT -DQT_HAVE_SSE2 -DQT_THREAD_SUPPORT -DQT_NEEDS_QMAIN -I'c:/QtSDK/Desktop/Qt/4.7.4/mingw/include/QtCore' -I'c:/QtSDK/Desktop/Qt/4.7.4/mingw/include/QtGui' -I'c:/QtSDK/Desktop/Qt/4.7.4/mingw/include' -I'c:/QtSDK/Desktop/Qt/4.7.4/mingw/include/ActiveQt' -I'release' -I'.' -I'../CanSewLyzer_vs' -I'.' -I'c:/QtSDK/Desktop/Qt/4.7.4/mingw/mkspecs/win32-g++' -o release/main.o ../CanSewLyzer_vs/main.cpp
In file included from ../CanSewLyzer_vs/../../geny/common/mythread/devicethreadobject.h:8,
from ../CanSewLyzer_vs/mainwindow.h:9,
from ../CanSewLyzer_vs/main.cpp:2:
../CanSewLyzer_vs/../../geny/common/mythread/../event/event.h: In constructor 'UpdateEvent::UpdateEvent(QString)':
../CanSewLyzer_vs/../../geny/common/mythread/../event/event.h:56: error: class 'UpdateEvent' does not have any field named 'StringEvent'
../CanSewLyzer_vs/../../geny/common/mythread/../event/event.h:56: error: no matching function for call to 'StringEvent<UpdateEvent>::StringEvent()'
../CanSewLyzer_vs/../../geny/common/mythread/../event/event.h:18: note: candidates are: StringEvent<T>::StringEvent(QString) [with T = UpdateEvent]
../CanSewLyzer_vs/../../geny/common/mythread/../event/event.h:15: note: StringEvent<UpdateEvent>::StringEvent(const StringEvent<UpdateEvent>&)
../CanSewLyzer_vs/../../geny/common/mythread/../event/event.h: In constructor 'ClearEvent::ClearEvent(QString)':
../CanSewLyzer_vs/../../geny/common/mythread/../event/event.h:68: error: class 'ClearEvent' does not have any field named 'StringEvent'
../CanSewLyzer_vs/../../geny/common/mythread/../event/event.h:68: error: no matching function for call to 'StringEvent<ClearEvent>::StringEvent()'
../CanSewLyzer_vs/../../geny/common/mythread/../event/event.h:18: note: candidates are: StringEvent<T>::StringEvent(QString) [with T = ClearEvent]
../CanSewLyzer_vs/../../geny/common/mythread/../event/event.h:15: note: StringEvent<ClearEvent>::StringEvent(const StringEvent<ClearEvent>&)
mingw32-make.exe[1]: Leaving directory `D:/Stryker/Stryker/Qt/AutoS/CanSewLyzer_err_mingw/CanSewLyzer_vs-build-desktop-Qt_4_7_4_for_Desktop_-_MinGW_4_4__Qt_SDK__Release'
mingw32-make.exe[1]: *** [release/main.o] Error 1
mingw32-make.exe: *** [release] Error 2
18:23:14: The process "C:\QtSDK\mingw\bin\mingw32-make.exe" exited with code 2.
Error while building project CanSewLyzer_vs (target: Desktop)
When executing build step 'Make'*

It's not MinGW/GCC having issues with templates, it's MSVC being permissive about the syntax.
Change
explicit UpdateEvent(QString val): StringEvent(val)
...
explicit ClearEvent(QString val): StringEvent(val)
With
explicit UpdateEvent(QString val): StringEvent<UpdateEvent>(val)
...
explicit ClearEvent(QString val): StringEvent<ClearEvent>(val)

Related

Does clang support CUDA __global__ function recursion?

nvcc has supported recursive __global__ functions. What about clang? I failed to compile the following code (using clang++ -o helloWorld helloWorld.cu --cuda-gpu-arch=sm_75 -ldl -lrt -lcudart_static -pthread -L/usr/local/cuda/lib64 -std=c++14):
#include <stdio.h>
__global__ void cuda_hello(int i){
printf("Hello World from GPU!\n");
if (i < 3) {
cuda_hello<<<1,1>>>(i+1);
}
}
int main() {
cuda_hello<<<1,1>>>(0);
return 0;
}
. The error I got is
helloWorld.cu:6:3: error: reference to __global__ function 'cuda_hello' in __global__ function
cuda_hello<<<1,1>>>(i+1);
^
helloWorld.cu:3:17: note: 'cuda_hello' declared here
__global__ void cuda_hello(int i){
Anyone knows if/how clang supports such recursion?
At the time of writing (July 2020), it would appear that while separate compilation support has been added to clang, dynamic parallelism ("nested kernels") is still not supported.

What does [-Wstrict-overflow] mean here?

Minimal app:
TestProject.pro:
QT += core gui widgets
CONFIG += C++11
QMAKE_CXXFLAGS_RELEASE -= -O
QMAKE_CXXFLAGS_RELEASE -= -O0
QMAKE_CXXFLAGS_RELEASE -= -O1
QMAKE_CXXFLAGS_RELEASE -= -O2
QMAKE_CXXFLAGS_RELEASE *= -O3
QMAKE_CXXFLAGS_RELEASE -= -Os
QMAKE_CXXFLAGS_RELEASE -= -Ofast
TARGET = TestProject
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp
HEADERS += mainwindow.h
main.cpp:
#include <mainwindow.h>
#include <QApplication>
int main(int argc, char* argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
mainwindow.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QObject>
#include <QStack>
class Other : public QObject
{
Q_OBJECT
public:
explicit Other(QObject* parent = 0);
virtual ~Other();
void test();
private:
QStack<int> myStack;
};
//--------------------------------------------------------------------------------------------------
#include <QMainWindow>
#include <QPushButton>
#include <QTextEdit>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget* parent = 0);
virtual ~MainWindow();
};
#endif // MAINWINDOW_H
mainwindow.cpp:
#include "mainwindow.h"
Other::Other(QObject* parent) :
QObject(parent)
{}
Other::~Other()
{}
void Other::test() //warning on this line
{
myStack.pop(); //but not when this line is commented
}
//--------------------------------------------------------------------------------------------------
MainWindow::MainWindow(QWidget* parent) :
QMainWindow(parent)
{
(new Other(this))->test();
}
MainWindow::~MainWindow()
{}
Compiling with g++ -O3 -Wall gives this warning:
...TestProject/mainwindow.cpp:10: warning: assuming signed overflow does not occur when assuming that (X - c) <= X is always true [-Wstrict-overflow]
void Other::test() //warning on this line
^
Compiling with g++ -O2 -Wall does not.
This question makes sense, as it's for a conditional, but I'm not getting it on a conditional. I'm getting it on a function itself.
I'd like to use the more aggressive optimization, but still compile cleanly if I can. Is there something weird going on with QStack?
Update:
I still don't know what the warning is supposed to mean in this context, but I found a way to get rid of it.
I copied the code from qstack.h and pasted it into my own function, then called it instead of the built-in QStack::pop():
void Other::pop() //warning on this line
{
Q_ASSERT(!myStack.isEmpty());
int t = myStack.data()[myStack.size() - 1];
myStack.resize(myStack.size() - 1);
return t;
}
Still have the warning, but it's moved to the custom pop() function.
Then I played with it a bit and found that caching myStack.size() - 1 for the resize operation kills the warning, but only if it's done before extracting the data():
void Other::pop() //no warning
{
Q_ASSERT(!myStack.isEmpty());
int size = myStack.size() - 1;
int t = myStack.data()[myStack.size() - 1];
myStack.resize(size);
return t;
}
Using the cached value for both operations is also warning-free.
So that's one of probably several ways to get rid of it, but does anyone know why it occurs here?
The warning is telling you that the compiler is not checking if the result of the substraction is negative or not.
Why this matters in this particular line?
myStack.data()[myStack.size() - 1];
Because you are indexing an array (actually using a pointer variable, coming from the data() function, which returns a T*) using the result of a substraction operation, which may result in a negative number, which is, typically, something you don't want.
When you take that substraction and move it to a new variable, then the compiler sees that you are indexing an array directly with an int variable, and not the result of a substraction, so it does not warn anymore if the passed variable is negative or not.
About your comment, you are using the cached data for the resize function. I am not really sure about why this happens, but I guess it may be related that, in -O3 the compiler enables the following flag:
-finline-functions
See http://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html for more details.
This flag enables all the inlining functions optimisation. Since you are using all templated classes, the compiler may be inlining all the involved functions, caching some results and somewhere is optimising the signed overflow. Probably in the if sentence in the resizefunction:
template <typename T>
void QVector<T>::resize(int asize)
{
int newAlloc;
const int oldAlloc = int(d->alloc);
QArrayData::AllocationOptions opt;
//Here, asize will be replaced with d->size - 1 after all the inlining.
//So the compiler is assuming that d->size - 1 will not overflow,
// because of undefined behaviour,
// instead of thinking that it may wrap.
if (asize > oldAlloc) {
newAlloc = asize;
opt = QArrayData::Grow;
} else {
newAlloc = oldAlloc;
}
reallocData(asize, newAlloc, opt);
}
But this is just a guess.

Building an R package in Windows

I am trying to build an R package in Windows using Rtools v3.4 (latest)
I have developed a package with Rcpp and I have built it in an Ubuntu system successfully, but when I try to build it in my Windows 10 machine with the command
R CMD INSTALL --build --compile-both mypackage
I got the following error:
file1.hpp:25:18: required from here
C:/Rtools/mingw_32/i686-w64-mingw32/include/c++/ext/new_allocator.h:110:30: error: invalid conversion from 'const void*' to 'void*' [-fpermissive]
{ ::operator delete(__p); }
My Makefile.win file has this lines:
PKG_CPPFLAGS=-I./dep1/src -I./dep2/cudd -I./dep2/mtr -I./dep2/cplusplus -I./dep2/dddmp -I./dep2/util -I./dep2
CXX_STD=CXX11
PKG_LIBS=-llib1 -lm -fPIC -L./include/windows -llib2
What could be the reasson of that error?
Edit after #DirkEddelbuettel comment:
If add the -fpermissive flag I solve that issue, but a new error appears
R CMD INSTALL --build --compile-both -fpermissive mypackage
file1.hpp:95:71: required from here
C:/Rtools/mingw_32/i686-w64-mingw32/include/c++/bits/stl_construct.h:75:7: error: invalid static_cast from type 'const std::basic_string<char>*' to type 'void*'
{ ::new(static_cast<void*>(__p)) _T1(std::forward<_Args>(__args)...); }
The conflicting class has the following structure:
#ifndef __myKconf__configInfo__
#define __myKconf__configInfo__
#include <iostream>
#include <map>
#include <vector>
class myClass : public parentClass {
public :
myClass() {
...
}
//some public methods
void addToMenu(const std::string& s) {
contents.push_back(s);
}
private:
std::vector<const std::string> contents;
//more private elements
};
#endif
Very difficult to add to a constant string...
private:
std::vector<const std::string> contents;
//more private elements
Make it:
private:
std::vector<std::string> contents;
And you should be golden

Build issue in Qt

I am new in Qt. I am trying to build my project in Qt 5.5.1 but getting the following error. What could be reason for this?
Error Snapshot:
---------------------------------------
18:56:20: Starting: "C:\Qt\Qt5.5.1\Tools\mingw492_32\bin\mingw32-make.exe"
C:/Qt/Qt5.5.1/Tools/mingw492_32/bin/mingw32-make -f Makefile.Release
mingw32-make[1]: Entering directory 'D:/Perforce_Client/project/src'
g++ -c -pipe -fno-keep-inline-dllexport -Wp,-isystem,D:/Perforce_Client/target/win32/usr/include -fno-strict-aliasing -Werror -O2 -std=c++0x -frtti -Wall -Wextra -fexceptions -mthreads -DUNICODE -DQT_NO_DEBUG -DQT_QUICK_LIB -DQT_GUI_LIB -DQT_QML_LIB -DQT_NETWORK_LIB -DQT_CONCURRENT_LIB -DQT_CORE_LIB -DQT_NEEDS_QMAIN -I. -I..\..\target\win32\usr\include -IC:\Qt\Qt5.5.1\5.5\mingw492_32\include -IC:\Qt\Qt5.5.1\5.5\mingw492_32\include\QtQuick -IC:\Qt\Qt5.5.1\5.5\mingw492_32\include\QtGui -IC:\Qt\Qt5.5.1\5.5\mingw492_32\include\QtANGLE -IC:\Qt\Qt5.5.1\5.5\mingw492_32\include\QtQml -IC:\Qt\Qt5.5.1\5.5\mingw492_32\include\QtNetwork -IC:\Qt\Qt5.5.1\5.5\mingw492_32\include\QtConcurrent -IC:\Qt\Qt5.5.1\5.5\mingw492_32\include\QtCore -Iobj -IC:\Qt\Qt5.5.1\5.5\mingw492_32\mkspecs\win32-g++ -o obj\myGen3.o main\myGen3.cc
In file included from C:\Qt\Qt5.5.1\5.5\mingw492_32\include/QtCore/qobject.h:40:0,
from C:\Qt\Qt5.5.1\5.5\mingw492_32\include/QtCore/qiodevice.h:39,
from C:\Qt\Qt5.5.1\5.5\mingw492_32\include/QtCore/qfiledevice.h:37,
from C:\Qt\Qt5.5.1\5.5\mingw492_32\include\QtCore/qfile.h:37,
from C:\Qt\Qt5.5.1\5.5\mingw492_32\include\QtCore/QFile:1,
from main\myGen3.cc:6:
C:\Qt\Qt5.5.1\5.5\mingw492_32\include/QtCore/qobjectdefs.h:164:65: error: variable or field 'qt_static_metacall' declared void
Q_DECL_HIDDEN_STATIC_METACALL static void qt_static_metacall(QObject *, QMetaObject::Call, int, void **); \
^
C:\Qt\Qt5.5.1\5.5\mingw492_32\include/QtCore/qobjectdefs.h:164:65: error: 'QObject' was not declared in this scope
C:\Qt\Qt5.5.1\5.5\mingw492_32\include/QtCore/qobjectdefs.h:164:74: error: expected primary-expression before ',' token
Q_DECL_HIDDEN_STATIC_METACALL static void qt_static_metacall(QObject *, QMetaObject::Call, int, void **); \
^
C:\Qt\Qt5.5.1\5.5\mingw492_32\include/QtCore/qobjectdefs.h:164:76: error: incomplete type 'QMetaObject' used in nested name specifier
Q_DECL_HIDDEN_STATIC_METACALL static void qt_static_metacall(QObject *, QMetaObject::Call, int, void **); \
^
C:\Qt\Qt5.5.1\5.5\mingw492_32\include/QtCore/qobjectdefs.h:164:95: error: expected primary-expression before 'int'
Q_DECL_HIDDEN_STATIC_METACALL static void qt_static_metacall(QObject *, QMetaObject::Call, int, void **); \
^
C:\Qt\Qt5.5.1\5.5\mingw492_32\include/QtCore/qobjectdefs.h:164:100: error: expected primary-expression before 'void'
Q_DECL_HIDDEN_STATIC_METACALL static void qt_static_metacall(QObject *, QMetaObject::Call, int, void **); \
---------------------------------------
I am using Qt Creator 3.5.1 (enterprise) based on Qt 5.5.1 (MSVC 2013, 32 bit).
Build&Run -> Kits configuration is: Desktop QT5.5.1 MinGW 32 bit

Qt: Signals and slots Error: undefined reference to `vtable for

Following example from this link: http://developer.kde.org/documentation/books/kde-2.0-development/ch03lev1sec3.html
#include <QObject>
#include <QPushButton>
#include <iostream>
using namespace std;
class MyWindow : public QWidget
{
Q_OBJECT // Enable slots and signals
public:
MyWindow();
private slots:
void slotButton1();
void slotButton2();
void slotButtons();
private:
QPushButton *button1;
QPushButton *button2;
};
MyWindow :: MyWindow() : QWidget()
{
// Create button1 and connect button1->clicked() to this->slotButton1()
button1 = new QPushButton("Button1", this);
button1->setGeometry(10,10,100,40);
button1->show();
connect(button1, SIGNAL(clicked()), this, SLOT(slotButton1()));
// Create button2 and connect button2->clicked() to this->slotButton2()
button2 = new QPushButton("Button2", this);
button2->setGeometry(110,10,100,40);
button2->show();
connect(button2, SIGNAL(clicked()), this, SLOT(slotButton2()));
// When any button is clicked, call this->slotButtons()
connect(button1, SIGNAL(clicked()), this, SLOT(slotButtons()));
connect(button2, SIGNAL(clicked()), this, SLOT(slotButtons()));
}
// This slot is called when button1 is clicked.
void MyWindow::slotButton1()
{
cout << "Button1 was clicked" << endl;
}
// This slot is called when button2 is clicked
void MyWindow::slotButton2()
{
cout << "Button2 was clicked" << endl;
}
// This slot is called when any of the buttons were clicked
void MyWindow::slotButtons()
{
cout << "A button was clicked" << endl;
}
int main ()
{
MyWindow a;
}
results in:
[13:14:34 Mon May 02] ~/junkPrograms/src/nonsense $make
g++ -c -m64 -pipe -O2 -Wall -W -D_REENTRANT -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/opt/qtsdk-2010.05/qt/mkspecs/linux-g++-64 -I. -I/opt/qtsdk-2010.05/qt/include/QtCore -I/opt/qtsdk-2010.05/qt/include/QtGui -I/opt/qtsdk-2010.05/qt/include -I. -I. -o signalsSlots.o signalsSlots.cpp
g++ -m64 -Wl,-O1 -Wl,-rpath,/opt/qtsdk-2010.05/qt/lib -o nonsense signalsSlots.o -L/opt/qtsdk-2010.05/qt/lib -lQtGui -L/opt/qtsdk-2010.05/qt/lib -L/usr/X11R6/lib64 -lQtCore -lpthread
signalsSlots.o: In function `MyWindow::MyWindow()':
signalsSlots.cpp:(.text+0x1a2): undefined reference to `vtable for MyWindow'
signalsSlots.cpp:(.text+0x1aa): undefined reference to `vtable for MyWindow'
signalsSlots.o: In function `MyWindow::MyWindow()':
signalsSlots.cpp:(.text+0x3e2): undefined reference to `vtable for MyWindow'
signalsSlots.cpp:(.text+0x3ea): undefined reference to `vtable for MyWindow'
signalsSlots.o: In function `main':
signalsSlots.cpp:(.text+0x614): undefined reference to `vtable for MyWindow'
signalsSlots.o:signalsSlots.cpp:(.text+0x61d): more undefined references to `vtable for MyWindow' follow
collect2: ld returned 1 exit status
make: *** [nonsense] Error 1
vtable is for virtual functions, AFAIK, what's the reason of error here?
It looks like moc doesn't generate code for your QObject because you declare it in the .cpp file. The easiest way to fix that is to move the declaration of MyWindow to a header, and add the header to the HEADERS list, in the .pro file:
HEADERS += yourheader.h
Then rerun qmake.
(Please note that the KDE 2.0 book you look at is vastly outdated)
Maybe too late but... Had the same issue and fighted for a while to find where it comes from.
Right click on your project and select “Run qmake” to for a new build of MOC classes. It usually does run automatically...
The moc compiler generates the stubs and calls in moc_xxxx.cpp, and generates the vtable stuff
Just Change your Main() as follows:
#include <QtGui/QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MyWindow w;
w.show();
return a.exec();
}
Based on andref comment just above, when everything is in one cpp file like stuff.cpp, you need to add at the end of the file:
#include "moc_stuff.cpp"
(this is with Qt 4.7.0)
This issue may be caused by some of the following reasons. I have stumbled on all of them from time to time.
Your class header may be missing the Q_OBJECT macro. Add the macro to the header as already noted by other answers.
class MyWidg : public QWidget
{
Q_OBJECT
Your class header may not be parsed by the moc. Add the header file in the HEADERS definitions of your .pro (or .pri) project file.
HEADERS += file.h
If none of the above is true, then you probably need to run qmake again to make sure moc parses the header again, just in case the Q_OBJECT macro was added in the header after the qmake was run. Just run qmake again.

Resources