C / C ++ extern“C”变量编译宏

时间:2014-07-05 15:13:44

标签: c++ c macros

如何测试我是否使用宏在C ++或C中进行链接? 假设我的代码应该在C ++文件中作为C链接,我会使用extern "C"{//code here}来使我的代码在链接中作为C。如何设置我的name.c文件以适用于C和C ++。有点像...

#ifdef C++//or other macro
extern "C"{
#endif

#ifdef C++
}
#endif

替换" C ++"的正确宏是什么?我有上面的,它会跨平台吗?或者我如何将其设置为跨平台?

此外,在C ++文件中对C代码执行extern "C"{}有什么意义?

感谢任何帮助。提前谢谢。

1 个答案:

答案 0 :(得分:2)

  

"替换" C ++""

的正确宏是什么?

它是#ifdef __cplusplus

#ifdef __cplusplus
extern "C"{
#endif

//.....

#ifdef __cplusplus
}
#endif
  

对于" ......必须要做的意义是什么......?"

阅读:In C++ source, what is the effect of extern “C”?