问题
I'm trying to implement an external c++ header interface that will be produced as a shared library. Their example interface has c style functionality wrapped in extern "C" as they don't want name mangling to be performed.
My current implementation is qt dependent. Can I now place this qt code in the extern "C" and expect it to work out of the box? If this works why?
pseudo code: // don't expect this to work and might contain errors.
extern "C"{
void doStuff(){
QString filename="Data.txt";
QFile file( filename );
if ( file.open(QIODevice::ReadWrite) )
{
QTextStream stream( &file );
std::stringstream ss;
ss<<"something"<< endl;
}
}
}
Thanks!
回答1:
extern "C"
doesn't keep you from using C++ inside the function. It only affects the way that the function is called (including the name of the function). Since the name is not mangled, and the calling sequence might be different from standard C++ calling sequence, you have to make sure that the function is declared as extern "C"
in any C++ translation unit in which it appears. Also, "C"
names are not namespaced, but you probably knew that.
And as far as I know, Qt is not doing anything to make your code more or less C-friendly.
来源:https://stackoverflow.com/questions/28492565/writing-c-code-in-extern-c