Planned maintenance has been done but it did not solve the problem. So work will continue on this and a new time for trying updates will be announced asap.

Building a c shared library in QtCreator



  • Hi all,

    I have a project that needs to build a DSO with few dependencies and an initialisation function. I'm using QtCreator and qmake.

    In the .pro file created I have

    TEMPLATE = lib
    

    There is only a single c source file.

    It builds the DSO using the c++ compiler . The question is how do I instruct it to use c to link the DSO, and secondly how do I pass additional arguments. I was trying

    LINK = cc
    LFLAGS += -Wl,-init,MyInit
    

    But neither seem to have an effect....


  • Qt Champions 2018

    @martyn What is the file name extension of you C file? Is it .cpp or .c?



  • Its ".c" ...

    Martyn



  • @martyn
    shouldn't that be

    QMAKE_LINK = cc
    QMKAE_LFLAGS+= -Wl,-init,MyInit
    
    

    ?


  • Qt Champions 2018

    @gerd is correct. The question to @Martyn is rather, why is QMAKE_LINK needed?

    The question is how do I instruct it to use c to link the DSO

    There is no such thing as a C or C++ linker. The linker links just symbols. If your file is compiled with a C++ compiler, then something wents wrong.

    Please post the whole .pro file and the complete compile log for reference.



  • @gerd said in Building a c shared library in QtCreator:

    @martyn
    shouldn't that be

    QMAKE_LINK = cc
    QMKAE_LFLAGS+= -Wl,-init,MyInit
    

    OK - Yes the QMAKE_ versions do work :-)

    @aha_1980 said in Building a c shared library in QtCreator:

    @gerd is correct. The question to @Martyn is rather, why is QMAKE_LINK needed?

    The question is how do I instruct it to use c to link the DSO

    There is no such thing as a C or C++ linker. The linker links just symbols. If your file is compiled with a C++ compiler, then something wents wrong.

    Yes - I am wanting to use gcc as the (frontend/driver for the) linker to avoid libstdc++ being linked. To confirm, the source file was always being compiled by the c compiler.

    Please post the whole .pro file and the complete compile log for reference.

    QT       -= core gui
    TARGET = myproject
    TEMPLATE = lib
    DEFINES += MYPROJECT_LIBRARY
    DEFINES += QT_DEPRECATED_WARNINGS
    QMAKE_LFLAGS += -Wl,-init,myprojectInit
    #QMAKE_LINK = cc
    
    SOURCES += \
            binding.c
    
    HEADERS += \
            binding.h \
            pebinder_global.h 
    
    unix {
        target.path = /usr/lib
        INSTALLS += target
    }
    

    This provides the link step as

    g++ -Wl,-init,myprojectInit -Wl,-O1 -Wl,-z,relro -shared -Wl,-soname,libmyproject.so.1 -o libmyproject.so.1.0.0 binding.o -lpthread
    

    So, as you say, the question is should qmake be smart enough to realise that c++ isn't needed to link if all the source files are .c.

    Though I'm happy to now have a simple solution :-)


Log in to reply