Call external library weird behavior



  • Hi,
    I have gcc a shared library that print out hello world and return sum. but when it is used in Qt, it return sum first, and print string after closing the form.

    1. Could someone tell me what is wrong?
    2. When I run ./HelloWorld (this qt project) in debug folder, it said cannot find shared library libadd.so, but I have copied libadd.so to debug folder.

    used this command to generate so file.
    @gcc add.c -shared -fPIC -o libadd.so@

    this is add.h file
    @#ifndef ADD_H
    #define ADD_H
    #include <stdio.h>
    #include <stdint.h>
    extern "C"{
    extern int add(int a,int b);
    }
    #endif
    @

    this is add.c file

    @#include <stdint.h>
    #include <stdio.h>
    #include <string.h>
    int add(int a, int b){
    printf("Hello World");
    return a + b;
    }
    @

    HelloWorld.pro content.
    @LIBS += -L$$PWD/ -ladd

    INCLUDEPATH += $$PWD/
    DEPENDPATH += $$PWD/@

    @
    #include "MainWindow.h"
    #include <QApplication>
    #include "add.h"
    #include <QDebug>
    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    MainWindow w;

    int sum = add(1,2);
    qDebug()<< sum;
    w.show();
    return a.exec(&#41;;
    

    }

    @


  • Lifetime Qt Champion

    Hi,

    You need to add the folder where the library is to LD_LIBRARY_PATH before calling your application, that way the loader will search for it at the correct place



  • Thanks. That answered the second question. But first one is still interesting.


  • Lifetime Qt Champion

    Sorry, I missed the first one.

    Nothing wrong, IIRC it's a question of buffering but I don't remember the exact details


Log in to reply
 

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