PDA

View Full Version : LNK2019 error using multiple source files



supernatrium
6th December 2019, 16:16
Hello community,

I can't run my hello world program, I get LNK2019 error.

main.cpp

#include <QCoreApplication>
#include "hello.h"

int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);

Hello *hi = new Hello();

return app.exec();
}

hello.h


#ifndef HELLO_H
#define HELLO_H

class Hello{
public:
Hello();
};

#endif // HELLO_H


hello.cpp


#include <iostream>

class Hello{
public:
Hello(){
std::cout << "hello world" << std::endl;
}
};



myproject.pro


QT -= gui

CONFIG += c++11 console
CONFIG -= app_bundle
DEFINES += QT_DEPRECATED_WARNINGS
SOURCES += \
hello.cpp \
main.cpp

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

HEADERS += \
hello.h


It works fine without creating Hello object.

d_stranz
6th December 2019, 17:44
Did you run qmake again after changing your .pro file to add the hello.* entries?

ChristianEhrlicher
7th December 2019, 07:25
You define your class Hello a second time in hello.cpp