关于C++中调用C代码的问题

claudehotline 2014-05-15 05:13:29
请问C++程序中可以调用C写的第三方库的函数吗?
...全文
209 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2014-05-15
  • 打赏
  • 举报
回复
Mixed-Language Programming with C++ Home | Overview | How Do I C++ uses the same calling convention and parameter-passing techniques as C, but naming conventions are different because of C++ decoration of external symbols. By causing C++ to drop name decoration, the extern "C" syntax makes it possible for a C++ module to share data and routines with other languages. The following example declares prn as an external function using the C naming convention. This declaration appears in C++ source code. extern "C" { void prn(); } To call functions written in Fortran (or MASM), declare the function as you would in C and use a "C" linkage specification. For example, to call the Fortran function FACT from C++, declare it as follows: extern "C" { int __stdcall FACT( int n ); } The extern "C" syntax can be used to adjust a call from C++ to other languages, or to change the naming convention of C++ routines called from other languages. However, extern "C" can be used only from within C++. If the C++ code does not use extern "C" and cannot be changed, you can call C++ routines only by determining the name decoration and generating it from the other language. You can always determine the decoration by using the DUMPBIN utility. Use this approach only as a last resort, because the decoration scheme is not guaranteed to remain the same between versions. Use of extern "C" has some restrictions: You cannot declare a member function with extern "C". You can specify extern "C" for only one instance of an overloaded function; all other instances of an overloaded function have C++ linkage. For more information on the extern "C" linkage specification, see Linkage Specifications in C++ Language Reference.
claudehotline 2014-05-15
  • 打赏
  • 举报
回复
具体怎么调用啊
mujiok2003 2014-05-15
  • 打赏
  • 举报
回复
当然可以,小心c++重载机制的名字编码。

65,209

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧