Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Problem with linking C library to Qt project
Forum Updated to NodeBB v4.3 + New Features

Problem with linking C library to Qt project

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 293 Views 2 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.
  • A Offline
    A Offline
    Alex Lyapin
    wrote on last edited by
    #1

    I know this is a 100times discussed topic, but after number of attempts, I just can't find the solution since don't understand the situation - no errors.
    I am trying to connect dll "C" library to my project and receiving some crazy code in App output "...exited with code -1073741515" as well as empty console with Press <RETURN> to close this window...

    So, here are mine:
    mylib.c:

    #include "mylib.h"
    int mysum(int a, int b){
    	return a + b;
    }
    

    mylib.h:

    #ifdef __cplusplus
    extern "C" {
    #endif
    #define EXPORT __declspec(dllexport)
    
    EXPORT int mysum(int, int);
    
    #ifdef __cplusplus
    }
    #endif
    

    testlib.pro:

    QT -= gui
    
    CONFIG += c++11 console
    CONFIG -= app_bundle
    
    SOURCES += \
            main.cpp
    
    LIBS += -L$$PWD/../../../../TestDLL/ -lmylib
    
    INCLUDEPATH += $$PWD/../../../../TestDLL
    DEPENDPATH += $$PWD/../../../../TestDLL
    
    HEADERS += \
        ../../../../TestDLL/mylib.h
    

    main.cpp:

    #include <QCoreApplication>
    #include "mylib.h"
    #include <QtDebug>
    
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
        qDebug()<<mysum(1,2);
        return a.exec();
    }
    

    I think, I am missing something with linkage in pro file, but can't get what.
    TestDLL is the folder with:
    mylib.dll
    mylib.h
    mylib.c
    libmylib.a

    to build the dll I've used:

    gcc -c mylib.c
    gcc -shared -o mylib.dll -Wl,--out-implib,libmylib.a mylib.o
    

    Will be appreciated for some hints... Thank you

    1 Reply Last reply
    0
    • Kent-DorfmanK Offline
      Kent-DorfmanK Offline
      Kent-Dorfman
      wrote on last edited by
      #2

      what happens when you build the app with libmylib.a or mylib.o instead of the dll? Verify that the error is related to the dll linkage, and not the .o or .a file create when compiling the library.

      What happens when you use the dll in a console app that is NOT a Qt app, but a simple gcc console app using std::cout << myint(a,b)

      1 Reply Last reply
      0
      • A Offline
        A Offline
        Alex Lyapin
        wrote on last edited by
        #3

        @Kent-Dorfman , thank you for the hint!
        So, what I've changed:

        1. in mylib.h :
        #ifdef BUILDING_DLL
        #define EXPORT __declspec(dllexport)
        #else
        #define EXPORT __declspec(dllimport)
        #endif
        

        and for compiling dll:

        gcc -c -DBUILDING_DLL mylib.c
        gcc -shared -o mylib.dll mylib.o -Wl,--out-implib,libmylib.a
        

        Plus in .pro file:

        LIBS += "$$PWD/../../../../TestDLL/mylib.dll"
        

        I believe, the line in .pro file is the most important. Now everything works fine.

        1 Reply Last reply
        1

        • Login

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