writing c++ code in extern c

限于喜欢 提交于 2020-04-07 03:51:27

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!