Important: Please read the Qt Code of Conduct -

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, but I have copied to debug folder.

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

    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);

    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;
    @ content.
    @LIBS += -L$$PWD/ -ladd


    #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;;
    return a.exec(&#41;;



  • Lifetime Qt Champion


    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