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

Qt Creator / project organisation / shared files



  • Dear all,

    I would like to organise a project in subprojects. My problem is, that every subproject needs to access certain header files and C++ classes that do not form an executable nor a library and for this reason are not in a project.

    What is the correct way to manage those files? Is there a "best practice" how to do this?

    My idea was to put them in a subdirectory called "shared" and to create a "shared.pri" file in this directory that is included in every .pro file and adds the necessary HEADERS and SOURCES directives. So far, so good. However, when I open one of the .cpp files in the "shared" directory, Qt Creator displays errors because it does not find certain classes, e.g. QObject, although the directive "#include <QObject>" is present in the .cpp file.

    Any idea what I am doing wrong here?

    Thank you very much for your help.

    Kind regards,

    Ralf


  • Lifetime Qt Champion

    Try reverting the order of the libraries so first your static library and then the one from Visa.


  • Lifetime Qt Champion

    Hi,

    From the description you give these shared files do enter the definition of library content. You seem to want to reuse them in different parts of your other projects by both including and building them. So why not make these a library ?



  • Hello SGaist and thank you for your answer.

    I have started with Qt and C++ some months ago and consider myself as a beginner.

    If it is possible to put C++ classes into a library and if this is the recommended way, I will try to find out how this has to be done. These C++ classes are more or less (but not only) wrappers for C functions in third party libraries. The other files contain for example configuration data and helper functions that are needed by all subprojects.

    Kind regards,

    Ralf


  • Lifetime Qt Champion

    So I confirm, it really fits the content of a library.

    Take the time to make it properly with tests and everything. That will help you in the long run.



  • Hello SGaist,

    ok, I will try this but as I am new to this I run almost immediately into difficulties.

    As I wrote, the methods of my classes need to access a third party library that exists only as a DLL (a package that is installed separately and is not related to my project). When I try to build a static library with TEMPLATE = lib and CONFIG += staticlib, qmake seems to try to link this DLL statically to my library but this fails.

    How can I create a static library with methods that can access an external DLL? In the articles that I have found until now I have not found a solution.

    Thank you very much,

    Ralf


  • Lifetime Qt Champion

    Does that 3rd party library come with a header and library to link to ?

    Which compiler was used to build it ?
    Is it a C or C++ library ?



  • Yes and I am able to use it in a Qt project when I create an application, not a library. For information, the 3rd party library is the VISA library that comes for example from Keysight or National Instruments. The include file is "visa.h" and the library that has to be used during linking is "visa32.lib" or "ktvisa32.lib"

    I don't know which compiler they use to build it but it is probably not gcc and as far as I know, "visa32.dll" is a C library, not C++.

    When I create a DLL for my project, it seems to work. The problem arises when I try to create a static library.

    Ralf


  • Lifetime Qt Champion

    Ok, C library so you are safe compiler wise.

    What exact error do you get when you build your library as static library ?



  • Hello SGaist,

    ok, I will try to be as clear as possible. My library is called "VisaDevice" and is built from a file called "visadev.cpp". It contains the base class for my individual instruments (e.g. HP34401A). The methods of this class access the C functions in the 3rd party library "visa32.dll". When I build the "VisaDevice" library as a DLL everything is fine.

    As I have seen now, I can also build "VisaDevice" as a static library without a problem and I get a file called "libVisaDevice.a". However, during the linking stage of my subprojects the functions in the 3rd party library "visa32.dll" are not found for "visadef.cpp", although "visa32" is specified in the LIBS option of the project file:

    H:\Projekte\Messtechnik\build-Messplatz-Desktop_Qt_5_15_2_MinGW_32_bit-Debug\VisaDevice\..\..\Messplatz\VisaDevice\visadev.cpp:74: Fehler: undefined reference to `viClose@4'
    H:\Projekte\Messtechnik\build-Messplatz-Desktop_Qt_5_15_2_MinGW_32_bit-Debug\VisaDevice\debug/libVisaDevice.a(visadev.o): In function `ZN10VisaDeviceD2Ev':
    H:\Projekte\Messtechnik\build-Messplatz-Desktop_Qt_5_15_2_MinGW_32_bit-Debug\VisaDevice/../../Messplatz/VisaDevice/visadev.cpp:74: undefined reference to `viClose@4'
    

    These are my project files:

    1. VisaDevice.pro:
    QT -= gui
    TEMPLATE = lib
    CONFIG += c++11
    CONFIG += staticlib
    INCLUDEPATH += \
        "C:/Program Files (x86)/IVI Foundation/VISA/WinNT/Include" \
        "$$PWD/../VisaDevice"
    LIBS += \
        -L"C:/Program Files (x86)/IVI Foundation/VISA/WinNT/lib/msc" \
        -l"visa32"
    SOURCES += \
        visadev.cpp
    HEADERS += \
        visadev.h
    TRANSLATIONS += \
        VisaDevice_de_DE.ts
    # Default rules for deployment.
    unix {
        target.path = $$[QT_INSTALL_PLUGINS]/generic
    }
    !isEmpty(target.path): INSTALLS += target
    DISTFILES += \
        VisaDevice.pri
    
    1. VisaDevice.pri (included by the subprojects):
    INCLUDEPATH += \
        "C:/Program Files (x86)/IVI Foundation/VISA/WinNT/Include" \
        "$$PWD/../VisaDevice"
    LIBS += \
        -L"C:/Program Files (x86)/IVI Foundation/VISA/WinNT/lib/msc" \
        -l"visa32"
    LIBS += \
        -L"$$shadowed($$PWD)/debug" \
        -l"VisaDevice"
    

    and the project file of one of the subprojects:

    QT       += core gui
    greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
    CONFIG += c++11
    include($$PWD/../VisaDevice/VisaDevice.pri)
    SOURCES += \
        hp34401a.cpp \
        main.cpp \
        mainwindow.cpp
    HEADERS += \
        hp34401a.h \
        mainwindow.h
    FORMS += \
        mainwindow.ui
    TRANSLATIONS += \
        HP34401A_de_DE.ts
    # Default rules for deployment.
    qnx: target.path = /tmp/$${TARGET}/bin
    else: unix:!android: target.path = /opt/$${TARGET}/bin
    !isEmpty(target.path): INSTALLS += target
    

    I am sure that I am the culprit but what is my (beginners) mistake?

    Kind regards,

    Ralf


  • Lifetime Qt Champion

    Nothing stands out as wrong.

    Does your visa SDK provide more than one library ?



  • I am not sure to understand correctly.

    What do you mean by "more than one library"? More than one with the same name in different directories or more than one with different names for the same purpose? Or what else?

    As far as I know and can see, there are different libraries probably for the same purpose, "visa32.lib", "agvisa32.lib" and "ktvisa32.lib". This is because as far as I know, the standard library name is "visa32" but Keysight Technologies has added some functions and called it "ktvisa32" and Agilent probably "agvisa32".

    I have tried to link with these libraries too but also without success. For information, all three corresponding DLLs are installed in C:\Windows\System32.

    I have always linked with "visa32" because I don't use the Keysight extensions. I did never dive into the depths of compiler and linker options but could a setting in Qt Creator be wrong or missing?

    Ralf


  • Lifetime Qt Champion

    Try reverting the order of the libraries so first your static library and then the one from Visa.



  • Hello SGaist,

    thank you very much, this seems to have solved the problem. Compile with CONFIG += staticlib ok.

    Kind regards,

    Ralf


Log in to reply