I have been struggling for a while with an issue on Qt.
Here is my code:
hexbutton.h:
#ifndef HEXBUTTON_H
#define HEXBUTTON_H
#include <QPushButton>
#include <QWidget>
#include <QIcon>
class HexButton : public QPushButton
{
Q_OBJECT
public:
HexButton(QWidget *parent, QIcon &icon, int i, int j);
public slots:
void changeIcon();
};
#endif // HEXBUTTON_H
Hexbutton.cpp:
#include "hexbutton.h"
HexButton::HexButton(QWidget *parent, QIcon &icon, int i , int j) : QPushButton(parent){
//setFlat(true);
setIcon(icon);
setGeometry((i*40)+((j*40)/2), j*40, 40, 40);
}
void HexButton::changeIcon(){
setIcon(QIcon("/Users/jonathanbibas/Documents/Workspace Qt/Test/hexagon.gif"));
}
MyWindow.h:
#ifndef MYWINDOW_H
#define MYWINDOW_H
#include <QApplication>
#include <QWidget>
#include <QPushButton>
#include <QLCDNumber>
#include <QSlider>
#include <QProgressBar>
#include "hexbutton.h"
class MyWindow : public QWidget
{
public:
MyWindow();
~MyWindow();
private:
HexButton * myButtons[11][11];
};
#endif // MYWINDOW_H
MyWindow.cpp:
#include "MyWindow.h"
#include <QColor>
#include <QIcon>
MyWindow::MyWindow() : QWidget() {
setFixedSize(740, 440);
QIcon icon = QIcon("/Users/jonathanbibas/Documents/Workspace Qt/Test/whitehexagon.png");
for(int i =0 ; i < 11 ; i ++){
for(int j =0 ; j < 11 ; j ++){
myButtons[i][j] = new HexButton(this, icon, i, j);
QObject::connect(myButtons[i][j], SIGNAL(clicked()), myButtons[i][j], SLOT(changeIcon()));
}
}
}
MyWindow::~MyWindow()
{
delete myButtons;
}
And finally, Main.cpp:
#include <QApplication>
#include "MyWindow.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MyWindow fenetre;
fenetre.show();
return app.exec();
}
Just in case, here is the Test.pro
SOURCES += \
Main.cpp \
MyWindow.cpp \
hexbutton.cpp
HEADERS += \
MyWindow.h \
hexbutton.h
And I get the 2 errors:
1) symbol(s) not found for architecture x86_64
2) collect2: ld returned 1 exit status
It also says 121 times (11*11):
Object::connect: No such slot QPushButton::changeIcon() in ../Test/MyWindow.cpp:19
and on the compile output it says:
18:22:15: Running build steps for project Test...
18:22:15: Configuration unchanged, skipping qmake step.
18:22:15: Starting: "/usr/bin/make" -w
make: Entering directory `/Users/jonathanbibas/Documents/Workspace Qt/Test-build-desktop-Desktop_Qt_4_8_0_for_GCC__Qt_SDK__Debug'
g++ -c -pipe -g -gdwarf-2 -arch x86_64 -Xarch_x86_64 -mmacosx-version-min=10.5 -Wall -W -DQT_GUI_LIB -DQT_CORE_LIB -I../../../QtSDK/Desktop/Qt/4.8.0/gcc/mkspecs/macx-g++ -I../Test -I../../../QtSDK/Desktop/Qt/4.8.0/gcc/lib/QtCore.framework/Versions/4/Headers -I../../../QtSDK/Desktop/Qt/4.8.0/gcc/include/QtCore -I../../../QtSDK/Desktop/Qt/4.8.0/gcc/lib/QtGui.framework/Versions/4/Headers -I../../../QtSDK/Desktop/Qt/4.8.0/gcc/include/QtGui -I../../../QtSDK/Desktop/Qt/4.8.0/gcc/include -I. -I../Test -I. -F/Users/jonathanbibas/QtSDK/Desktop/Qt/4.8.0/gcc/lib -o hexbutton.o ../Test/hexbutton.cpp
g++ -headerpad_max_install_names -arch x86_64 -Xarch_x86_64 -mmacosx-version-min=10.5 -o Test.app/Contents/MacOS/Test Main.o MyWindow.o hexbutton.o moc_MyWindow.o -F/Users/jonathanbibas/QtSDK/Desktop/Qt/4.8.0/gcc/lib -L/Users/jonathanbibas/QtSDK/Desktop/Qt/4.8.0/gcc/lib -framework QtGui -framework QtCore
Undefined symbols for architecture x86_64:
"vtable for HexButton", referenced from:
HexButton::HexButton(QWidget*, QIcon&, int, int)in hexbutton.o
HexButton::HexButton(QWidget*, QIcon&, int, int)in hexbutton.o
NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
make: *** [Test.app/Contents/MacOS/Test] Error 1
make: Leaving directory `/Users/jonathanbibas/Documents/Workspace Qt/Test-build-desktop-Desktop_Qt_4_8_0_for_GCC__Qt_SDK__Debug'
18:22:20: The process "/usr/bin/make" exited with code 2.
Error while building project Test (target: Desktop)
When executing build step 'Make'
Apparently the error comes from the Q_OBJECT (needed for the slots definition), but there is something wrong with my code, not with my compiler (because I have when slots are in MainWindow, it works fine).
Thank you for your help!
Faced same problem
undefined refrence error for my signal.
After putting Q_OBJECT macro... vtable errors.
I did this and worked for me
Added Q_OBJECT to file
Cleaned project
Ran qmake
Rebuild
it compiles fine.
Got the same error. I had a private slot declared in a .h file, but I didn't have it defined in my cpp file. I removed my unused slot in both places, and it compiled.
This happened to me when merging Qt code from one platform to another. I just resolved it, after much trial and error, so I'm posting it here since my issue was different. If you are merging new files, you can't just merge the files and the .Pro file and expect that they'll show up in Qt Creator for compilation. Notice your new files aren't actually being displayed in the IDE under your folders. I overlooked this, and was just looking at the folder structure and .Pro file. Once I manually added all of the files in Qt, it compiled fine. Here is how I figured this out:
Right click on the vague "symbol(s) not found for architecture x86_64" error, and select Compiled Output
At the bottom it will show what method(s) it can't find. Find what file(s) those methods are in, and add those headers and class files, by right clicking the folder they should be in the IDE, and selecting "Add Existing File"
Build, or Clean then Build
CONFIG -= x86_64 fixed this problem for me
This can happen, when you forgot the Class-Operator, when defining your function.
H File:
class Globals
{
public:
unsigned int myFunction();
};
CPP File:
unsigned int Globals::myFunction(){return 3;}
I found the answer finally! (for whoever will be interested)
The mistake is that I shouldn't have extended the class QPushButton, to customize my button, but rather, create my own class HexButton extending/inheriting the QWidget class!
Doing so we must add to the button, a QPushButton instance.
Well it then makes:
class HexButton : public QWidget
{
Q_OBJECT
public:
HexButton(QWidget *parent = 0);
public slots:
void changeIcon();
private:
QPushButton *button;
};
Move the slot changeIcon() into HexButton class from MyWindow class.
Normally this happens with Qt because of your build system - it indicates that you're not compiling/linking in the code generated by moc
I'm pretty sure that there's nothing wrong with your code - so the error must be in some setting in qt creator, which I can't really help with as I've not used it much.
If you haven't already, you could try adding an explicit empty destructor declaration to hexbutton.hpp and definition to hexbutton.cpp. It shouldn't be necessary, but I've heard of this making a difference on some quirky compilers/linkers.
try to remove pro.user file and reimport project with any qt 32bit kit.
Related
I know this question was already asked like a thousand times but i tried every solution and nothing helped.
I made a little program in Qt and after a while i got this error message:
symbol(s) not found for architecture x86_64
linker command failed with exit code 1 (use -v to see invocation)
I redid the qmake command and the rebuild project, nothing worked.
I'm new in Qt.
I am using Qt version 5.10.0 on mac os 10.13.3
Here are my files:
gui.h
#ifndef GUI_H
#define GUI_H
#include <QWidget>
#include <QPainter>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QPushButton>
#include <QLineEdit>
class gui : public QWidget
{
Q_OBJECT
public:
gui(QWidget *parent = 0);
~gui();
private:
QHBoxLayout *hbox1;
QHBoxLayout *hbox2;
QVBoxLayout *vbox;
QPushButton *search;
QPushButton *replace;
QLineEdit *searchText;
QLineEdit *replaceText;
QLineEdit *textField;
public slots:
void find();
void replaceFuckingText();
};
#endif // GUI_H
gui.cpp
#include "gui.h"
gui::gui(QWidget *parent)
: QWidget(parent)
{
hbox1 = new QHBoxLayout();
hbox2 = new QHBoxLayout();
vbox = new QVBoxLayout();
search = new QPushButton("Search");
replace = new QPushButton("Replace");
searchText = new QLineEdit();
replaceText = new QLineEdit();
textField = new QLineEdit();
hbox1->addWidget(searchText);
hbox1->addWidget(replaceText);
vbox->addLayout(hbox1);
hbox2->addWidget(search);
hbox2->addWidget(replace);
vbox->addLayout(hbox2);
vbox->addWidget(textField);
setLayout(vbox);
show();
connect(replace,SIGNAL(clicked()), this, SLOT(replaceFuckingText()));
}
gui::~gui()
{
}
void gui::replaceFuckingText() {
QString searchTextValue = searchText->text();
QString replaceTextValue = replaceText->text();
QString textToReplace = textField->text();
textToReplace.replace(searchTextValue,replaceTextValue);
textField->setText(textToReplace);
}
main.cpp:
#include "gui.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
gui w;
w.show();
return a.exec();
}
I hope you can help me. I'm working on this error for over a week now.
If you need more informations feel free to ask and I'll post them.
symbol(s) not found for architecture x86_64
on MacOS generally is not difficult to diagnose and generally it comes from something you defined in headers but does not have proper implementation!
to know why exactly you got this message click Compiler Output in Qt Creator, most likely you will see where the make error is coming from, in your code case I see below error:
Undefined symbols for architecture x86_64: "gui::find()", referenced
from:
gui::qt_static_metacall(QObject*, QMetaObject::Call, int, void**) in moc_gui.o ld: symbol(s) not found for architecture x86_64
from this message it looks that you have declared gui::find() slot or method in a header, but no where in your cpp that slot has any implementation!
So all what you need is to add code for slot gui::find() in your cpp file.
When I add the following to your gui.cpp, the code compiles without issues:
void gui::find()
{
// do some staff
}
If you're really tired and accidentally add the .cpp file to HEADERS instead of SOURCES in your .pro file, you can also get this error.
I have naturally trivial question as I mean: we press button --> counter increases, counter increases --> QLabel's value is renewed. I caught strange error and don't want to do. I'm not dummy in C++ but in QT I am. It's my first and most trivial application in it.
Some answers there (on Stack Overflow) advised to add virtual constructor. It has no effect.
I tried to rewrite signals and slots to new qt5 style but there were another problems, I was too lazy to fix them, was it (rewriting, not laziness :) ) a good way, maybe problem is really with versions?
I just haven't tried to reinstall QT or install Qt4, maybe problem is in it?
about versions:
$ qmake --version
responds:
QMake version 3.0
Using Qt version 5.5.1 in /usr/lib/x86_64-linux-gnu
conn.pro:
TEMPLATE = app
QT += core gui
TARGET = conn
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
SOURCES += main.cpp
main.cpp:
#include <QApplication>
#include <QLabel>
#include <QPushButton>
#include <QObject>
class Counter : public QObject {
Q_OBJECT
private:
double i_;
public:
virtual ~Counter()
{
}
Counter() : QObject(), i_(0)
{
}
public slots:
void slot_incCounter();
signals:
void goodbye(){}
void counterChanged(double){}
};
void Counter::slot_incCounter() {
emit counterChanged(++i_);
if (i_ == 5) {
emit goodbye();
}
}
int main(int argc, char* argv[]) {
QApplication my_app(argc, argv);
QLabel label1("label i created");
label1.show();
QPushButton button1("press me");
button1.show();
Counter counter1;
QObject::connect(&button1, SIGNAL(clicked()),
&counter1, SLOT(slot_incCounter()));
QObject::connect(&counter1, SIGNAL(counterChanged(double a)),
&label1, SLOT(setNum(double a)));
QObject::connect(&counter1, SIGNAL(goodbye()),
&my_app, SLOT(quit()));
return my_app.exec();
}
Try to run it:
qmake && make && ./conn
So I see in console:
g++ -m64 -Wl,-O1 -o conn main.o -L/usr/X11R6/lib64 -lQt5Widgets -lQt5Gui -lQt5Core -lGL -lpthread
main.o: In function `main':
main.cpp:(.text.startup+0xd6): undefined reference to `vtable for Counter'
collect2: error: ld returned 1 exit status
Makefile:144: recipe for target 'conn' failed
make`:` *** [conn] Error 1
What should I do?
Qt uses the meta object compiler (moc) to enable e.g. signal and slots. By default it works perfectly if the Q_OBJECT macro is in a header file. So the easiest would be you put Counter into it's own header/implementation file, rerun qmake and make. (That's by the way good practice...)
If you want to stick with a single main.cpp file you need to tell the moc explicitly that this file contains macros moc needs to parse. You do this with the following line at the very end of main.cpp:
#include "main.moc"
Then also rerun qmake and make.
Please keep in mind that the manually including a moc-include directive is not the best choice. So better split your C++ classes into separate files right from the beginning...
Thank you very much! Your answer was full, useful and making all more obvious.
Solution was:
1. Move class Counter to Counter.h
Since this moment the message about vtable disappeared. Appeared messages that goodbye() and Counter::counterChanged(double) have multiple definition. The first definition was mine in Counter.cpp (WRONG WAY). The second was in moc_Counter.cpp, generated by MOC utility. So:
2. Remove definitions (my empty definitions) of signal functions, because moc makes its own in file moc_Counter.cpp:
// SIGNAL 0
void Counter::goodbye()
{
QMetaObject::activate(this, &staticMetaObject, 0, Q_NULLPTR);
}
// SIGNAL 1
void Counter::counterChanged(double _t1)
{
void *_a[] = { Q_NULLPTR, const_cast<void*>(reinterpret_cast<const void*>(&_t1)) };
QMetaObject::activate(this, &staticMetaObject, 1, _a);
}
and they cause a problem of multiple definition.
Summing it up, working code:
main.cpp:
#include <QApplication>
#include "Counter.h"
int main(int argc, char* argv[]) {
QApplication my_app(argc, argv);
QLabel label1("1");
label1.show();
QPushButton button1("press me");
button1.show();
Counter counter1;
QObject::connect(&button1, SIGNAL(clicked()),
&counter1, SLOT(slot_incCounter()));
QObject::connect(&counter1, SIGNAL(counterChanged(double)),
&label1, SLOT(setNum(double)));
QObject::connect(&counter1, SIGNAL(goodbye()),
&my_app, SLOT(quit()));
return my_app.exec();
}
void Counter::slot_incCounter() {
emit counterChanged(++i_);
if (i_ == 5) {
emit goodbye();
}
}
Counter.h:
#ifndef COUNTER_H
#define COUNTER_H
#include <QLabel>
#include <QPushButton>
#include <QObject>
class Counter : public QObject {
Q_OBJECT
private:
double i_;
public:
virtual ~Counter()
{
}
Counter() : QObject()
{
}
public slots:
void slot_incCounter();
signals:
void goodbye();
void counterChanged(double);
};
#endif // COUNTER_H
Counter.cpp:
#include "Counter.h"
Thank you, you're great!
I'm practicing with gtk (or gtkmm in this case), to which I'm completely new and I'm relatively new to c++. I got a working program that could open a window and put a few widgets in it, but now I'm trying to add an action to a button, and it just won't work.
main.cc:
#include <iostream>
#include "buttons.h"
#include <gtkmm/application.h>
void printLine()
{
std::cout<<"you pressed the button"<<std::endl;
}
int main(int argc, char *argv[])
{
Glib::RefPtr<Gtk::Application> app =
Gtk::Application::create(argc, argv,
"org.gtkmm.examples.base");
Buttons buttons;
return app->run(buttons);
}
buttons.h:
#ifndef GTKMM_EXAMPLE_BUTTONS_H
#define GTKMM_EXAMPLE_BUTTONS_H
#include <gtkmm/window.h>
#include <gtkmm/button.h>
#include <gtkmm/box.h>
class Buttons : public Gtk::Window
{
public:
Buttons();
virtual ~Buttons();
protected:
//Signal handlers:
void on_button_clicked();
//Child widgets:
Gtk::Button m_button;
Gtk::Box buttonBox;
};
#endif //GTKMM_EXAMPLE_BUTTONS_H
buttons.cc:
#include <iostream>
#include "buttons.h"
Buttons::Buttons()
{
m_button.add_pixlabel("info.xpm", "click here");
set_title("Pixmap'd buttons!");
set_border_width(10);
m_button.signal_clicked().connect( sigc::mem_fun(*this,
&Buttons::on_button_clicked) );
add(buttonBox);
buttonBox.pack_start(m_button);
//m_button.show();
show_all_children();
}
Buttons::~Buttons()
{
}
void Buttons::on_button_clicked()
{
printLine();
}
I am using g++ to compile the program and it gives me this error message:
g++ main.cc -o button pkg-config gtkmm-3.0 --cflags --libs
/tmp/ccKyphYe.o: In function main':
main.cc:(.text+0x93): undefined reference toButtons::Buttons()'
main.cc:(.text+0xc5): undefined reference to Buttons::~Buttons()'
main.cc:(.text+0x124): undefined reference toButtons::~Buttons()'
collect2: error: ld returned 1 exit status
You have to put all your source files in the compile line, so just add buttons.cc right after main.cc and you should be good. There are other ways to do it, but just to get you going, that should work.
The longer answer is that the compiler compiles each src file (.cc files in your example) separately and builds object files (.o or .obj). To do this, all it needs are the declarations of the things it uses (#include'd in header files). If they are missing, you get a "compiler error".
But later when it actually puts together the final program that you are going to run, it needs the actual definitions (the actual code) for everything that is used, and if it can't find the actual definition, you get "undefined reference" errors. This is called a "linker error". This means you are missing libraries, archives, or object (.obj) files.
HOWEVER, when you put everything on the same compiler line -- all your c++ src files including one with a main() function, the compiler automatically generates the object files and does the linking all in one step.
I have been struggling for a while with an issue on Qt.
Here is my code:
hexbutton.h:
#ifndef HEXBUTTON_H
#define HEXBUTTON_H
#include <QPushButton>
#include <QWidget>
#include <QIcon>
class HexButton : public QPushButton
{
Q_OBJECT
public:
HexButton(QWidget *parent, QIcon &icon, int i, int j);
public slots:
void changeIcon();
};
#endif // HEXBUTTON_H
Hexbutton.cpp:
#include "hexbutton.h"
HexButton::HexButton(QWidget *parent, QIcon &icon, int i , int j) : QPushButton(parent){
//setFlat(true);
setIcon(icon);
setGeometry((i*40)+((j*40)/2), j*40, 40, 40);
}
void HexButton::changeIcon(){
setIcon(QIcon("/Users/jonathanbibas/Documents/Workspace Qt/Test/hexagon.gif"));
}
MyWindow.h:
#ifndef MYWINDOW_H
#define MYWINDOW_H
#include <QApplication>
#include <QWidget>
#include <QPushButton>
#include <QLCDNumber>
#include <QSlider>
#include <QProgressBar>
#include "hexbutton.h"
class MyWindow : public QWidget
{
public:
MyWindow();
~MyWindow();
private:
HexButton * myButtons[11][11];
};
#endif // MYWINDOW_H
MyWindow.cpp:
#include "MyWindow.h"
#include <QColor>
#include <QIcon>
MyWindow::MyWindow() : QWidget() {
setFixedSize(740, 440);
QIcon icon = QIcon("/Users/jonathanbibas/Documents/Workspace Qt/Test/whitehexagon.png");
for(int i =0 ; i < 11 ; i ++){
for(int j =0 ; j < 11 ; j ++){
myButtons[i][j] = new HexButton(this, icon, i, j);
QObject::connect(myButtons[i][j], SIGNAL(clicked()), myButtons[i][j], SLOT(changeIcon()));
}
}
}
MyWindow::~MyWindow()
{
delete myButtons;
}
And finally, Main.cpp:
#include <QApplication>
#include "MyWindow.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MyWindow fenetre;
fenetre.show();
return app.exec();
}
Just in case, here is the Test.pro
SOURCES += \
Main.cpp \
MyWindow.cpp \
hexbutton.cpp
HEADERS += \
MyWindow.h \
hexbutton.h
And I get the 2 errors:
1) symbol(s) not found for architecture x86_64
2) collect2: ld returned 1 exit status
It also says 121 times (11*11):
Object::connect: No such slot QPushButton::changeIcon() in ../Test/MyWindow.cpp:19
and on the compile output it says:
18:22:15: Running build steps for project Test...
18:22:15: Configuration unchanged, skipping qmake step.
18:22:15: Starting: "/usr/bin/make" -w
make: Entering directory `/Users/jonathanbibas/Documents/Workspace Qt/Test-build-desktop-Desktop_Qt_4_8_0_for_GCC__Qt_SDK__Debug'
g++ -c -pipe -g -gdwarf-2 -arch x86_64 -Xarch_x86_64 -mmacosx-version-min=10.5 -Wall -W -DQT_GUI_LIB -DQT_CORE_LIB -I../../../QtSDK/Desktop/Qt/4.8.0/gcc/mkspecs/macx-g++ -I../Test -I../../../QtSDK/Desktop/Qt/4.8.0/gcc/lib/QtCore.framework/Versions/4/Headers -I../../../QtSDK/Desktop/Qt/4.8.0/gcc/include/QtCore -I../../../QtSDK/Desktop/Qt/4.8.0/gcc/lib/QtGui.framework/Versions/4/Headers -I../../../QtSDK/Desktop/Qt/4.8.0/gcc/include/QtGui -I../../../QtSDK/Desktop/Qt/4.8.0/gcc/include -I. -I../Test -I. -F/Users/jonathanbibas/QtSDK/Desktop/Qt/4.8.0/gcc/lib -o hexbutton.o ../Test/hexbutton.cpp
g++ -headerpad_max_install_names -arch x86_64 -Xarch_x86_64 -mmacosx-version-min=10.5 -o Test.app/Contents/MacOS/Test Main.o MyWindow.o hexbutton.o moc_MyWindow.o -F/Users/jonathanbibas/QtSDK/Desktop/Qt/4.8.0/gcc/lib -L/Users/jonathanbibas/QtSDK/Desktop/Qt/4.8.0/gcc/lib -framework QtGui -framework QtCore
Undefined symbols for architecture x86_64:
"vtable for HexButton", referenced from:
HexButton::HexButton(QWidget*, QIcon&, int, int)in hexbutton.o
HexButton::HexButton(QWidget*, QIcon&, int, int)in hexbutton.o
NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
make: *** [Test.app/Contents/MacOS/Test] Error 1
make: Leaving directory `/Users/jonathanbibas/Documents/Workspace Qt/Test-build-desktop-Desktop_Qt_4_8_0_for_GCC__Qt_SDK__Debug'
18:22:20: The process "/usr/bin/make" exited with code 2.
Error while building project Test (target: Desktop)
When executing build step 'Make'
Apparently the error comes from the Q_OBJECT (needed for the slots definition), but there is something wrong with my code, not with my compiler (because I have when slots are in MainWindow, it works fine).
Thank you for your help!
Faced same problem
undefined refrence error for my signal.
After putting Q_OBJECT macro... vtable errors.
I did this and worked for me
Added Q_OBJECT to file
Cleaned project
Ran qmake
Rebuild
it compiles fine.
Got the same error. I had a private slot declared in a .h file, but I didn't have it defined in my cpp file. I removed my unused slot in both places, and it compiled.
This happened to me when merging Qt code from one platform to another. I just resolved it, after much trial and error, so I'm posting it here since my issue was different. If you are merging new files, you can't just merge the files and the .Pro file and expect that they'll show up in Qt Creator for compilation. Notice your new files aren't actually being displayed in the IDE under your folders. I overlooked this, and was just looking at the folder structure and .Pro file. Once I manually added all of the files in Qt, it compiled fine. Here is how I figured this out:
Right click on the vague "symbol(s) not found for architecture x86_64" error, and select Compiled Output
At the bottom it will show what method(s) it can't find. Find what file(s) those methods are in, and add those headers and class files, by right clicking the folder they should be in the IDE, and selecting "Add Existing File"
Build, or Clean then Build
CONFIG -= x86_64 fixed this problem for me
This can happen, when you forgot the Class-Operator, when defining your function.
H File:
class Globals
{
public:
unsigned int myFunction();
};
CPP File:
unsigned int Globals::myFunction(){return 3;}
I found the answer finally! (for whoever will be interested)
The mistake is that I shouldn't have extended the class QPushButton, to customize my button, but rather, create my own class HexButton extending/inheriting the QWidget class!
Doing so we must add to the button, a QPushButton instance.
Well it then makes:
class HexButton : public QWidget
{
Q_OBJECT
public:
HexButton(QWidget *parent = 0);
public slots:
void changeIcon();
private:
QPushButton *button;
};
Move the slot changeIcon() into HexButton class from MyWindow class.
Normally this happens with Qt because of your build system - it indicates that you're not compiling/linking in the code generated by moc
I'm pretty sure that there's nothing wrong with your code - so the error must be in some setting in qt creator, which I can't really help with as I've not used it much.
If you haven't already, you could try adding an explicit empty destructor declaration to hexbutton.hpp and definition to hexbutton.cpp. It shouldn't be necessary, but I've heard of this making a difference on some quirky compilers/linkers.
try to remove pro.user file and reimport project with any qt 32bit kit.
I have the code:
#include <iostream>
#include <QThread>
#include <unistd.h>
#include <stdlib.h>
#include <QApplication>
using std::cerr;
using std::endl;
class Thread : public QThread
{
Q_OBJECT
public:
Thread();
~Thread();
void setMessage(const QString &_message);
void stop();
protected:
void run();
private:
QString message;
volatile bool stopped;
};
Thread::Thread()
{
stopped = false;
run();
}
Thread::~Thread()
{
}
void Thread::run()
{
while(!stopped){
cerr << qPrintable(message);
sleep(1);
}
stopped = false;
cerr << endl;
}
void Thread::stop()
{
stopped = true;
}
void Thread::setMessage(const QString &_message)
{
message = _message;
}
int main(int argc,char *argv[])
{
QApplication app(argc, argv);
Thread *A,*B;
A = new Thread();
B = new Thread();
A->setMessage("Thread A\n");
B->setMessage("Thread B\n");
//.run();
//.run();
sleep(10);
A->stop();
B->stop();
return 0;
}
and i have error
g++ -Wl,--hash-style=gnu -Wl,--as-needed -Wl,-O1 -o tmp main.o -L/usr/lib -lQtGui -lQtCore -lpthread
main.o: In function `Thread::~Thread()':
main.cpp:(.text+0xa): undefined reference to `vtable for Thread'
main.o: In function `Thread::Thread()':
main.cpp:(.text+0x1da): undefined reference to `vtable for Thread'
collect2: ld returned 1 exit status
make: *** [tmp] Error 1
You must generate a header with moc. This can be done automatically with the Qt build system. Instead of using gcc directly, you should use a qmake file.
Also you should probably separate the declaration and code into header file and cpp file.
Here is a description of what moc does: http://doc.qt.nokia.com/latest/moc.html
And a similar question (with answers) here on stackoverflow:
Undefined reference to vtable. Trying to compile a Qt project
You need to have a line at the bottom of your source file:
#include "main.moc"
That's because the declaration of class Thread isn't in a header - it's in a .cpp file. So by default moc won't run on it. Adding the line does two things:
it signals to qmake and moc that moc has to process the .cpp file
it causes the stuff that moc generates to be pulled in by the compile step
So after adding that line you'll need to rerun qmake so it can update the makefiles to cause main.moc to be generated.
Normally, moc runs against header files and creates .cpp files that get included in the build (qmake sees to this). This 'trick' causes moc to also be run on the .cpp filein question (and to have the moc generated code compiled in).
An alternative to including main.moc at the end of main.cpp is to move the definition of class Thread to a .h header file and #include that. If the definition is in a header qmake and moc should handle things automatically.
I think that the qmake system requires that your header file include directly. Mine was failing to generate a moc until I added that include.
Simple add string to .pro file. This was helping for me in same problem
INSTALLS += target
See queudcustomtype example.