Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Problem with linking library



  • Hi,
    I use Qt_5_9_4_MSVC2015_64bit. I try to write unit test for my classes.
    :-1: error: LNK1104: cannot open file 'myapp.lib'
    Can you help me to solve it, please?
    Is my structure o project is, ok?
    Structure of my project look like here:
    Game:
    |game.pro
    |test
    |@test.pro
    |@main.cpp
    |src
    |@src.pro
    |@source.h
    |@source.cpp
    |app
    |@app.pro
    |@main.cpp

    Files looks:
    game.pro:
    TEMPLATE = subdirs
    CONFIG += ordered

    SUBDIRS +=
    src
    tests
    app

    app.depends = src
    tests.depends = src
    ......................................................
    app.pro
    include(../defaults.pri)

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

    SOURCES += main.cpp

    LIBS += -L../src -lmyapp
    ......................................................
    main.cpp
    #include <iostream>
    int main(int argc, char *argv[])
    {
    std::cout << "main in app";
    return 0;
    }
    ......................................................

    src.pro
    include(../defaults.pri)
    CONFIG -= qt

    TARGET = myapp
    TEMPLATE = lib

    SOURCES +=
    source.cpp
    HEADERS +=
    source.h
    ......................................................
    source.h
    #ifndef SOURCE_H
    #define SOURCE_H

    class Source
    {
    public:
    Source();
    };

    #endif // SOURCE_H

    source.cpp
    #include "source.h"

    Source::Source()
    {

    }
    ......................................................
    tests.pro
    include(../defaults.pri)
    TEMPLATE = app

    CONFIG += console
    CONFIG -= app_bundle
    CONFIG += qt qwidgets
    QT += testlib

    HEADERS += cardsbuildertest.h
    SOURCES +=
    cardsbuildertest.cpp
    main.cpp
    LIBS += -L../src -lmyapp
    ......................................................
    main.cpp
    #include "cardsbuildertest.h"
    #include <source.h>
    int main(int argc, char *argv[])
    {

    Source source = Source();
    return 0;
    }


  • Lifetime Qt Champion

    Hi @kokodyn, welcome.

    What is the problem you encounter?



  • I forgot to add a problem code, this is:
    :-1: error: LNK1104: cannot open file 'myapp.lib'
    Can you help me to solve it, please?
    Is my project structure is ok?
    After dividing tests and source code, I had a problem to include headers from source code to use it in test.


  • Lifetime Qt Champion

    @kokodyn without having a deeper look, you need to asure two things:

    1. make sure the lib is built before anything else
    2. give the linker the (relative) path to your lib

    I only have the phone now, so no deeper analysys possible from my side...



  • @aha_1980 thanks for answer!
    The problem was because myapp.lib was never created.
    It was caused by lack of:
    __declspec(dllexport)
    In class source.h
    Without this only myapp.dll file was created.
    Is my project structure is ok? Spliting to directories and static linking tests and source code is the way how it is should be done?


  • Lifetime Qt Champion

    @kokodyn from a short glance, the structure is good.

    If you prefer static or dynamic linking is a matter of taste, just mind the license you are using and if it allows static linking.


Log in to reply