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. Calling QT c++ function from a c function: undefined reference
Forum Update on Monday, May 27th 2025

Calling QT c++ function from a c function: undefined reference

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 709 Views
  • 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.
  • V Offline
    V Offline
    viniltc
    wrote on 20 Mar 2019, 16:37 last edited by
    #1

    Hi all
    I am using two source code files - one with c code and the other with c++ code as well as using both c and c++ libraries.

    The main function is c++ code. In the additional functions that i have written, I want to call c functions from some c++ functions and vice-versa.

    I have defined the function (send_using_qtserial) to be called from a C file as this in my MainWindow.cpp

    extern "C" size_t MainWindow::send_using_qtserial(uint8_t *data, size_t len)
    {
        return serial->write((const char *)data, (qint64)len);
    }
    

    I have called that function in Protocol.c as follows:

    extern size_t send_using_qtserial(uint8_t *data, size_t len);
    size_t UART_Write_Out(const USART_MODULE_ID id, void * source, size_t nBytes)
    {
        uint8_t *data= (uint8_t*)source;
        return send_using_qtserial(data, nBytes);
    }
    

    Unfortunately, I cannot seem to call a c++ function from a c function. I get the
    the build error 'undefined reference to 'send_using_qtserial'' error for the c++ function in the c function I am trying to call it from.

    Any suggestions?

    1 Reply Last reply
    0
    • V Offline
      V Offline
      VRonin
      wrote on 20 Mar 2019, 16:44 last edited by
      #2

      I want to call c functions from some c++

      This is fine

      and vice-versa

      This is the problem. C doesn't know classes so it can't see methods inside classes.
      You need to make send_using_qtserial a global static function, it can't be a method of MainWindow

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      5
      • V Offline
        V Offline
        viniltc
        wrote on 20 Mar 2019, 17:21 last edited by
        #3

        @VRonin Thanks a lot, I got your point

        1 Reply Last reply
        1
        • P Pl45m4 referenced this topic on 15 Oct 2024, 02:11

        1/3

        20 Mar 2019, 16:37

        • Login

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