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

Can't use a static library with QtCreator



  • Hello,
    I haven't coded in C++ for several years and re-installed QtCreator 4.2.1 with Qt 5.8.0 and mingw492_32 compiler on my laptop (Windows 8.1) a few weeks ago. I mean : for personal education and leisure only. I accepted all default options upon installing QtCreator.
    Now, I have a nice Project called Mathlib2 created as a C++ static library, with two files: mathlib2.h and mathlib2.cpp . This compiles without any problem and generated a library file libMathlib2.a with the Build folder: D:\Qt-apps\build-Mathlib2-Desktop_Qt_5_5_1_MinGW_32bit-Release\release .
    That sounds good but... I can't use the library ad share it with other Projects that need it because the build process can't find it the build path.
    (the only way i can build and run my other Projects is to create separate copies of mathlib2.h and mathlib2.cpp files and #include "mathlib2.h" in any .cpp file that needs it in each Project).
    I read the QtCreator documentation esp. chapter Build Environment , according to which there are some environment variables INCLUDE, LIB, LIBPATH that can be taylored to specific needs, but in my Build Environment shown below, theses variables can't be found in the list: alt text .
    I am not familiar at all with environment variables and don't know how to proceed to have my library within the build path and how to reformulate my #include "mathlib2.h" accordingly . Thanks for any help!


  • Lifetime Qt Champion

    Hi,

    Usually when you create a re-usable library, you put it in a central place and use your project .pro file to add the paths needed to the includes and library files.



  • @SGaist
    Thanks! Now I got something that works event though not exactly the way I wanted...
    I just edited manually my project .pro files (which I did dare do before) inserting the path to the library header and source files before the file names. That is a good first step anyway since I only one copy of library source files, and no longer multiple ones.
    I still wonder what is the correct syntax to use in the .pro file in order to refer the compiled static library ( .a file) instead of source files. Is the header file anyway needed to compile to client projects? Should I modify anything in my Build parameters?
    Next step after the next is to make and use a .dll but that is probably too much for me at the moment...
    Next after the next after the next will be to deploy locally my Projects (currently they can't run outside the QtCreator environment, due to missing dll's)
    Merci beaucoup! FT


  • Lifetime Qt Champion

    @TGVF You should read http://doc.qt.io/qt-5.8/qmake-variable-reference.html
    Use INCLUDEPATH to add diretory containing the header file (yes you need the header file - how else do you want to build your app if it uses that lib?).
    Use LIBS to add the lib itself to your project



  • @jsulm
    Yes, this document is key to understanding what is needed to build projects and it is new to me!
    I tried to modify my Test_header.pro file as follows:

    QT += core
    QT -= gui
    
    CONFIG += c++11
    
    TARGET = Test_header
    CONFIG += console
    CONFIG -= app_bundle
    
    TEMPLATE = app
    
    LIBS += "D:\Qt-apps\build-Mathlib2-Desktop_Qt_5_5_1_MinGW_32bit-Release\release"
    
    INCLUDEPATH += "D:\Qt-apps\Mathlib2"
    
    SOURCES += main.cpp \
    
    # The following define makes your compiler emit warnings if you use
    # any feature of Qt which as been marked deprecated (the exact warnings
    # depend on your compiler). Please consult the documentation of the
    # deprecated API in order to know how to port your code away from it.
    DEFINES += QT_DEPRECATED_WARNINGS
    
    # You can also make your code fail to compile if you use deprecated APIs.
    # In order to do so, uncomment the following line.
    # You can also select to disable deprecated APIs only up to a certain version of Qt.
    #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
    
    HEADERS += \
        D:\Qt-apps\Mathlib2\mathlib2.h
    
    

    With includes in the main as follows:

    #include "mathlib2.h"
    #include <cmath>
    #include <iomanip>
    #include <fstream>
    #include <cstring>
    

    But the build process fails with problem:

    erreur : cannot find D:\Qt-apps\build-Mathlib2-Desktop_Qt_5_5_1_MinGW_32bit-Release\release: Permission denied
    

    Why is "permission denied" for the folder where the library libMathlib2.a is located?


  • Lifetime Qt Champion

    1. Either use double backslashes or go with the unix notation and use one forward slash. Qt handles the transformation for you so I highly recommend you to use it (it's also valid for your code)
    2. For the LIBS variable, use -Lpaht/to/your/libs/folder
    3. Don't put mathlib2.h in the HEADERS variable. This variable is for the headers that are part for your project, not for its dependencies.


  • @SGaist said in Can't use a static library with QtCreator:

    Either use double backslashes or go with the unix notation and use one forward slash. Qt handles the transformation for you so I highly recommend you to use it (it's also valid for your code)

    I put forward slashes instead of backward slashes but that wasn't the sole problem. My main mistake was actually that I included the LIBS path up to the folder name only. Now, I introduced the fully qualified name of the individual library file and it works.
    My question is resolved; thanks a lot for your time and support!
    FT


Log in to reply