简体   繁体   中英

writing c++ code in extern c

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!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM