create Staticlib



  • Hi, i did a simple static lib i called CN16K_SFTP for test,
    i can compile the lib to generate the .a file and use it from another project.

    //including my static lib to another project
    LIBS += -L../CN16K_SFTP/build/release -llibCN16K_SFTP
    INCLUDEPATH += ../CN16K_SFTP

    My goal i to add some SFTP file transfert functions my static lib, so i add libssh

    // .pro of CN16K_SFTP

    LIBS += -L$$PWD -lssh
    HEADERS += \
            cn16k_sftp.h \
        legacy.h \
        libssh.h \
        libsshpp.hpp \
        server.h \
        sftp.h \
        ssh2.h
    

    #include "sftp.h"

    then i write simple connexion function.

    Now if i try to use my static lib from a project i get errors

    • list itemmain.cpp:-1: erreur : undefined reference to `__imp_ssh_options_set'

    • list itemmain.cpp:-1: erreur : undefined reference to `__imp_ssh_connect'

    What is my mistake here please ?



  • @LeLev

    My guess is that you are missing the LIBS statement with ssh lib when you are linking your application using your static lib.



  • @koahnig thx,
    So even if my app is not 'directly' using libssh (but a static lib that use libssh) i have to add LIBS statement in my app .pro ?



  • i just tested and the answer is yes.


  • Lifetime Qt Champion

    Hi,

    Because you are likely "leaking" symbols of libssh through your library so yes, you have to link to the dependencies of your library in that kind of case.



  • @SGaist said in create Staticlib:
    thank you fo the precision, may i ask how to give symbols of libssh to my library correctly, so the project that use my lib dont need to link to libssh also ?

    for the moment my libs .pro file looks like this :

    QT       += network
    QT       -= gui
    TARGET = CN16K_SFTP
    TEMPLATE = lib
    CONFIG += staticlib
    
    DEFINES += QT_DEPRECATED_WARNINGS
    LIBS += -L$$PWD -lssh
    
    SOURCES += \
            cn16k_sftp.cpp
    
    HEADERS += \
            cn16k_sftp.h \
        legacy.h \
        libssh.h \
        libsshpp.hpp \
        server.h \
        sftp.h \
        ssh2.h
    unix {
        target.path = /usr/lib
        INSTALLS += target
    }
    
    

    and in the project where i use my static Library i do this :

    LIBS += -L../CN16K_SFTP/build/release -llibCN16K_SFTP
    LIBS += -L../LIB/SSH_Lib/LSSH_0_8_0 -lssh // how to avoid this line 
    
    

  • Lifetime Qt Champion

    Don't use any of OpenSSL symbols in your header file. If needed use the PIMPLE idiom.



  • @SGaist could you please explain little bit, i don't use anything directly related to openSSL can't understand what do you mean sorry.

    In my static lib projects .pro i dont set this,

    INCLUDEPATH += ../LIB/SSH_Lib/SSH
    

    insted, i import the files in the project folder and do :

    HEADERS += \
            cn16k_sftp.h \
        legacy.h \
        libssh.h \
        libsshpp.hpp \
        server.h \
        sftp.h \
        ssh2.h
    

    Is this related to what you told me ?
    Thank you


  • Lifetime Qt Champion

    Since I don't know what your code looks like, I can't answer.

    What are all these files ?



  • @SGaist this are libssh source files, i imported this files in the project path insted of doing
    INCLUDEPATH += path/to/sources

    legacy.h
    libssh.h
    libsshpp.hpp
    server.h
    sftp.h
    ssh2.h

    and this one is the QObject derived class where i use libssh ( my lib )
    cn16k_sftp.h \


  • Lifetime Qt Champion

    Well, that's not the proper way of using an external library. You shouldn't copy over files like that.



  • @SGaist do i have to set the
    INCLUDEPATH += path/to/sources
    instead of copying the files ?

    Thnank you



  • If i delete libssh source files from my Static library and instead do
    INCLUDEPATH += ../path/to/libssh/sources

    Then in the project where i use my Static Lib i have to set
    INCLUDEPATH += ../path/to/libssh/sources also



  • @LeLev

    If you consider it from a logical point of view, one will have different folders for different libraries. For smaller libraries especially when you are starting your own library this is not obvious from the beginning, but you will notice with time.
    For third party I would consider it as mandatory, because how would you be able to change to another version. It will be a nightmare when you have mixed the headers with your own or other third party libs.
    This explains also why third party libraries have typically certain folder structure. There is often a bin or lib folder holding the library versions. There is a src or inc(lude) folder holding the headers as required. Names might vary, but the structure is typical.

    The conclusion of these philosophic excusions is that you better keep everything separated because you may want to change to a new version with very little work. The solution is to use somewhere path settings to point to the proper folders. You find this in different flavors of "make" applications.

    For qmake it would be INCLUDEPATH and LIBS. For use of a newer version of a library for tests you simply change the path entries there. If something is not to your liking you simply change back or restore the old .pro file.

    @LeLev said in create Staticlib:

    If i delete libssh source files from my Static library and instead do
    INCLUDEPATH += ../path/to/libssh/sources

    Then in the project where i use my Static Lib i have to set
    INCLUDEPATH += ../path/to/libssh/sources also

    Yes, under the assumnption that the path is pointing to the proper includes.



  • @koahnig thank you very much.
    I really appreciate the explanations


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.