Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to provide C callbacks in a library



  • Hey all,

    I don't have a ton of experience building libs. I was going to turn the core of my app into a static library, to re-use in some other projects. But the project includes vendor-provided SDK code (to interact with a piece of hardware) that needs to call C functions in the host app. Callbacks, I guess.

    Currently these functions are in main.cpp as extern "C". I'm trying to isolate all of the functionality that relies on them (and a bunch of other functionality, like DB access) in the lib, which can offer up C++ objects to be used by a Qt Quick GUI. Is this feasible?

    Thanks!


  • Lifetime Qt Champion

    Hi
    QML already expects you to do c++ back end for data and processing so
    calling some SDK function and collect data from the callbacks is absolutely feasible and
    also kinda expected in many use cases.
    So yes, hide the callbacks and C function behind c++ classes exposing the data to QML via
    properties or a model and it should work nicely.



  • @mrjj Thanks, but the SDK is going to call C functions back. I don't know where to put the C functions in the library, because there's no main.cpp.


  • Lifetime Qt Champion

    @Stokestack then simply put them in another C++ file and declare them as extern C.



  • @aha_1980 Thanks. I don't know why I was thinking this wouldn't work in a lib.


Log in to reply