I am a beginner in qt and was attempting to get an online tutorial example running. I got CMake to build the project and I did not use .pro file or .qml file or qmake. while there seem to be some linking errors while compiling.
Undefined symbols for architecture x86_64:
"Counter::valueChanged(int)", referenced from:
Counter::setValue(int) in test_1.cpp.o
"vtable for Counter", referenced from:
Counter::Counter() in test_1.cpp.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
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I tried all the solutions in the similar questions asked on Stackflow but they seem not to work since most of them suggested to re-run the Qmake , which I did not use.
Here are my source files
#include <QObject>
class Counter : public QObject
{
Q_OBJECT
public:
Counter() { m_value = 0; }
~Counter(){ delete(this); }
int value() const { return m_value; }
public slots:
void setValue(int value)
{
if (value != m_value) {
m_value = value;
emit valueChanged(value);
}
}
signals:
void valueChanged(int newValue);
private:
int m_value;
};
int main(){
Counter a, b;
QObject::connect(&a, SIGNAL(valueChanged(int)),
&b, SLOT(setValue(int)));
a.setValue(12); // a.value() == 12, b.value() == 12
b.setValue(48); // a.value() == 12, b.value() == 48
}
and the CmakeList.txt
cmake_minimum_required(VERSION 3.15)
project(chart_system)
set(CMAKE_CXX_STANDARD 11)
add_executable(chart_system test_1.cpp)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)
find_package(Qt5Core)
find_package(Qt5Widgets)
target_link_libraries(
${PROJECT_NAME}
Qt5::Core
Qt5::Gui
Qt5::Widgets
Qt5::Charts
)
qt5_use_modules(${PROJECT_NAME} Core Widgets OpenGL Xml Gui Charts)
The two files above are all the files I got in the project. Save me out please😢
First of all I recommend you look for a more tutorial updated or better use the official Qt documentation:
In your case you have several errors:
You do not need to delete the object itself in the destroyer, the objective of the destroyer is to free up resources handled by the class.
Do not use the old connection style because they hide errors, better use the new connection style.
If in the main file you create a class that implements signals, slots, etc. then you must include "name_of_file.moc" since this file that is created by the MOC is necessary to compile.
For the signals and slots functions you must create an eventloop, that is, create a QXApplication and call exec(), in your case, just QCoreApplication.
You are using very old CMake instructions for Qt, I recommend you check the links above.
test_1.cpp
#include <QCoreApplication>
#include <QObject>
#include <QDebug>
class Counter : public QObject
{
Q_OBJECT
public:
Counter(QObject *parent=nullptr): QObject(parent) { m_value = 0; }
~Counter(){}
int value() const { return m_value; }
public slots:
void setValue(int value)
{
if (value != m_value) {
m_value = value;
emit valueChanged(value);
}
}
signals:
void valueChanged(int newValue);
private:
int m_value;
};
int main(int argc, char *argv[]){
QCoreApplication app(argc, argv);
Counter a, b;
QObject::connect(&a, &Counter::valueChanged, &b, &Counter::setValue);
QObject::connect(&b, &Counter::valueChanged, [](int value){
qDebug() << value;
});
a.setValue(12);
return app.exec();
}
#include "test_1.moc"
CMakeLists.txt
cmake_minimum_required(VERSION 3.1.0)
project(chart_system)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_AUTOMOC ON)
if(CMAKE_VERSION VERSION_LESS "3.7.0")
set(CMAKE_INCLUDE_CURRENT_DIR ON)
endif()
find_package(Qt5 COMPONENTS Core REQUIRED)
add_executable(chart_system test_1.cpp)
target_link_libraries(
${PROJECT_NAME}
Qt5::Core
)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.