PDA

View Full Version : Problems calling C function in C++/Qt class



Rayven
2nd June 2006, 17:59
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:


#ifndef READ_FILE
#define READ_FILE

int readFile( FILE* fd, int a, int b, char *data);

#endif


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:


extern int readFile( FILE* fd, int a, int b, char *data);

int readFile( FILE* fd, int a, int b, char *data);

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

jacek
2nd June 2006, 18:24
C and C++ have different name mangling schemes, try this:

#ifndef READ_FILE
#define READ_FILE

#ifdef __cplusplus
extern "C"
{
#endif

int readFile( FILE* fd, int a, int b, char *data );

#ifdef __cplusplus
};
#endif

#endif

Rayven
2nd June 2006, 22:32
Yes, that works! Thanks!

This also works:

In the C++ file



extern "C" {
#include "readFile.h"
}


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