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:
Code:
#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:
Code:
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!!
Re: Problems calling C function in C++/Qt class
C and C++ have different name mangling schemes, try this:
Code:
#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
Re: Problems calling C function in C++/Qt class
Yes, that works! Thanks!
This also works:
In the C++ file
Code:
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.