Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt Creator and other tools
  4. Call external library weird behavior
Forum Updated to NodeBB v4.3 + New Features

Call external library weird behavior

Scheduled Pinned Locked Moved Qt Creator and other tools
4 Posts 2 Posters 1.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sharethl
    wrote on last edited by
    #1

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

    }

    @

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • S Offline
        S Offline
        sharethl
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Sorry, I missed the first one.

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

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved