I’m an PyQt programmer and trying to learn C++ Qt.
I’m following this link for learning how to import my ui files. But when I’m trying to compile, I’m getting that error:
[utdmr@utdmr-arch cpp]$ qmake -project && qmake && make
/usr/bin/uic ui.ui -o ui_ui.h
g++ -c -m64 -pipe -march=x86-64 -mtune=generic -O2 -pipe -Wall -W -D_REENTRANT -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/share/qt/mkspecs/linux-g++-64 -I. -I/usr/include/QtCore -I/usr/include/QtGui -I/usr/include -I. -I. -I. -o main.o main.cpp
/usr/bin/moc -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/share/qt/mkspecs/linux-g++-64 -I. -I/usr/include/QtCore -I/usr/include/QtGui -I/usr/include -I. -I. -I. gui.h -o moc_gui.cpp
g++ -c -m64 -pipe -march=x86-64 -mtune=generic -O2 -pipe -Wall -W -D_REENTRANT -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/share/qt/mkspecs/linux-g++-64 -I. -I/usr/include/QtCore -I/usr/include/QtGui -I/usr/include -I. -I. -I. -o moc_gui.o moc_gui.cpp
g++ -m64 -Wl,--hash-style=gnu -Wl,--as-needed -Wl,-O1 -o cpp main.o moc_gui.o -L/usr/lib -lQtGui -lQtCore -lpthread
moc_gui.o: In function `Gui::Gui(QWidget*)':
moc_gui.cpp:(.text+0x80): multiple definition of `Gui::Gui(QWidget*)'
main.o:main.cpp:(.text+0x0): first defined here
moc_gui.o: In function `Gui::Gui(QWidget*)':
moc_gui.cpp:(.text+0x80): multiple definition of `Gui::Gui(QWidget*)'
main.o:main.cpp:(.text+0x0): first defined here
collect2: ld returned 1 exit status
make: *** [cpp] Error 1
But I can’t see that multiple definition on my code. I’m also new to C++ classes’ inheritance.
Here is my files:
gui.h:
#include "ui_ui.h"
class Gui : public QWidget
{
Q_OBJECT
public:
Gui(QWidget *parent=0);
private:
Ui::Form ui;
};
Gui::Gui(QWidget *parent)
: QWidget(parent)
{
ui.setupUi(this);
}
main.cpp:
#include <QApplication>
#include "gui.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
Gui gui;
gui.show();
return app.exec();
}
ui_ui.h: http://paste.kde.org/56251/ (I used pastebin because it’s long)
Thanks.
You should put the function definition into a .cpp file.
So create gui.cpp with the following contents:
removing it from the .h file.
The reason you were getting the error is that the header file was being included in more than one .cpp file (your main.cpp and moc_gui.cpp, a generated file) and since the function definition was in the header file it was multiply defined.
Oh, don’t forget to add the new .cpp file to your project. If you forget you will get link errors about undefined functions!
@cbamber85 made some good comments on your original question. Although in this case your problems are not being caused by the lack of multiple inclusion guards as your projects get bigger you may run into issues so getting into the habit now will be useful. To protect your file gui.h against multiple inclusion you do this:
The actual macro name
GUI_H_is not significant you could useSnOOpybut you need to have a different one for each header file so using some variation of the file name is common practice. It is also conventional to only use uppercase letters in#defined macros (just a convention; the compiler does not care).In a lot of modern compilers your can simple use:
OK, so what’s this multiple inclusion and when does it occur? Well suppose you a file called types.h that defined some basic types you used frequently. Now suppose you have two classes Foo and Bar each of which uses types from types.h. Here’s what this might look like:
File: types.h
File: foo.h
File: bar.h
Notice I have left out the multiple inclusion guards. Now suppose I have a main that uses both Foo and Bar:
In the absence of multiple inclusion guards this will complain that Int32 is multiply defined… What’s the problem? Well since main.cpp includes foo.h and foo.h includes types.h it gets included once there and then when bar.h gets included it also includes types.h so it gets included a second time there.
I hope this all makes sense. I purposely left it out of my original answer, but in the light of the comments it is probably better to explain it.