user3085931
user3085931

Reputation: 1803

undefined reference to own library function

I'm using QT(5)-Creator with 2 projects, that compile both with MinGW my C++ programs under Windows 7. One project generates a static library, and the other is just there to verify that the library is alright.

The error:

When building the library no errors are generated and it creates successfully the static lib MyClassName.a.

In turn, when trying to import the library, I get error messages durring building/compilation:

undefined reference to 'MyClassName::MyClassName()'
undefined reference to 'MyClassName::MyClassFunction()'

The files:

here's the main.cpp of my importer project

#include <QCoreApplication>
#include "MyClassName.h"
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    MyClassName *mainThread = new MyClassName();
    mainThread->MyClassFunction();

    return a.exec();
}

and it's .pro file

QT       += core
QT       += widgets
QT       -= gui

TARGET = TESTerase
CONFIG   += console
CONFIG   -= app_bundle
CONFIG   +=extserialport
CONFIG   +=staticlib

TEMPLATE = app

HEADERS += \
    MyClassName.h

SOURCES += main.cpp    

unix:!macx: LIBS += -L$$PWD/../MyClassName-Release/ -lMyClassName

INCLUDEPATH += $$PWD
INCLUDEPATH += $$PWD/../MyClassName-Release/release
DEPENDPATH += $$PWD/../MyClassName-Release/release

unix:!macx: PRE_TARGETDEPS += $$PWD/../MyClassName-Release/release/MyClassName.a

btw I've used QT-Creator's include external library function

My guesses:

Anyways searching the Internet about it usually just results in the hint, to use the same compiler for both projects - which shouldn't matter in my case (since I'm building both projects with the same Creator and the same settings).

Has anybody an idea?

Thanks for your support

Kind Regards

[Update for Bogdans request] the new .pro file

#-------------------------------------------------
#
# Project created by QtCreator 2015-04-29T19:46:22
#
#-------------------------------------------------

QT       += core
QT       += widgets

QT       -= gui

TARGET = TESTerase

TEMPLATE = app
CONFIG   += console
CONFIG   -= app_bundle
CONFIG   +=extserialport

HEADERS += \
    MyClassName.h

SOURCES += main.cpp

unix:!macx: LIBS += -L$$PWD/../MyClassName-Release/ -lMyClassName



win32:CONFIG(release, debug|release): LIBS += -L$$PWD/../MyClassName-Release/release/ -lMyClassName
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/../MyClassName-Release/debug/ -lMyClassName
else:unix:!macx: LIBS += -L$$PWD/../MyClassName-Release/ -lMyClassName

INCLUDEPATH += $$PWD/../MyClassName-Release/release
DEPENDPATH += $$PWD/../MyClassName-Release/release

win32-g++:CONFIG(release, debug|release): PRE_TARGETDEPS += $$PWD/../MyClassName-Release/release/MyClassName.a
else:win32-g++:CONFIG(debug, debug|release): PRE_TARGETDEPS += $$PWD/../MyClassName-Release/debug/MyClassName.a
else:win32:!win32-g++:CONFIG(release, debug|release): PRE_TARGETDEPS += $$PWD/../MyClassName-Release/release/MyClassName.lib
else:win32:!win32-g++:CONFIG(debug, debug|release): PRE_TARGETDEPS += $$PWD/../MyClassName-Release/debug/MyClassName.lib
else:unix:!macx: PRE_TARGETDEPS += $$PWD/../MyClassName-Release/MyClassName.a

Upvotes: 1

Views: 919

Answers (2)

Bartel
Bartel

Reputation: 411

Had the same problem just now. Build was working fine on macOS/OSX but not on Windows with the same codebase.

It was caused by the fact I was not exporting my symbols, which is necessary on Windows.

Use Q_DECL_EXPORT on functions when building the lib. And use Q_DECL_IMPORT when using the functions from the lib.

Example how to do this here: https://wiki.qt.io/How_to_create_a_library_with_Qt_and_use_it_in_an_application#Creating_a_shared_library

Solved the problem in my case, hope it helps!

Upvotes: 0

Bogdan
Bogdan

Reputation: 986

Your .pro file defines library dependencies only for unix platform unix:!macx:. I suppose there should be also win32 for Windows.

Upvotes: 1

Related Questions