如何在C ++中使用C代码 [英] How to use C code in C++

查看:87
本文介绍了如何在C ++中使用C代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个小问题: C ++可以在程序中使用C头文件吗?

Just a small question: Can C++ use C header files in a program?

这可能是一个奇怪的问题,基本上我需要在C ++中使用其他程序(用C语言制作)的源代码.通常,两个头文件之间有什么区别?也许如果我改变一些图书馆... 我希望你能帮助我.

This might be a weird question, basically I need to use the source code from other program (made in C language) in a C++ one. Is there any difference between both header files in general? Maybe if I change some libraries... I hope you can help me.

推荐答案

是的,您可以在C ++代码中包含C标头.添加这个是正常的:

Yes, you can include C headers in C++ code. It's normal to add this:

#ifdef __cplusplus
extern "C"
{
#endif

// C header here

#ifdef __cplusplus
}
#endif

以便C ++编译器知道将函数声明等视为C,而不是C ++.

so that the C++ compiler knows that function declarations etc. should be treated as C and not C++.

这篇关于如何在C ++中使用C代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆