Results 1 to 3 of 3

Thread: Problems calling C function in C++/Qt class

  1. #1
    Join Date
    Jan 2006
    Location
    Third rock from the sun
    Posts
    106
    Thanks
    17
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Problems calling C function in C++/Qt class

    I am trying to call a C function from a C++/Qt class. Basically, the C++ code gets the filename from the open file dialog, and passes it to a C function to read and parse the file. I am using g++ and gcc to compile the C++ and C code respectivly.

    C header:
    Qt Code:
    1. #ifndef READ_FILE
    2. #define READ_FILE
    3.  
    4. int readFile( FILE* fd, int a, int b, char *data);
    5.  
    6. #endif
    To copy to clipboard, switch view to plain text mode 

    C++ calling function just calls readFile( fd, a, b, data ) with a #include "readFile" preprocessor. I have tried putting in an extern line and prototyping the function in the C++/Qt file:
    Qt Code:
    1. extern int readFile( FILE* fd, int a, int b, char *data);
    2.  
    3. int readFile( FILE* fd, int a, int b, char *data);
    To copy to clipboard, switch view to plain text mode 
    However, each time I compile, I get a linker error "Undefined Reference readFile(IO_FILE, int, int, char*)". Anyone have any thoughts on what my issue is? Also, if I comment out the call to readFile in the C++/Qt, the program builds and executes (but of course does not read through the file )

    Thanks!!

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problems calling C function in C++/Qt class

    C and C++ have different name mangling schemes, try this:
    Qt Code:
    1. #ifndef READ_FILE
    2. #define READ_FILE
    3.  
    4. #ifdef __cplusplus
    5. extern "C"
    6. {
    7. #endif
    8.  
    9. int readFile( FILE* fd, int a, int b, char *data );
    10.  
    11. #ifdef __cplusplus
    12. };
    13. #endif
    14.  
    15. #endif
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to jacek for this useful post:

    Rayven (2nd June 2006)

  4. #3
    Join Date
    Jan 2006
    Location
    Third rock from the sun
    Posts
    106
    Thanks
    17
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problems calling C function in C++/Qt class

    Yes, that works! Thanks!

    This also works:

    In the C++ file

    Qt Code:
    1. extern "C" {
    2. #include "readFile.h"
    3. }
    To copy to clipboard, switch view to plain text mode 

    I also found out that you can just compile the C code using g++, but there are some issues with the deletion.

Similar Threads

  1. I got two problems when I used static compiled library of QT4
    By qintm in forum Installation and Deployment
    Replies: 8
    Last Post: 20th April 2006, 09:52
  2. virtual overloaded functions and base class function call...
    By nouknouk in forum General Programming
    Replies: 7
    Last Post: 11th March 2006, 22:26
  3. Problems with Q_OBJECT and subclassing
    By renaissanz in forum Qt Programming
    Replies: 4
    Last Post: 21st February 2006, 23:18
  4. Qt 4.1 and KDE 3.5.1 on OSX 10.2.8
    By Ptero-4 in forum Installation and Deployment
    Replies: 6
    Last Post: 6th February 2006, 03:44
  5. Replies: 25
    Last Post: 15th January 2006, 01:53

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.