简体   繁体   English

在extern中编写c ++代码

[英]writing c++ code in extern c

I'm trying to implement an external c++ header interface that will be produced as a shared library. 我正在尝试实现一个外部c ++头接口,它将作为共享库生成。 Their example interface has c style functionality wrapped in extern "C" as they don't want name mangling to be performed. 它们的示例接口具有包含在extern“C”中的c样式功能,因为它们不希望执行名称修改。

My current implementation is qt dependent. 我当前的实现依赖于qt。 Can I now place this qt code in the extern "C" and expect it to work out of the box? 我现在可以将这个qt代码放在extern“C”中并期望它开箱即用吗? 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. extern "C"不会阻止您在函数内部使用C ++。 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. 由于名称没有被破坏,并且调用序列可能与标准C ++调用序列不同,因此必须确保该函数在出现的任何C ++转换单元中声明为extern "C" Also, "C" names are not namespaced, but you probably knew that. 此外, "C"名称不是命名空间,但您可能知道这一点。

And as far as I know, Qt is not doing anything to make your code more or less C-friendly. 据我所知,Qt没有做任何事情来使您的代码或多或少地对C友好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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